2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
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 "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture
);
28 /* Context activation is done by the caller. */
29 static HRESULT
cubetexture_bind(IWineD3DBaseTextureImpl
*texture
,
30 const struct wined3d_gl_info
*gl_info
, BOOL srgb
)
32 BOOL set_gl_texture_desc
;
35 TRACE("texture %p, gl_info %p, srgb %#x.\n", texture
, gl_info
, srgb
);
37 hr
= basetexture_bind(texture
, gl_info
, srgb
, &set_gl_texture_desc
);
38 if (set_gl_texture_desc
&& SUCCEEDED(hr
))
40 UINT sub_count
= texture
->baseTexture
.level_count
* texture
->baseTexture
.layer_count
;
41 BOOL srgb_tex
= !gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
] && texture
->baseTexture
.is_srgb
;
42 GLuint name
= srgb_tex
? texture
->baseTexture
.texture_srgb
.name
: texture
->baseTexture
.texture_rgb
.name
;
45 for (i
= 0; i
< sub_count
; ++i
)
47 IWineD3DSurfaceImpl
*surface
= surface_from_resource(texture
->baseTexture
.sub_resources
[i
]);
48 surface_set_texture_name(surface
, name
, srgb_tex
);
55 /* Do not call while under the GL lock. */
56 static void cubetexture_preload(IWineD3DBaseTextureImpl
*texture
, enum WINED3DSRGB srgb
)
58 UINT sub_count
= texture
->baseTexture
.level_count
* texture
->baseTexture
.layer_count
;
59 IWineD3DDeviceImpl
*device
= texture
->resource
.device
;
60 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
61 struct wined3d_context
*context
= NULL
;
62 struct gl_texture
*gl_tex
;
66 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
75 cubetexture_preload(texture
, SRGB_RGB
);
83 srgb_mode
= texture
->baseTexture
.is_srgb
;
87 gl_tex
= basetexture_get_gl_texture(texture
, gl_info
, srgb_mode
);
89 /* We only have to activate a context for gl when we're not drawing.
90 * In most cases PreLoad will be called during draw and a context was
91 * activated at the beginning of drawPrimitive. */
92 if (!device
->isInDraw
)
94 /* No danger of recursive calls, context_acquire() sets isInDraw to true
95 * when loading offscreen render targets into their texture. */
96 context
= context_acquire(device
, NULL
);
99 if (texture
->resource
.format
->id
== WINED3DFMT_P8_UINT
100 || texture
->resource
.format
->id
== WINED3DFMT_P8_UINT_A8_UNORM
)
102 for (i
= 0; i
< sub_count
; ++i
)
104 IWineD3DSurfaceImpl
*surface
= surface_from_resource(texture
->baseTexture
.sub_resources
[i
]);
106 if (palette9_changed(surface
))
108 TRACE("Reloading surface %p because the d3d8/9 palette was changed.\n", surface
);
109 /* TODO: This is not necessarily needed with hw palettized texture support. */
110 surface_load_location(surface
, SFLAG_INSYSMEM
, NULL
);
111 /* Make sure the texture is reloaded because of the palette change,
112 * this kills performance though :( */
113 surface_modify_location(surface
, SFLAG_INTEXTURE
, FALSE
);
118 /* If the texture is marked dirty or the srgb sampler setting has changed
119 * since the last load then reload the surfaces. */
122 for (i
= 0; i
< sub_count
; ++i
)
124 surface_load(surface_from_resource(texture
->baseTexture
.sub_resources
[i
]), srgb_mode
);
129 TRACE("Texture %p not dirty, nothing to do.\n" , texture
);
132 /* No longer dirty. */
133 gl_tex
->dirty
= FALSE
;
135 if (context
) context_release(context
);
138 /* Do not call while under the GL lock. */
139 static void cubetexture_unload(struct wined3d_resource
*resource
)
141 IWineD3DBaseTextureImpl
*texture
= basetexture_from_resource(resource
);
142 UINT sub_count
= texture
->baseTexture
.level_count
* texture
->baseTexture
.layer_count
;
145 TRACE("texture %p.\n", texture
);
147 for (i
= 0; i
< sub_count
; ++i
)
149 struct wined3d_resource
*sub_resource
= texture
->baseTexture
.sub_resources
[i
];
150 IWineD3DSurfaceImpl
*surface
= surface_from_resource(sub_resource
);
152 sub_resource
->resource_ops
->resource_unload(sub_resource
);
153 surface_set_texture_name(surface
, 0, TRUE
);
154 surface_set_texture_name(surface
, 0, FALSE
);
157 basetexture_unload(texture
);
160 static const struct wined3d_texture_ops cubetexture_ops
=
166 static const struct wined3d_resource_ops cubetexture_resource_ops
=
171 static void cubetexture_cleanup(IWineD3DCubeTextureImpl
*This
)
173 UINT sub_count
= This
->baseTexture
.level_count
* This
->baseTexture
.layer_count
;
176 TRACE("(%p) : Cleaning up.\n", This
);
178 for (i
= 0; i
< sub_count
; ++i
)
180 struct wined3d_resource
*sub_resource
= This
->baseTexture
.sub_resources
[i
];
184 IWineD3DSurfaceImpl
*surface
= surface_from_resource(sub_resource
);
186 /* Clean out the texture name we gave to the surface so that the
187 * surface doesn't try and release it. */
188 surface_set_texture_name(surface
, 0, TRUE
);
189 surface_set_texture_name(surface
, 0, FALSE
);
190 surface_set_texture_target(surface
, 0);
191 surface_set_container(surface
, WINED3D_CONTAINER_NONE
, NULL
);
192 IWineD3DSurface_Release((IWineD3DSurface
*)surface
);
195 basetexture_cleanup((IWineD3DBaseTextureImpl
*)This
);
198 static HRESULT WINAPI
IWineD3DCubeTextureImpl_QueryInterface(IWineD3DBaseTexture
*iface
, REFIID riid
, LPVOID
*ppobj
)
200 IWineD3DCubeTextureImpl
*This
= (IWineD3DCubeTextureImpl
*)iface
;
201 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
202 if (IsEqualGUID(riid
, &IID_IUnknown
)
203 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
204 || IsEqualGUID(riid
, &IID_IWineD3DResource
)
205 || IsEqualGUID(riid
, &IID_IWineD3DBaseTexture
))
207 IUnknown_AddRef(iface
);
212 return E_NOINTERFACE
;
215 static ULONG WINAPI
IWineD3DCubeTextureImpl_AddRef(IWineD3DBaseTexture
*iface
)
217 IWineD3DCubeTextureImpl
*This
= (IWineD3DCubeTextureImpl
*)iface
;
218 TRACE("(%p) : AddRef increasing from %d\n", This
, This
->resource
.ref
);
219 return InterlockedIncrement(&This
->resource
.ref
);
222 /* Do not call while under the GL lock. */
223 static ULONG WINAPI
IWineD3DCubeTextureImpl_Release(IWineD3DBaseTexture
*iface
)
225 IWineD3DCubeTextureImpl
*This
= (IWineD3DCubeTextureImpl
*)iface
;
227 TRACE("(%p) : Releasing from %d\n", This
, This
->resource
.ref
);
228 ref
= InterlockedDecrement(&This
->resource
.ref
);
231 cubetexture_cleanup(This
);
232 This
->resource
.parent_ops
->wined3d_object_destroyed(This
->resource
.parent
);
233 HeapFree(GetProcessHeap(), 0, This
);
238 static HRESULT WINAPI
IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DBaseTexture
*iface
,
239 REFGUID riid
, const void *data
, DWORD data_size
, DWORD flags
)
241 return resource_set_private_data(&((IWineD3DCubeTextureImpl
*)iface
)->resource
, riid
, data
, data_size
, flags
);
244 static HRESULT WINAPI
IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DBaseTexture
*iface
,
245 REFGUID guid
, void *data
, DWORD
*data_size
)
247 return resource_get_private_data(&((IWineD3DCubeTextureImpl
*)iface
)->resource
, guid
, data
, data_size
);
250 static HRESULT WINAPI
IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DBaseTexture
*iface
, REFGUID refguid
)
252 return resource_free_private_data(&((IWineD3DCubeTextureImpl
*)iface
)->resource
, refguid
);
255 static DWORD WINAPI
IWineD3DCubeTextureImpl_SetPriority(IWineD3DBaseTexture
*iface
, DWORD priority
)
257 return resource_set_priority(&((IWineD3DCubeTextureImpl
*)iface
)->resource
, priority
);
260 static DWORD WINAPI
IWineD3DCubeTextureImpl_GetPriority(IWineD3DBaseTexture
*iface
)
262 return resource_get_priority(&((IWineD3DCubeTextureImpl
*)iface
)->resource
);
265 /* Do not call while under the GL lock. */
266 static void WINAPI
IWineD3DCubeTextureImpl_PreLoad(IWineD3DBaseTexture
*iface
)
268 cubetexture_preload((IWineD3DBaseTextureImpl
*)iface
, SRGB_ANY
);
271 static WINED3DRESOURCETYPE WINAPI
IWineD3DCubeTextureImpl_GetType(IWineD3DBaseTexture
*iface
)
273 return resource_get_type(&((IWineD3DCubeTextureImpl
*)iface
)->resource
);
276 static void * WINAPI
IWineD3DCubeTextureImpl_GetParent(IWineD3DBaseTexture
*iface
)
278 TRACE("iface %p.\n", iface
);
280 return ((IWineD3DCubeTextureImpl
*)iface
)->resource
.parent
;
283 static DWORD WINAPI
IWineD3DCubeTextureImpl_SetLOD(IWineD3DBaseTexture
*iface
, DWORD LODNew
)
285 return basetexture_set_lod((IWineD3DBaseTextureImpl
*)iface
, LODNew
);
288 static DWORD WINAPI
IWineD3DCubeTextureImpl_GetLOD(IWineD3DBaseTexture
*iface
)
290 return basetexture_get_lod((IWineD3DBaseTextureImpl
*)iface
);
293 static DWORD WINAPI
IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DBaseTexture
*iface
)
295 return basetexture_get_level_count((IWineD3DBaseTextureImpl
*)iface
);
298 static HRESULT WINAPI
IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture
*iface
,
299 WINED3DTEXTUREFILTERTYPE FilterType
)
301 return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl
*)iface
, FilterType
);
304 static WINED3DTEXTUREFILTERTYPE WINAPI
IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture
*iface
)
306 return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl
*)iface
);
309 static void WINAPI
IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture
*iface
)
311 basetexture_generate_mipmaps((IWineD3DBaseTextureImpl
*)iface
);
314 static struct wined3d_resource
* WINAPI
IWineD3DCubeTextureImpl_GetSubResource(IWineD3DBaseTexture
*iface
,
315 UINT sub_resource_idx
)
317 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
319 TRACE("iface %p, sub_resource_idx %u.\n", iface
, sub_resource_idx
);
321 return basetexture_get_sub_resource(texture
, sub_resource_idx
);
324 static HRESULT WINAPI
IWineD3DCubeTextureImpl_AddDirtyRegion(IWineD3DBaseTexture
*iface
,
325 UINT layer
, const WINED3DBOX
*dirty_region
)
327 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
328 struct wined3d_resource
*sub_resource
;
330 TRACE("iface %p, layer %u, dirty_region %p.\n", iface
, layer
, dirty_region
);
332 if (!(sub_resource
= basetexture_get_sub_resource(texture
, layer
* texture
->baseTexture
.level_count
)))
334 WARN("Failed to get sub-resource.\n");
335 return WINED3DERR_INVALIDCALL
;
338 basetexture_set_dirty(texture
, TRUE
);
339 surface_add_dirty_rect(surface_from_resource(sub_resource
), dirty_region
);
344 static const IWineD3DBaseTextureVtbl IWineD3DCubeTexture_Vtbl
=
347 IWineD3DCubeTextureImpl_QueryInterface
,
348 IWineD3DCubeTextureImpl_AddRef
,
349 IWineD3DCubeTextureImpl_Release
,
350 /* IWineD3DResource */
351 IWineD3DCubeTextureImpl_GetParent
,
352 IWineD3DCubeTextureImpl_SetPrivateData
,
353 IWineD3DCubeTextureImpl_GetPrivateData
,
354 IWineD3DCubeTextureImpl_FreePrivateData
,
355 IWineD3DCubeTextureImpl_SetPriority
,
356 IWineD3DCubeTextureImpl_GetPriority
,
357 IWineD3DCubeTextureImpl_PreLoad
,
358 IWineD3DCubeTextureImpl_GetType
,
359 /* IWineD3DBaseTexture */
360 IWineD3DCubeTextureImpl_SetLOD
,
361 IWineD3DCubeTextureImpl_GetLOD
,
362 IWineD3DCubeTextureImpl_GetLevelCount
,
363 IWineD3DCubeTextureImpl_SetAutoGenFilterType
,
364 IWineD3DCubeTextureImpl_GetAutoGenFilterType
,
365 IWineD3DCubeTextureImpl_GenerateMipSubLevels
,
366 IWineD3DCubeTextureImpl_GetSubResource
,
367 IWineD3DCubeTextureImpl_AddDirtyRegion
,
370 HRESULT
cubetexture_init(IWineD3DCubeTextureImpl
*texture
, UINT edge_length
, UINT levels
,
371 IWineD3DDeviceImpl
*device
, DWORD usage
, enum wined3d_format_id format_id
, WINED3DPOOL pool
,
372 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
374 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
375 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
376 UINT pow2_edge_length
;
381 /* TODO: It should only be possible to create textures for formats
382 * that are reported as supported. */
383 if (WINED3DFMT_UNKNOWN
>= format_id
)
385 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
386 return WINED3DERR_INVALIDCALL
;
389 if (!gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
] && pool
!= WINED3DPOOL_SCRATCH
)
391 WARN("(%p) : Tried to create not supported cube texture.\n", texture
);
392 return WINED3DERR_INVALIDCALL
;
395 /* Calculate levels for mip mapping */
396 if (usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
398 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
400 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
401 return WINED3DERR_INVALIDCALL
;
406 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
407 return WINED3DERR_INVALIDCALL
;
414 levels
= wined3d_log2i(edge_length
) + 1;
415 TRACE("Calculated levels = %u.\n", levels
);
418 texture
->lpVtbl
= &IWineD3DCubeTexture_Vtbl
;
420 hr
= basetexture_init((IWineD3DBaseTextureImpl
*)texture
, &cubetexture_ops
,
421 6, levels
, WINED3DRTYPE_CUBETEXTURE
, device
, usage
, format
, pool
,
422 parent
, parent_ops
, &cubetexture_resource_ops
);
425 WARN("Failed to initialize basetexture, returning %#x\n", hr
);
429 /* Find the nearest pow2 match. */
430 pow2_edge_length
= 1;
431 while (pow2_edge_length
< edge_length
) pow2_edge_length
<<= 1;
433 if (gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
] || (edge_length
== pow2_edge_length
))
435 /* Precalculated scaling for 'faked' non power of two texture coords. */
436 texture
->baseTexture
.pow2Matrix
[0] = 1.0f
;
437 texture
->baseTexture
.pow2Matrix
[5] = 1.0f
;
438 texture
->baseTexture
.pow2Matrix
[10] = 1.0f
;
439 texture
->baseTexture
.pow2Matrix
[15] = 1.0f
;
443 /* Precalculated scaling for 'faked' non power of two texture coords. */
444 texture
->baseTexture
.pow2Matrix
[0] = ((float)edge_length
) / ((float)pow2_edge_length
);
445 texture
->baseTexture
.pow2Matrix
[5] = ((float)edge_length
) / ((float)pow2_edge_length
);
446 texture
->baseTexture
.pow2Matrix
[10] = ((float)edge_length
) / ((float)pow2_edge_length
);
447 texture
->baseTexture
.pow2Matrix
[15] = 1.0f
;
448 texture
->baseTexture
.pow2Matrix_identity
= FALSE
;
450 texture
->baseTexture
.target
= GL_TEXTURE_CUBE_MAP_ARB
;
452 /* Generate all the surfaces. */
454 for (i
= 0; i
< texture
->baseTexture
.level_count
; ++i
)
456 /* Create the 6 faces. */
457 for (j
= 0; j
< 6; ++j
)
459 static const GLenum cube_targets
[6] =
461 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
462 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
463 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
464 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
465 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
466 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
,
468 UINT idx
= j
* texture
->baseTexture
.level_count
+ i
;
469 IWineD3DSurface
*surface
;
471 hr
= IWineD3DDeviceParent_CreateSurface(device
->device_parent
, parent
, tmp_w
, tmp_w
,
472 format_id
, usage
, pool
, i
/* Level */, j
, &surface
);
475 FIXME("(%p) Failed to create surface, hr %#x.\n", texture
, hr
);
476 cubetexture_cleanup(texture
);
480 surface_set_container((IWineD3DSurfaceImpl
*)surface
, WINED3D_CONTAINER_TEXTURE
, (IWineD3DBase
*)texture
);
481 surface_set_texture_target((IWineD3DSurfaceImpl
*)surface
, cube_targets
[j
]);
482 texture
->baseTexture
.sub_resources
[idx
] = &((IWineD3DSurfaceImpl
*)surface
)->resource
;
483 TRACE("Created surface level %u @ %p.\n", i
, surface
);
485 tmp_w
= max(1, tmp_w
>> 1);