2 * IDirect3DStateBlock9 implementation
4 * Copyright 2002-2003 Raphael Junqueira
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
25 #include "d3d9_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d9
);
29 static inline struct d3d9_stateblock
*impl_from_IDirect3DStateBlock9(IDirect3DStateBlock9
*iface
)
31 return CONTAINING_RECORD(iface
, struct d3d9_stateblock
, IDirect3DStateBlock9_iface
);
34 static HRESULT WINAPI
d3d9_stateblock_QueryInterface(IDirect3DStateBlock9
*iface
, REFIID riid
, void **out
)
36 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
38 if (IsEqualGUID(riid
, &IID_IDirect3DStateBlock9
)
39 || IsEqualGUID(riid
, &IID_IUnknown
))
41 IDirect3DStateBlock9_AddRef(iface
);
46 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
52 static ULONG WINAPI
d3d9_stateblock_AddRef(IDirect3DStateBlock9
*iface
)
54 struct d3d9_stateblock
*stateblock
= impl_from_IDirect3DStateBlock9(iface
);
55 ULONG refcount
= InterlockedIncrement(&stateblock
->refcount
);
57 TRACE("%p increasing refcount to %u.\n", iface
, refcount
);
62 static ULONG WINAPI
d3d9_stateblock_Release(IDirect3DStateBlock9
*iface
)
64 struct d3d9_stateblock
*stateblock
= impl_from_IDirect3DStateBlock9(iface
);
65 ULONG refcount
= InterlockedDecrement(&stateblock
->refcount
);
67 TRACE("%p decreasing refcount to %u.\n", iface
, refcount
);
72 wined3d_stateblock_decref(stateblock
->wined3d_stateblock
);
73 wined3d_mutex_unlock();
75 IDirect3DDevice9Ex_Release(stateblock
->parent_device
);
76 heap_free(stateblock
);
82 static HRESULT WINAPI
d3d9_stateblock_GetDevice(IDirect3DStateBlock9
*iface
, IDirect3DDevice9
**device
)
84 struct d3d9_stateblock
*stateblock
= impl_from_IDirect3DStateBlock9(iface
);
86 TRACE("iface %p, device %p.\n", iface
, device
);
88 *device
= (IDirect3DDevice9
*)stateblock
->parent_device
;
89 IDirect3DDevice9_AddRef(*device
);
91 TRACE("Returning device %p.\n", *device
);
96 static HRESULT WINAPI
d3d9_stateblock_Capture(IDirect3DStateBlock9
*iface
)
98 struct d3d9_stateblock
*stateblock
= impl_from_IDirect3DStateBlock9(iface
);
100 TRACE("iface %p.\n", iface
);
102 wined3d_mutex_lock();
103 wined3d_stateblock_capture(stateblock
->wined3d_stateblock
);
104 wined3d_mutex_unlock();
109 static HRESULT WINAPI
d3d9_stateblock_Apply(IDirect3DStateBlock9
*iface
)
111 struct d3d9_stateblock
*stateblock
= impl_from_IDirect3DStateBlock9(iface
);
112 struct wined3d_texture
*wined3d_texture
;
113 unsigned int i
, offset
, stride
, stage
;
114 struct wined3d_buffer
*wined3d_buffer
;
115 struct d3d9_vertexbuffer
*buffer
;
116 enum wined3d_format_id format
;
117 struct d3d9_texture
*texture
;
118 struct d3d9_device
*device
;
121 TRACE("iface %p.\n", iface
);
123 wined3d_mutex_lock();
124 wined3d_stateblock_apply(stateblock
->wined3d_stateblock
);
125 device
= impl_from_IDirect3DDevice9Ex(stateblock
->parent_device
);
126 device
->sysmem_vb
= 0;
127 for (i
= 0; i
< D3D9_MAX_STREAMS
; ++i
)
129 if (FAILED(hr
= wined3d_device_get_stream_source(device
->wined3d_device
,
130 i
, &wined3d_buffer
, &offset
, &stride
)))
132 if (!wined3d_buffer
|| !(buffer
= wined3d_buffer_get_parent(wined3d_buffer
)))
134 if (buffer
->draw_buffer
)
135 device
->sysmem_vb
|= 1u << i
;
137 device
->sysmem_ib
= (wined3d_buffer
= wined3d_device_get_index_buffer(device
->wined3d_device
, &format
, &offset
))
138 && (buffer
= wined3d_buffer_get_parent(wined3d_buffer
)) && buffer
->draw_buffer
;
139 device
->auto_mipmaps
= 0;
140 for (i
= 0; i
< D3D9_MAX_TEXTURE_UNITS
; ++i
)
142 stage
= i
>= 16 ? i
- 16 + D3DVERTEXTEXTURESAMPLER0
: i
;
144 if ((wined3d_texture
= wined3d_device_get_texture(device
->wined3d_device
, stage
))
145 && (texture
= wined3d_texture_get_parent(wined3d_texture
))
146 && texture
->usage
& D3DUSAGE_AUTOGENMIPMAP
)
147 device
->auto_mipmaps
|= 1u << i
;
149 device
->auto_mipmaps
&= ~(1u << i
);
151 wined3d_mutex_unlock();
157 static const struct IDirect3DStateBlock9Vtbl d3d9_stateblock_vtbl
=
160 d3d9_stateblock_QueryInterface
,
161 d3d9_stateblock_AddRef
,
162 d3d9_stateblock_Release
,
163 /* IDirect3DStateBlock9 */
164 d3d9_stateblock_GetDevice
,
165 d3d9_stateblock_Capture
,
166 d3d9_stateblock_Apply
,
169 HRESULT
stateblock_init(struct d3d9_stateblock
*stateblock
, struct d3d9_device
*device
,
170 D3DSTATEBLOCKTYPE type
, struct wined3d_stateblock
*wined3d_stateblock
)
174 stateblock
->IDirect3DStateBlock9_iface
.lpVtbl
= &d3d9_stateblock_vtbl
;
175 stateblock
->refcount
= 1;
177 if (wined3d_stateblock
)
179 stateblock
->wined3d_stateblock
= wined3d_stateblock
;
183 wined3d_mutex_lock();
184 hr
= wined3d_stateblock_create(device
->wined3d_device
,
185 (enum wined3d_stateblock_type
)type
, &stateblock
->wined3d_stateblock
);
186 wined3d_mutex_unlock();
189 WARN("Failed to create wined3d stateblock, hr %#x.\n", hr
);
194 stateblock
->parent_device
= &device
->IDirect3DDevice9Ex_iface
;
195 IDirect3DDevice9Ex_AddRef(stateblock
->parent_device
);