Commit b0ba731b authored by Henri Verbeet's avatar Henri Verbeet Committed by Alexandre Julliard

wined3d: Introduce cubetexture_init() to handle most of the cube texture initialization.

parent aabb5128
......@@ -25,7 +25,90 @@
#include "wined3d_private.h"
WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
#define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
#define GLINFO_LOCATION (*gl_info)
static void cubetexture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb)
{
/* Override the IWineD3DResource Preload method. */
IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
unsigned int i, j;
BOOL srgb_mode;
BOOL *dirty;
switch (srgb)
{
case SRGB_RGB:
srgb_mode = FALSE;
break;
case SRGB_BOTH:
cubetexture_internal_preload(iface, SRGB_RGB);
/* Fallthrough */
case SRGB_SRGB:
srgb_mode = TRUE;
break;
default:
srgb_mode = This->baseTexture.is_srgb;
break;
}
dirty = srgb_mode ? &This->baseTexture.srgbDirty : &This->baseTexture.dirty;
TRACE("(%p) : About to load texture: dirtified(%u).\n", This, *dirty);
/* We only have to activate a context for gl when we're not drawing.
* In most cases PreLoad will be called during draw and a context was
* activated at the beginning of drawPrimitive. */
if (!device->isInDraw)
{
/* No danger of recursive calls, ActivateContext sets isInDraw to true
* when loading offscreen render targets into their texture. */
ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
}
if (This->resource.format_desc->format == WINED3DFMT_P8
|| This->resource.format_desc->format == WINED3DFMT_A8P8)
{
for (i = 0; i < This->baseTexture.levels; ++i)
{
for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z; ++j)
{
if (palette9_changed((IWineD3DSurfaceImpl *)This->surfaces[j][i]))
{
TRACE("Reloading surface because the d3d8/9 palette was changed.\n");
/* TODO: This is not necessarily needed with hw palettized texture support. */
IWineD3DSurface_LoadLocation(This->surfaces[j][i], SFLAG_INSYSMEM, NULL);
/* Make sure the texture is reloaded because of the palette change,
* this kills performance though :( */
IWineD3DSurface_ModifyLocation(This->surfaces[j][i], SFLAG_INTEXTURE, FALSE);
}
}
}
}
/* If the texture is marked dirty or the srgb sampler setting has changed
* since the last load then reload the surfaces. */
if (*dirty)
{
for (i = 0; i < This->baseTexture.levels; ++i)
{
for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z; ++j)
{
IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
}
}
}
else
{
TRACE("(%p) Texture not dirty, nothing to do.\n" , iface);
}
/* No longer dirty. */
*dirty = FALSE;
}
static void cubetexture_cleanup(IWineD3DCubeTextureImpl *This, D3DCB_DESTROYSURFACEFN surface_destroy_cb)
{
......@@ -54,9 +137,140 @@ static void cubetexture_cleanup(IWineD3DCubeTextureImpl *This, D3DCB_DESTROYSURF
basetexture_cleanup((IWineD3DBaseTexture *)This);
}
HRESULT cubetexture_init(IWineD3DCubeTextureImpl *texture, UINT edge_length, UINT levels,
IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool, IUnknown *parent)
{
const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
const struct GlPixelFormatDesc *format_desc = getFormatDescEntry(format, gl_info);
UINT pow2_edge_length;
unsigned int i, j;
UINT tmp_w;
HRESULT hr;
/* TODO: It should only be possible to create textures for formats
* that are reported as supported. */
if (WINED3DFMT_UNKNOWN >= format)
{
WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
return WINED3DERR_INVALIDCALL;
}
if (!GL_SUPPORT(ARB_TEXTURE_CUBE_MAP) && pool != WINED3DPOOL_SCRATCH)
{
WARN("(%p) : Tried to create not supported cube texture.\n", texture);
return WINED3DERR_INVALIDCALL;
}
/* Calculate levels for mip mapping */
if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
{
if (!GL_SUPPORT(SGIS_GENERATE_MIPMAP))
{
WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
return WINED3DERR_INVALIDCALL;
}
if (levels > 1)
{
WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
return WINED3DERR_INVALIDCALL;
}
levels = 1;
}
else if (!levels)
{
levels = wined3d_log2i(edge_length) + 1;
TRACE("Calculated levels = %u.\n", levels);
}
hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, levels,
WINED3DRTYPE_CUBETEXTURE, device, 0, usage, format_desc, pool, parent);
if (FAILED(hr))
{
WARN("Failed to initialize basetexture, returning %#x\n", hr);
return hr;
}
if (texture->resource.format_desc->Flags & WINED3DFMT_FLAG_FILTERING)
{
texture->baseTexture.minMipLookup = minMipLookup;
texture->baseTexture.magLookup = magLookup;
}
else
{
texture->baseTexture.minMipLookup = minMipLookup_noFilter;
texture->baseTexture.magLookup = magLookup_noFilter;
}
/* Find the nearest pow2 match. */
pow2_edge_length = 1;
while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO) || (edge_length == pow2_edge_length))
{
/* Precalculated scaling for 'faked' non power of two texture coords. */
texture->baseTexture.pow2Matrix[0] = 1.0;
texture->baseTexture.pow2Matrix[5] = 1.0;
texture->baseTexture.pow2Matrix[10] = 1.0;
texture->baseTexture.pow2Matrix[15] = 1.0;
}
else
{
/* Precalculated scaling for 'faked' non power of two texture coords. */
texture->baseTexture.pow2Matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
texture->baseTexture.pow2Matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
texture->baseTexture.pow2Matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
texture->baseTexture.pow2Matrix[15] = 1.0;
texture->baseTexture.pow2Matrix_identity = FALSE;
}
/* Generate all the surfaces. */
tmp_w = edge_length;
for (i = 0; i < texture->baseTexture.levels; ++i)
{
/* Create the 6 faces. */
for (j = 0; j < 6; ++j)
{
static const GLenum cube_targets[6] =
{
GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
};
hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_w,
format, usage, pool, i /* Level */, j, &texture->surfaces[j][i]);
if (FAILED(hr))
{
FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
texture->surfaces[j][i] = NULL;
cubetexture_cleanup(texture, D3DCB_DefaultDestroySurface);
return hr;
}
IWineD3DSurface_SetContainer(texture->surfaces[j][i], (IWineD3DBase *)texture);
TRACE("Created surface level %u @ %p.\n", i, texture->surfaces[j][i]);
surface_set_texture_target(texture->surfaces[j][i], cube_targets[j]);
}
tmp_w = max(1, tmp_w >> 1);
}
texture->baseTexture.internal_preload = cubetexture_internal_preload;
return WINED3D_OK;
}
#undef GLINFO_LOCATION
/* *******************************************
IWineD3DCubeTexture IUnknown parts follow
******************************************* */
#define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
{
IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
......@@ -118,65 +332,6 @@ static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *ifa
return resource_get_priority((IWineD3DResource *)iface);
}
void cubetexture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb) {
/* Override the IWineD3DResource Preload method */
unsigned int i,j;
IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
BOOL srgb_mode;
BOOL *dirty;
switch(srgb) {
case SRGB_RGB: srgb_mode = FALSE; break;
case SRGB_BOTH: cubetexture_internal_preload(iface, SRGB_RGB);
case SRGB_SRGB: srgb_mode = TRUE; break;
/* DONTKNOW, and shut up the compiler */
default: srgb_mode = This->baseTexture.is_srgb; break;
}
dirty = srgb_mode ? &This->baseTexture.srgbDirty : &This->baseTexture.dirty;
TRACE("(%p) : About to load texture: dirtified(%d)\n", This, *dirty);
/* We only have to activate a context for gl when we're not drawing. In most cases PreLoad will be called during draw
* and a context was activated at the beginning of drawPrimitive
*/
if(!device->isInDraw) {
/* No danger of recursive calls, ActivateContext sets isInDraw to true when loading
* offscreen render targets into their texture
*/
ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
}
if (This->resource.format_desc->format == WINED3DFMT_P8 || This->resource.format_desc->format == WINED3DFMT_A8P8)
{
for (i = 0; i < This->baseTexture.levels; i++) {
for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
if(palette9_changed((IWineD3DSurfaceImpl *)This->surfaces[j][i])) {
TRACE("Reloading surface because the d3d8/9 palette was changed\n");
/* TODO: This is not necessarily needed with hw palettized texture support */
IWineD3DSurface_LoadLocation(This->surfaces[j][i], SFLAG_INSYSMEM, NULL);
/* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
IWineD3DSurface_ModifyLocation(This->surfaces[j][i], SFLAG_INTEXTURE, FALSE);
}
}
}
}
/* If the texture is marked dirty or the srgb sampler setting has changed since the last load then reload the surfaces */
if (*dirty) {
for (i = 0; i < This->baseTexture.levels; i++) {
for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
}
}
} else {
TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
}
/* No longer dirty */
*dirty = FALSE;
return;
}
static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface) {
cubetexture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
}
......
......@@ -1295,47 +1295,8 @@ static HRESULT WINAPI IWineD3DDeviceImpl_CreateCubeTexture(IWineD3DDevice *iface
WINED3DPOOL Pool, IWineD3DCubeTexture **ppCubeTexture, IUnknown *parent)
{
IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
const struct GlPixelFormatDesc *format_desc = getFormatDescEntry(Format, &This->adapter->gl_info);
IWineD3DCubeTextureImpl *object; /** NOTE: impl ref allowed since this is a create function **/
unsigned int i, j;
UINT tmpW;
HRESULT hr;
unsigned int pow2EdgeLength;
/* TODO: It should only be possible to create textures for formats
that are reported as supported */
if (WINED3DFMT_UNKNOWN >= Format) {
WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN\n", This);
return WINED3DERR_INVALIDCALL;
}
if (!GL_SUPPORT(ARB_TEXTURE_CUBE_MAP) && Pool != WINED3DPOOL_SCRATCH) {
WARN("(%p) : Tried to create not supported cube texture\n", This);
return WINED3DERR_INVALIDCALL;
}
/* Calculate levels for mip mapping */
if (Usage & WINED3DUSAGE_AUTOGENMIPMAP)
{
if (!GL_SUPPORT(SGIS_GENERATE_MIPMAP))
{
WARN("No mipmap generation support, returning D3DERR_INVALIDCALL\n");
return WINED3DERR_INVALIDCALL;
}
if (Levels > 1)
{
WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL\n");
return WINED3DERR_INVALIDCALL;
}
Levels = 1;
}
else if (!Levels)
{
Levels = wined3d_log2i(EdgeLength) + 1;
TRACE("Calculated levels = %d\n", Levels);
}
object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
if (!object)
......@@ -1346,82 +1307,18 @@ static HRESULT WINAPI IWineD3DDeviceImpl_CreateCubeTexture(IWineD3DDevice *iface
}
object->lpVtbl = &IWineD3DCubeTexture_Vtbl;
hr = basetexture_init((IWineD3DBaseTextureImpl *)object, Levels,
WINED3DRTYPE_CUBETEXTURE, This, 0, Usage, format_desc, Pool, parent);
hr = cubetexture_init(object, EdgeLength, Levels, This, Usage, Format, Pool, parent);
if (FAILED(hr))
{
WARN("Failed to initialize basetexture, returning %#x\n", hr);
WARN("Failed to initialize cubetexture, returning %#x\n", hr);
HeapFree(GetProcessHeap(), 0, object);
*ppCubeTexture = NULL;
return hr;
}
TRACE("(%p) : Created basetexture %p\n", This, object);
TRACE("(%p) Create Cube Texture\n", This);
/* Find the nearest pow2 match */
pow2EdgeLength = 1;
while (pow2EdgeLength < EdgeLength) pow2EdgeLength <<= 1;
if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO) || (EdgeLength == pow2EdgeLength)) {
/* Precalculated scaling for 'faked' non power of two texture coords */
object->baseTexture.pow2Matrix[ 0] = 1.0;
object->baseTexture.pow2Matrix[ 5] = 1.0;
object->baseTexture.pow2Matrix[10] = 1.0;
object->baseTexture.pow2Matrix[15] = 1.0;
} else {
/* Precalculated scaling for 'faked' non power of two texture coords */
object->baseTexture.pow2Matrix[ 0] = ((float)EdgeLength) / ((float)pow2EdgeLength);
object->baseTexture.pow2Matrix[ 5] = ((float)EdgeLength) / ((float)pow2EdgeLength);
object->baseTexture.pow2Matrix[10] = ((float)EdgeLength) / ((float)pow2EdgeLength);
object->baseTexture.pow2Matrix[15] = 1.0;
object->baseTexture.pow2Matrix_identity = FALSE;
}
if (object->resource.format_desc->Flags & WINED3DFMT_FLAG_FILTERING)
{
object->baseTexture.minMipLookup = minMipLookup;
object->baseTexture.magLookup = magLookup;
} else {
object->baseTexture.minMipLookup = minMipLookup_noFilter;
object->baseTexture.magLookup = magLookup_noFilter;
}
/* Generate all the surfaces */
tmpW = EdgeLength;
for (i = 0; i < object->baseTexture.levels; i++) {
/* Create the 6 faces */
for (j = 0; j < 6; j++) {
static const GLenum cube_targets[6] = {
GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
};
hr = IWineD3DDeviceParent_CreateSurface(This->device_parent, parent, tmpW, tmpW,
Format, Usage, Pool, i /* Level */, j, &object->surfaces[j][i]);
if (FAILED(hr))
{
FIXME("(%p) Failed to create surface\n",object);
IWineD3DCubeTexture_Release((IWineD3DCubeTexture *)object);
*ppCubeTexture = NULL;
return hr;
}
IWineD3DSurface_SetContainer(object->surfaces[j][i], (IWineD3DBase *)object);
TRACE("Created surface level %d @ %p,\n", i, object->surfaces[j][i]);
surface_set_texture_target(object->surfaces[j][i], cube_targets[j]);
}
tmpW = max(1, tmpW >> 1);
}
object->baseTexture.internal_preload = cubetexture_internal_preload;
TRACE("(%p) : Created Cube Texture %p\n", This, object);
*ppCubeTexture = (IWineD3DCubeTexture *) object;
*ppCubeTexture = (IWineD3DCubeTexture *)object;
return WINED3D_OK;
}
......
......@@ -1712,7 +1712,6 @@ typedef struct IWineD3DBaseTextureClass
void (*internal_preload)(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb);
} IWineD3DBaseTextureClass;
void cubetexture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb);
void volumetexture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb);
void surface_internal_preload(IWineD3DSurface *iface, enum WINED3DSRGB srgb);
......@@ -1785,6 +1784,9 @@ typedef struct IWineD3DCubeTextureImpl
extern const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl;
HRESULT cubetexture_init(IWineD3DCubeTextureImpl *texture, UINT edge_length, UINT levels,
IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool, IUnknown *parent);
typedef struct _WINED3DVOLUMET_DESC
{
UINT Width;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment