Next try. This time I reverted most of my header changes and just needed to add some...
[reactos.git] / reactos / dll / directx / wine / wined3d / cubetexture.c
index ce5f6b6..4b6900a 100644 (file)
 #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, NULL, 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)
+{
+    unsigned int i, j;
+
+    TRACE("(%p) : Cleaning up.\n", This);
+
+    for (i = 0; i < This->baseTexture.levels; ++i)
+    {
+        for (j = 0; j < 6; ++j)
+        {
+            IWineD3DSurface *surface = This->surfaces[j][i];
+
+            if (surface)
+            {
+                /* Clean out the texture name we gave to the surface so that the
+                 * surface doesn't try and release it. */
+                surface_set_texture_name(surface, 0, TRUE);
+                surface_set_texture_name(surface, 0, FALSE);
+                surface_set_texture_target(surface, 0);
+                IWineD3DSurface_SetContainer(surface, NULL);
+                surface_destroy_cb(surface);
+            }
+        }
+    }
+    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 struct 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;
+    }
+
+    /* 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.0f;
+        texture->baseTexture.pow2Matrix[5] = 1.0f;
+        texture->baseTexture.pow2Matrix[10] = 1.0f;
+        texture->baseTexture.pow2Matrix[15] = 1.0f;
+    }
+    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.0f;
+        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;
@@ -91,65 +321,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);
 }
@@ -219,6 +390,7 @@ static BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface)
     return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
 }
 
+/* Context activation is done by the caller. */
 static HRESULT WINAPI IWineD3DCubeTextureImpl_BindTexture(IWineD3DCubeTexture *iface, BOOL srgb) {
     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
     BOOL set_gl_texture_desc;
@@ -257,36 +429,13 @@ static BOOL WINAPI IWineD3DCubeTextureImpl_IsCondNP2(IWineD3DCubeTexture *iface)
     return FALSE;
 }
 
-static void WINAPI IWineD3DCubeTextureImpl_ApplyStateChanges(IWineD3DCubeTexture *iface, 
-                                                        const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1], 
-                                                        const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
-    TRACE("(%p) : relay to BaseTexture\n", iface);
-    basetexture_apply_state_changes((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
-}
-
-
 /* *******************************************
    IWineD3DCubeTexture IWineD3DCubeTexture parts follow
    ******************************************* */
 static void WINAPI IWineD3DCubeTextureImpl_Destroy(IWineD3DCubeTexture *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroySurface) {
     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
-    unsigned int i,j;
-    TRACE("(%p) : Cleaning up\n",This);
-    for (i = 0; i < This->baseTexture.levels; i++) {
-        for (j = 0; j < 6; j++) {
-            if (This->surfaces[j][i] != NULL) {
-                IWineD3DSurface *surface = This->surfaces[j][i];
-                /* Clean out the texture name we gave to the surface so that the surface doesn't try and release it */
-                surface_set_texture_name(surface, 0, TRUE);
-                surface_set_texture_name(surface, 0, FALSE);
-                surface_set_texture_target(surface, 0);
-                /* Cleanup the container */
-                IWineD3DSurface_SetContainer(This->surfaces[j][i], 0);
-                D3DCB_DestroySurface(This->surfaces[j][i]);
-            }
-        }
-    }
-    basetexture_cleanup((IWineD3DBaseTexture *)iface);
+
+    cubetexture_cleanup(This, D3DCB_DestroySurface);
     /* finally delete the object */
     HeapFree(GetProcessHeap(), 0, This);
 }
@@ -399,7 +548,6 @@ const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
     IWineD3DCubeTextureImpl_BindTexture,
     IWineD3DCubeTextureImpl_GetTextureDimensions,
     IWineD3DCubeTextureImpl_IsCondNP2,
-    IWineD3DCubeTextureImpl_ApplyStateChanges,
     /* IWineD3DCubeTexture */
     IWineD3DCubeTextureImpl_Destroy,
     IWineD3DCubeTextureImpl_GetLevelDesc,