[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[reactos.git] / dll / directx / wine / d3dx9_36 / texture.c
index 95c1e5e..d33b463 100644 (file)
@@ -265,7 +265,7 @@ HRESULT WINAPI D3DXCheckTextureRequirements(struct IDirect3DDevice9 *device, UIN
         unsigned int channels;
         const struct pixel_format_desc *curfmt, *bestfmt = NULL;
 
-        TRACE("Requested format not supported, looking for a fallback.\n");
+        TRACE("Requested format is not supported, looking for a fallback.\n");
 
         if (!fmt)
         {
@@ -298,12 +298,13 @@ HRESULT WINAPI D3DXCheckTextureRequirements(struct IDirect3DDevice9 *device, UIN
 
             /* This format can be used, let's evaluate it.
                Weights chosen quite arbitrarily... */
-            score = 16 - 4 * (curchannels - channels);
+            score = 512 * (curfmt->type == fmt->type);
+            score -= 32 * (curchannels - channels);
 
             for (j = 0; j < 4; j++)
             {
                 int diff = curfmt->bits[j] - fmt->bits[j];
-                score += 16 - (diff < 0 ? -diff * 4 : diff);
+                score -= (diff < 0 ? -diff * 8 : diff) * (j == 0 ? 1 : 2);
             }
 
             if (score > bestscore)
@@ -329,10 +330,10 @@ HRESULT WINAPI D3DXCheckTextureRequirements(struct IDirect3DDevice9 *device, UIN
 
     if (fmt->block_width != 1 || fmt->block_height != 1)
     {
-        if (w < fmt->block_width)
-            w = fmt->block_width;
-        if (h < fmt->block_height)
-            h = fmt->block_height;
+        if (w % fmt->block_width)
+            w += fmt->block_width - w % fmt->block_width;
+        if (h % fmt->block_height)
+            h += fmt->block_height - h % fmt->block_height;
     }
 
     if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(w)))
@@ -534,6 +535,29 @@ HRESULT WINAPI D3DXCreateTexture(struct IDirect3DDevice9 *device, UINT width, UI
     return IDirect3DDevice9_CreateTexture(device, width, height, miplevels, usage, format, pool, texture, NULL);
 }
 
+static D3DFORMAT get_alpha_replacement_format(D3DFORMAT format)
+{
+    static const struct
+    {
+        D3DFORMAT orig_format;
+        D3DFORMAT replacement_format;
+    }
+    replacement_formats[] =
+    {
+        {D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8},
+        {D3DFMT_X1R5G5B5, D3DFMT_A1R5G5B5},
+        {D3DFMT_X4R4G4B4, D3DFMT_A4R4G4B4},
+        {D3DFMT_X8B8G8R8, D3DFMT_A8B8G8R8},
+        {D3DFMT_L8, D3DFMT_A8L8},
+    };
+    unsigned int i;
+
+    for (i = 0; i < sizeof(replacement_formats) / sizeof(replacement_formats[0]); ++i)
+        if (replacement_formats[i].orig_format == format)
+            return replacement_formats[i].replacement_format;
+    return format;
+}
+
 HRESULT WINAPI D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9 *device, const void *srcdata,
         UINT srcdatasize, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
         D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
@@ -542,7 +566,7 @@ HRESULT WINAPI D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9 *devi
     IDirect3DTexture9 **texptr;
     IDirect3DTexture9 *buftex;
     IDirect3DSurface9 *surface;
-    BOOL dynamic_texture;
+    BOOL dynamic_texture, format_specified = FALSE;
     D3DXIMAGE_INFO imginfo;
     UINT loaded_miplevels, skip_levels;
     D3DCAPS9 caps;
@@ -581,6 +605,8 @@ HRESULT WINAPI D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9 *devi
 
     if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
         format = imginfo.Format;
+    else
+        format_specified = TRUE;
 
     if (width == D3DX_FROM_FILE)
     {
@@ -626,9 +652,12 @@ HRESULT WINAPI D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9 *devi
         return hr;
     }
 
+    if (colorkey && !format_specified)
+        format = get_alpha_replacement_format(format);
+
     if (imginfo.MipLevels < miplevels && (D3DFMT_DXT1 <= imginfo.Format && imginfo.Format <= D3DFMT_DXT5))
     {
-        FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet\n");
+        FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet.\n");
         miplevels = imginfo.MipLevels;
     }
     if (imginfo.ResourceType == D3DRTYPE_VOLUMETEXTURE
@@ -645,7 +674,7 @@ HRESULT WINAPI D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9 *devi
     dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
     if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
     {
-        hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
+        hr = D3DXCreateTexture(device, width, height, miplevels, 0, format, D3DPOOL_SYSTEMMEM, &buftex);
         texptr = &buftex;
     }
     else
@@ -1186,7 +1215,7 @@ HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9 *devic
 
     if (mip_levels > image_info.MipLevels)
     {
-        FIXME("Generation of mipmaps for volume textures is not implemented yet\n");
+        FIXME("Generation of mipmaps for volume textures is not implemented yet.\n");
         mip_levels = image_info.MipLevels;
     }
 
@@ -1344,6 +1373,12 @@ HRESULT WINAPI D3DXFillTexture(struct IDirect3DTexture9 *texture, LPD3DXFILL2D f
     return D3D_OK;
 }
 
+HRESULT WINAPI D3DXFillTextureTX(struct IDirect3DTexture9 *texture, ID3DXTextureShader *texture_shader)
+{
+    FIXME("texture %p, texture_shader %p stub.\n", texture, texture_shader);
+    return E_NOTIMPL;
+}
+
 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9 *device,
                                                        const void *src_data,
                                                        UINT src_data_size,
@@ -1427,7 +1462,7 @@ HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9 *device,
 
     if (mip_levels > img_info.MipLevels && (D3DFMT_DXT1 <= img_info.Format && img_info.Format <= D3DFMT_DXT5))
     {
-        FIXME("Generation of mipmaps for compressed pixel formats not supported yet\n");
+        FIXME("Generation of mipmaps for compressed pixel formats is not supported yet.\n");
         mip_levels = img_info.MipLevels;
     }
 
@@ -1713,6 +1748,12 @@ HRESULT WINAPI D3DXFillCubeTexture(struct IDirect3DCubeTexture9 *texture, LPD3DX
     return D3D_OK;
 }
 
+HRESULT WINAPI D3DXFillCubeTextureTX(struct IDirect3DCubeTexture9 *texture, ID3DXTextureShader *texture_shader)
+{
+    FIXME("texture %p, texture_shader %p stub.\n", texture, texture_shader);
+    return E_NOTIMPL;
+}
+
 HRESULT WINAPI D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9 *texture, LPD3DXFILL3D function, void *funcdata)
 {
     DWORD miplevels;
@@ -1777,6 +1818,12 @@ HRESULT WINAPI D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9 *texture, LP
     return D3D_OK;
 }
 
+HRESULT WINAPI D3DXFillVolumeTextureTX(struct IDirect3DVolumeTexture9 *texture, ID3DXTextureShader *texture_shader)
+{
+    FIXME("texture %p, texture_shader %p stub.\n", texture, texture_shader);
+    return E_NOTIMPL;
+}
+
 HRESULT WINAPI D3DXSaveTextureToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
         IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
 {
@@ -1840,10 +1887,7 @@ HRESULT WINAPI D3DXSaveTextureToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE
     if (!dst_buffer || !src_texture) return D3DERR_INVALIDCALL;
 
     if (file_format == D3DXIFF_DDS)
-    {
-        FIXME("DDS file format isn't supported yet\n");
-        return E_NOTIMPL;
-    }
+        return save_dds_texture_to_memory(dst_buffer, src_texture, src_palette);
 
     type = IDirect3DBaseTexture9_GetType(src_texture);
     switch (type)