OK, I give up. Pigglesworth, HAALP. What do I do wrong??!
[reactos.git] / reactos / dll / directx / wine / wined3d / texture.c
1 /*
2 * IWineD3DTexture 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 /* *******************************************
31 IWineD3DTexture IUnknown parts follow
32 ******************************************* */
33 static HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DTexture *iface, REFIID riid, LPVOID *ppobj)
34 {
35 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
36 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
37 if (IsEqualGUID(riid, &IID_IUnknown)
38 || IsEqualGUID(riid, &IID_IWineD3DBase)
39 || IsEqualGUID(riid, &IID_IWineD3DResource)
40 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
41 || IsEqualGUID(riid, &IID_IWineD3DTexture)){
42 IUnknown_AddRef(iface);
43 *ppobj = This;
44 return WINED3D_OK;
45 }
46 *ppobj = NULL;
47 return E_NOINTERFACE;
48 }
49
50 static ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DTexture *iface) {
51 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
52 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
53 return InterlockedIncrement(&This->resource.ref);
54 }
55
56 static ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DTexture *iface) {
57 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
58 ULONG ref;
59 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
60 ref = InterlockedDecrement(&This->resource.ref);
61 if (ref == 0) {
62 IWineD3DTexture_Destroy(iface, D3DCB_DefaultDestroySurface);
63 }
64 return ref;
65 }
66
67
68 /* ****************************************************
69 IWineD3DTexture IWineD3DResource parts follow
70 **************************************************** */
71 static HRESULT WINAPI IWineD3DTextureImpl_GetDevice(IWineD3DTexture *iface, IWineD3DDevice** ppDevice) {
72 return resource_get_device((IWineD3DResource *)iface, ppDevice);
73 }
74
75 static HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
76 return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
77 }
78
79 static HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
80 return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
81 }
82
83 static HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid) {
84 return resource_free_private_data((IWineD3DResource *)iface, refguid);
85 }
86
87 static DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD PriorityNew) {
88 return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
89 }
90
91 static DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface) {
92 return resource_get_priority((IWineD3DResource *)iface);
93 }
94
95 void texture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb) {
96
97 /* Override the IWineD3DResource PreLoad method */
98 unsigned int i;
99 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
100 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
101 BOOL srgb_mode;
102 BOOL *dirty;
103
104 TRACE("(%p) : About to load texture\n", This);
105
106 switch(srgb) {
107 case SRGB_RGB: srgb_mode = FALSE; break;
108 case SRGB_BOTH: texture_internal_preload(iface, SRGB_RGB);
109 case SRGB_SRGB: srgb_mode = TRUE; break;
110 /* DONTKNOW, and shut up the compiler */
111 default: srgb_mode = This->baseTexture.is_srgb; break;
112 }
113 dirty = srgb_mode ? &This->baseTexture.srgbDirty : &This->baseTexture.dirty;
114
115 if(!device->isInDraw) {
116 /* ActivateContext sets isInDraw to TRUE when loading a pbuffer into a texture, thus no danger of
117 * recursive calls
118 */
119 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
120 }
121
122 if (This->resource.format_desc->format == WINED3DFMT_P8
123 || This->resource.format_desc->format == WINED3DFMT_A8P8)
124 {
125 for (i = 0; i < This->baseTexture.levels; i++) {
126 if(palette9_changed((IWineD3DSurfaceImpl *)This->surfaces[i])) {
127 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
128 /* TODO: This is not necessarily needed with hw palettized texture support */
129 IWineD3DSurface_LoadLocation(This->surfaces[i], SFLAG_INSYSMEM, NULL);
130 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
131 IWineD3DSurface_ModifyLocation(This->surfaces[i], SFLAG_INTEXTURE, FALSE);
132 }
133 }
134 }
135 /* If the texture is marked dirty or the srgb sampler setting has changed since the last load then reload the surfaces */
136 if (*dirty) {
137 for (i = 0; i < This->baseTexture.levels; i++) {
138 IWineD3DSurface_LoadTexture(This->surfaces[i], srgb_mode);
139 }
140 } else {
141 TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
142 }
143
144 /* No longer dirty */
145 *dirty = FALSE;
146
147 return ;
148 }
149
150 static void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface) {
151 texture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
152 }
153
154 static void WINAPI IWineD3DTextureImpl_UnLoad(IWineD3DTexture *iface) {
155 unsigned int i;
156 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
157 TRACE("(%p)\n", This);
158
159 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
160 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
161 * surface is fine
162 */
163 for (i = 0; i < This->baseTexture.levels; i++) {
164 IWineD3DSurface_UnLoad(This->surfaces[i]);
165 surface_set_texture_name(This->surfaces[i], 0, FALSE); /* Delete rgb name */
166 surface_set_texture_name(This->surfaces[i], 0, TRUE); /* delete srgb name */
167 }
168
169 basetexture_unload((IWineD3DBaseTexture *)iface);
170 }
171
172 static WINED3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface) {
173 return resource_get_type((IWineD3DResource *)iface);
174 }
175
176 static HRESULT WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface, IUnknown **pParent) {
177 return resource_get_parent((IWineD3DResource *)iface, pParent);
178 }
179
180 /* ******************************************************
181 IWineD3DTexture IWineD3DBaseTexture parts follow
182 ****************************************************** */
183 static DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
184 return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
185 }
186
187 static DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
188 return basetexture_get_lod((IWineD3DBaseTexture *)iface);
189 }
190
191 static DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface) {
192 return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
193 }
194
195 static HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
196 return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
197 }
198
199 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface) {
200 return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
201 }
202
203 static void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface) {
204 basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
205 }
206
207 /* Internal function, No d3d mapping */
208 static BOOL WINAPI IWineD3DTextureImpl_SetDirty(IWineD3DTexture *iface, BOOL dirty) {
209 return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
210 }
211
212 static BOOL WINAPI IWineD3DTextureImpl_GetDirty(IWineD3DTexture *iface) {
213 return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
214 }
215
216 static HRESULT WINAPI IWineD3DTextureImpl_BindTexture(IWineD3DTexture *iface, BOOL srgb) {
217 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
218 BOOL set_gl_texture_desc;
219 HRESULT hr;
220
221 TRACE("(%p) : relay to BaseTexture\n", This);
222
223 hr = basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &set_gl_texture_desc);
224 if (set_gl_texture_desc && SUCCEEDED(hr)) {
225 UINT i;
226 for (i = 0; i < This->baseTexture.levels; ++i) {
227 if(This->baseTexture.is_srgb) {
228 surface_set_texture_name(This->surfaces[i], This->baseTexture.srgbTextureName, TRUE);
229 } else {
230 surface_set_texture_name(This->surfaces[i], This->baseTexture.textureName, FALSE);
231 }
232 }
233 /* Conditinal non power of two textures use a different clamping default. If we're using the GL_WINE_normalized_texrect
234 * partial driver emulation, we're dealing with a GL_TEXTURE_2D texture which has the address mode set to repeat - something
235 * that prevents us from hitting the accelerated codepath. Thus manually set the GL state. The same applies to filtering.
236 * Even if the texture has only one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW fallback on macos.
237 */
238 if(IWineD3DBaseTexture_IsCondNP2(iface)) {
239 ENTER_GL();
240 glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
241 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
242 glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
243 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
244 glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
245 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
246 glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
247 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
248 LEAVE_GL();
249 This->baseTexture.states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_CLAMP;
250 This->baseTexture.states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_CLAMP;
251 This->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
252 This->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
253 This->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
254 }
255 }
256
257 return hr;
258 }
259
260 static UINT WINAPI IWineD3DTextureImpl_GetTextureDimensions(IWineD3DTexture *iface) {
261 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
262 TRACE("(%p)\n", This);
263
264 return This->target;
265 }
266
267 static BOOL WINAPI IWineD3DTextureImpl_IsCondNP2(IWineD3DTexture *iface) {
268 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
269 TRACE("(%p)\n", This);
270
271 return This->cond_np2;
272 }
273
274 static void WINAPI IWineD3DTextureImpl_ApplyStateChanges(IWineD3DTexture *iface,
275 const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
276 const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
277 TRACE("(%p) : relay to BaseTexture\n", iface);
278 basetexture_apply_state_changes((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
279 }
280
281 /* *******************************************
282 IWineD3DTexture IWineD3DTexture parts follow
283 ******************************************* */
284 static void WINAPI IWineD3DTextureImpl_Destroy(IWineD3DTexture *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroySurface) {
285 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
286 unsigned int i;
287
288 TRACE("(%p) : Cleaning up\n",This);
289 for (i = 0; i < This->baseTexture.levels; i++) {
290 if (This->surfaces[i] != NULL) {
291 /* Clean out the texture name we gave to the surface so that the surface doesn't try and release it */
292 surface_set_texture_name(This->surfaces[i], 0, TRUE);
293 surface_set_texture_name(This->surfaces[i], 0, FALSE);
294 surface_set_texture_target(This->surfaces[i], 0);
295 IWineD3DSurface_SetContainer(This->surfaces[i], 0);
296 D3DCB_DestroySurface(This->surfaces[i]);
297 }
298 }
299 TRACE("(%p) : cleaning up base texture\n", This);
300 basetexture_cleanup((IWineD3DBaseTexture *)iface);
301 /* free the object */
302 HeapFree(GetProcessHeap(), 0, This);
303 }
304
305 static HRESULT WINAPI IWineD3DTextureImpl_GetLevelDesc(IWineD3DTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
306 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
307
308 if (Level < This->baseTexture.levels) {
309 TRACE("(%p) Level (%d)\n", This, Level);
310 return IWineD3DSurface_GetDesc(This->surfaces[Level], pDesc);
311 }
312 WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
313 return WINED3DERR_INVALIDCALL;
314 }
315
316 static HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface, UINT Level, IWineD3DSurface** ppSurfaceLevel) {
317 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
318 HRESULT hr = WINED3DERR_INVALIDCALL;
319
320 if (Level < This->baseTexture.levels) {
321 *ppSurfaceLevel = This->surfaces[Level];
322 IWineD3DSurface_AddRef(This->surfaces[Level]);
323 hr = WINED3D_OK;
324 TRACE("(%p) : returning %p for level %d\n", This, *ppSurfaceLevel, Level);
325 }
326 if (WINED3D_OK != hr) {
327 WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
328 *ppSurfaceLevel = NULL; /* Just to be on the safe side.. */
329 }
330 return hr;
331 }
332
333 static HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface, UINT Level, WINED3DLOCKED_RECT *pLockedRect,
334 CONST RECT *pRect, DWORD Flags) {
335 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
336 HRESULT hr = WINED3DERR_INVALIDCALL;
337
338 if (Level < This->baseTexture.levels) {
339 hr = IWineD3DSurface_LockRect(This->surfaces[Level], pLockedRect, pRect, Flags);
340 }
341 if (WINED3D_OK == hr) {
342 TRACE("(%p) Level (%d) success\n", This, Level);
343 } else {
344 WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
345 }
346
347 return hr;
348 }
349
350 static HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT Level) {
351 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
352 HRESULT hr = WINED3DERR_INVALIDCALL;
353
354 if (Level < This->baseTexture.levels) {
355 hr = IWineD3DSurface_UnlockRect(This->surfaces[Level]);
356 }
357 if ( WINED3D_OK == hr) {
358 TRACE("(%p) Level (%d) success\n", This, Level);
359 } else {
360 WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
361 }
362 return hr;
363 }
364
365 static HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, CONST RECT* pDirtyRect) {
366 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
367 This->baseTexture.dirty = TRUE;
368 This->baseTexture.srgbDirty = TRUE;
369 TRACE("(%p) : dirtyfication of surface Level (0)\n", This);
370 surface_add_dirty_rect(This->surfaces[0], pDirtyRect);
371
372 return WINED3D_OK;
373 }
374
375 const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
376 {
377 /* IUnknown */
378 IWineD3DTextureImpl_QueryInterface,
379 IWineD3DTextureImpl_AddRef,
380 IWineD3DTextureImpl_Release,
381 /* IWineD3DResource */
382 IWineD3DTextureImpl_GetParent,
383 IWineD3DTextureImpl_GetDevice,
384 IWineD3DTextureImpl_SetPrivateData,
385 IWineD3DTextureImpl_GetPrivateData,
386 IWineD3DTextureImpl_FreePrivateData,
387 IWineD3DTextureImpl_SetPriority,
388 IWineD3DTextureImpl_GetPriority,
389 IWineD3DTextureImpl_PreLoad,
390 IWineD3DTextureImpl_UnLoad,
391 IWineD3DTextureImpl_GetType,
392 /* IWineD3DBaseTexture */
393 IWineD3DTextureImpl_SetLOD,
394 IWineD3DTextureImpl_GetLOD,
395 IWineD3DTextureImpl_GetLevelCount,
396 IWineD3DTextureImpl_SetAutoGenFilterType,
397 IWineD3DTextureImpl_GetAutoGenFilterType,
398 IWineD3DTextureImpl_GenerateMipSubLevels,
399 IWineD3DTextureImpl_SetDirty,
400 IWineD3DTextureImpl_GetDirty,
401 IWineD3DTextureImpl_BindTexture,
402 IWineD3DTextureImpl_GetTextureDimensions,
403 IWineD3DTextureImpl_IsCondNP2,
404 IWineD3DTextureImpl_ApplyStateChanges,
405 /* IWineD3DTexture */
406 IWineD3DTextureImpl_Destroy,
407 IWineD3DTextureImpl_GetLevelDesc,
408 IWineD3DTextureImpl_GetSurfaceLevel,
409 IWineD3DTextureImpl_LockRect,
410 IWineD3DTextureImpl_UnlockRect,
411 IWineD3DTextureImpl_AddDirtyRect
412 };