Visual C++ backend for rbuild (for now just a hacked mingw backend) and related compi...
[reactos.git] / dll / directx / wine / wined3d / cubetexture.c
1 /*
2 * IWineD3DCubeTexture implementation
3 *
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29
30 static const GLenum cube_targets[6] = {
31 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
32 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
33 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
34 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
35 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
36 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
37 };
38
39 /* *******************************************
40 IWineD3DCubeTexture IUnknown parts follow
41 ******************************************* */
42 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
43 {
44 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
45 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
46 if (IsEqualGUID(riid, &IID_IUnknown)
47 || IsEqualGUID(riid, &IID_IWineD3DBase)
48 || IsEqualGUID(riid, &IID_IWineD3DResource)
49 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
50 || IsEqualGUID(riid, &IID_IWineD3DCubeTexture)) {
51 IUnknown_AddRef(iface);
52 *ppobj = This;
53 return S_OK;
54 }
55 *ppobj = NULL;
56 return E_NOINTERFACE;
57 }
58
59 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
60 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
61 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
62 return InterlockedIncrement(&This->resource.ref);
63 }
64
65 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
66 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
67 ULONG ref;
68 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
69 ref = InterlockedDecrement(&This->resource.ref);
70 if (ref == 0) {
71 IWineD3DCubeTexture_Destroy(iface, D3DCB_DefaultDestroySurface);
72 }
73 return ref;
74 }
75
76 /* ****************************************************
77 IWineD3DCubeTexture IWineD3DResource parts follow
78 **************************************************** */
79 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetDevice(IWineD3DCubeTexture *iface, IWineD3DDevice** ppDevice) {
80 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
81 }
82
83 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
84 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
85 }
86
87 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
88 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
89 }
90
91 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid) {
92 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
93 }
94
95 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD PriorityNew) {
96 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
97 }
98
99 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface) {
100 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
101 }
102
103 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface) {
104 /* Override the IWineD3DResource Preload method */
105 unsigned int i,j;
106 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
107 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
108 BOOL srgb_mode = This->baseTexture.is_srgb;
109 BOOL srgb_was_toggled = FALSE;
110
111 TRACE("(%p) : About to load texture: dirtified(%d)\n", This, This->baseTexture.dirty);
112
113 /* We only have to activate a context for gl when we're not drawing. In most cases PreLoad will be called during draw
114 * and a context was activated at the beginning of drawPrimitive
115 */
116 if(!device->isInDraw) {
117 /* No danger of recursive calls, ActivateContext sets isInDraw to true when loading
118 * offscreen render targets into their texture
119 */
120 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
121 } else if (GL_SUPPORT(EXT_TEXTURE_SRGB) && This->baseTexture.bindCount > 0) {
122 srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
123 srgb_was_toggled = (This->baseTexture.is_srgb != srgb_mode);
124 This->baseTexture.is_srgb = srgb_mode;
125 }
126
127 if (This->resource.format == WINED3DFMT_P8 || This->resource.format == WINED3DFMT_A8P8) {
128 for (i = 0; i < This->baseTexture.levels; i++) {
129 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
130 if(palette9_changed((IWineD3DSurfaceImpl *)This->surfaces[j][i])) {
131 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
132 /* TODO: This is not necessarily needed with hw palettized texture support */
133 IWineD3DSurface_LoadLocation(This->surfaces[j][i], SFLAG_INSYSMEM, NULL);
134 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
135 IWineD3DSurface_ModifyLocation(This->surfaces[j][i], SFLAG_INTEXTURE, FALSE);
136 }
137 }
138 }
139 }
140 /* If the texture is marked dirty or the srgb sampler setting has changed since the last load then reload the surfaces */
141 if (This->baseTexture.dirty) {
142 for (i = 0; i < This->baseTexture.levels; i++) {
143 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
144 IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
145 }
146 }
147 } else if (srgb_was_toggled) {
148 /* Loop is repeated in the else block with the extra AddDirtyRect line to avoid the alternative of
149 * checking srgb_was_toggled in every iteration, even when the texture is just dirty
150 */
151 if (This->baseTexture.srgb_mode_change_count < 20)
152 ++This->baseTexture.srgb_mode_change_count;
153 else
154 FIXME("Cubetexture (%p) has been reloaded at least 20 times due to WINED3DSAMP_SRGBTEXTURE changes on it\'s sampler\n", This);
155
156 for (i = 0; i < This->baseTexture.levels; i++) {
157 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
158 IWineD3DSurface_AddDirtyRect(This->surfaces[j][i], NULL);
159 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], This->baseTexture.textureName, cube_targets[j]);
160 IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
161 }
162 }
163 } else {
164 TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
165 }
166
167 /* No longer dirty */
168 This->baseTexture.dirty = FALSE;
169 return ;
170 }
171
172 static void WINAPI IWineD3DCubeTextureImpl_UnLoad(IWineD3DCubeTexture *iface) {
173 unsigned int i, j;
174 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
175 TRACE("(%p)\n", This);
176
177 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
178 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
179 * surface is fine
180 */
181 for (i = 0; i < This->baseTexture.levels; i++) {
182 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
183 IWineD3DSurface_UnLoad(This->surfaces[j][i]);
184 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], 0, IWineD3DTexture_GetTextureDimensions(iface));
185 }
186 }
187
188 IWineD3DBaseTextureImpl_UnLoad((IWineD3DBaseTexture *) iface);
189 }
190
191 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface) {
192 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
193 }
194
195 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface, IUnknown **pParent) {
196 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
197 }
198
199 /* ******************************************************
200 IWineD3DCubeTexture IWineD3DBaseTexture parts follow
201 ****************************************************** */
202 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
203 return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
204 }
205
206 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
207 return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
208 }
209
210 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface) {
211 return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
212 }
213
214 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
215 return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
216 }
217
218 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface) {
219 return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
220 }
221
222 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface) {
223 IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
224 }
225
226 /* Internal function, No d3d mapping */
227 static BOOL WINAPI IWineD3DCubeTextureImpl_SetDirty(IWineD3DCubeTexture *iface, BOOL dirty) {
228 return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, dirty);
229 }
230
231 /* Internal function, No d3d mapping */
232 static BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface) {
233 return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
234 }
235
236 static HRESULT WINAPI IWineD3DCubeTextureImpl_BindTexture(IWineD3DCubeTexture *iface) {
237 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
238 BOOL set_gl_texture_desc = This->baseTexture.textureName == 0;
239 HRESULT hr;
240
241 TRACE("(%p) : relay to BaseTexture\n", This);
242
243 hr = IWineD3DBaseTextureImpl_BindTexture((IWineD3DBaseTexture *)iface);
244 if (set_gl_texture_desc && SUCCEEDED(hr)) {
245 UINT i, j;
246 for (i = 0; i < This->baseTexture.levels; ++i) {
247 for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z; ++j) {
248 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], This->baseTexture.textureName, cube_targets[j]);
249 }
250 }
251 }
252
253 return hr;
254 }
255
256 static UINT WINAPI IWineD3DCubeTextureImpl_GetTextureDimensions(IWineD3DCubeTexture *iface){
257 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
258 TRACE("(%p)\n", This);
259
260 return GL_TEXTURE_CUBE_MAP_ARB;
261 }
262
263 static BOOL WINAPI IWineD3DCubeTextureImpl_IsCondNP2(IWineD3DCubeTexture *iface) {
264 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
265 TRACE("(%p)\n", This);
266
267 return FALSE;
268 }
269
270 static void WINAPI IWineD3DCubeTextureImpl_ApplyStateChanges(IWineD3DCubeTexture *iface,
271 const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
272 const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
273 TRACE("(%p) : relay to BaseTexture\n", iface);
274 IWineD3DBaseTextureImpl_ApplyStateChanges((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
275 }
276
277
278 /* *******************************************
279 IWineD3DCubeTexture IWineD3DCubeTexture parts follow
280 ******************************************* */
281 static void WINAPI IWineD3DCubeTextureImpl_Destroy(IWineD3DCubeTexture *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroySurface) {
282 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
283 unsigned int i,j;
284 TRACE("(%p) : Cleaning up\n",This);
285 for (i = 0; i < This->baseTexture.levels; i++) {
286 for (j = 0; j < 6; j++) {
287 if (This->surfaces[j][i] != NULL) {
288 /* Clean out the texture name we gave to the surface so that the surface doesn't try and release it */
289 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], 0, 0);
290 /* Cleanup the container */
291 IWineD3DSurface_SetContainer(This->surfaces[j][i], 0);
292 D3DCB_DestroySurface(This->surfaces[j][i]);
293 }
294 }
295 }
296 IWineD3DBaseTextureImpl_CleanUp((IWineD3DBaseTexture *) iface);
297 /* finally delete the object */
298 HeapFree(GetProcessHeap(), 0, This);
299 }
300
301 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
302 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
303
304 if (Level < This->baseTexture.levels) {
305 TRACE("(%p) level (%d)\n", This, Level);
306 return IWineD3DSurface_GetDesc(This->surfaces[0][Level], pDesc);
307 }
308 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
309 return WINED3DERR_INVALIDCALL;
310 }
311
312 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
313 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
314 HRESULT hr = WINED3DERR_INVALIDCALL;
315
316 if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
317 *ppCubeMapSurface = This->surfaces[FaceType][Level];
318 IWineD3DSurface_AddRef(*ppCubeMapSurface);
319
320 hr = WINED3D_OK;
321 }
322 if (WINED3D_OK == hr) {
323 TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p\n", This, FaceType, Level, This->surfaces[FaceType][Level]);
324 } else {
325 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
326 }
327
328 return hr;
329 }
330
331 static HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
332 HRESULT hr = WINED3DERR_INVALIDCALL;
333 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
334
335 if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
336 hr = IWineD3DSurface_LockRect(This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
337 }
338
339 if (WINED3D_OK == hr) {
340 TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%u)\n", This, FaceType, Level, pLockedRect->pBits, hr);
341 } else {
342 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
343 }
344
345 return hr;
346 }
347
348 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level) {
349 HRESULT hr = WINED3DERR_INVALIDCALL;
350 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
351
352 if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
353 hr = IWineD3DSurface_UnlockRect(This->surfaces[FaceType][Level]);
354 }
355
356 if (WINED3D_OK == hr) {
357 TRACE("(%p) -> faceType(%d) level(%d) success(%u)\n", This, FaceType, Level, hr);
358 } else {
359 WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
360 }
361 return hr;
362 }
363
364 static HRESULT WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
365 HRESULT hr = WINED3DERR_INVALIDCALL;
366 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
367 This->baseTexture.dirty = TRUE;
368 TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);
369 if (FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
370 hr = IWineD3DSurface_AddDirtyRect(This->surfaces[FaceType][0], pDirtyRect);
371 } else {
372 WARN("(%p) overflow FaceType(%d)\n", This, FaceType);
373 }
374 return hr;
375 }
376
377
378 const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
379 {
380 /* IUnknown */
381 IWineD3DCubeTextureImpl_QueryInterface,
382 IWineD3DCubeTextureImpl_AddRef,
383 IWineD3DCubeTextureImpl_Release,
384 /* IWineD3DResource */
385 IWineD3DCubeTextureImpl_GetParent,
386 IWineD3DCubeTextureImpl_GetDevice,
387 IWineD3DCubeTextureImpl_SetPrivateData,
388 IWineD3DCubeTextureImpl_GetPrivateData,
389 IWineD3DCubeTextureImpl_FreePrivateData,
390 IWineD3DCubeTextureImpl_SetPriority,
391 IWineD3DCubeTextureImpl_GetPriority,
392 IWineD3DCubeTextureImpl_PreLoad,
393 IWineD3DCubeTextureImpl_UnLoad,
394 IWineD3DCubeTextureImpl_GetType,
395 /* IWineD3DBaseTexture */
396 IWineD3DCubeTextureImpl_SetLOD,
397 IWineD3DCubeTextureImpl_GetLOD,
398 IWineD3DCubeTextureImpl_GetLevelCount,
399 IWineD3DCubeTextureImpl_SetAutoGenFilterType,
400 IWineD3DCubeTextureImpl_GetAutoGenFilterType,
401 IWineD3DCubeTextureImpl_GenerateMipSubLevels,
402 IWineD3DCubeTextureImpl_SetDirty,
403 IWineD3DCubeTextureImpl_GetDirty,
404 IWineD3DCubeTextureImpl_BindTexture,
405 IWineD3DCubeTextureImpl_GetTextureDimensions,
406 IWineD3DCubeTextureImpl_IsCondNP2,
407 IWineD3DCubeTextureImpl_ApplyStateChanges,
408 /* IWineD3DCubeTexture */
409 IWineD3DCubeTextureImpl_Destroy,
410 IWineD3DCubeTextureImpl_GetLevelDesc,
411 IWineD3DCubeTextureImpl_GetCubeMapSurface,
412 IWineD3DCubeTextureImpl_LockRect,
413 IWineD3DCubeTextureImpl_UnlockRect,
414 IWineD3DCubeTextureImpl_AddDirtyRect
415 };