Ver Fonte

Added functions to query most often used texture formats, so that application code does not need to include RendererImpl.h.

Lasse Öörni há 14 anos atrás
pai
commit
977dd11341

+ 36 - 6
Engine/Graphics/Graphics.cpp

@@ -2024,6 +2024,36 @@ void Graphics::RemoveGPUObject(GPUObject* object)
     }
     }
 }
 }
 
 
+unsigned Graphics::GetAlphaFormat()
+{
+    return D3DFMT_A8;
+}
+
+unsigned Graphics::GetLuminanceFormat()
+{
+    return D3DFMT_L8;
+}
+
+unsigned Graphics::GetLuminanceAlphaFormat()
+{
+    return D3DFMT_A8L8;
+}
+
+unsigned Graphics::GetRGBFormat()
+{
+    return D3DFMT_X8R8G8B8;
+}
+
+unsigned Graphics::GetRGBAFormat()
+{
+    return D3DFMT_A8R8G8B8;
+}
+
+unsigned Graphics::GetDepthFormat()
+{
+    return D3DFMT_R32F;
+}
+
 bool Graphics::OpenWindow(int width, int height)
 bool Graphics::OpenWindow(int width, int height)
 {
 {
     WNDCLASS wc;
     WNDCLASS wc;
@@ -2139,8 +2169,8 @@ bool Graphics::CreateInterface()
         else
         else
         {
         {
             // No depth texture shadow map support -> no shadows at all
             // No depth texture shadow map support -> no shadows at all
-            shadowMapFormat_ = D3DFMT_UNKNOWN;
-            hiresShadowMapFormat_ = D3DFMT_UNKNOWN;
+            shadowMapFormat_ = 0;
+            hiresShadowMapFormat_ = 0;
         }
         }
     }
     }
     
     
@@ -2210,19 +2240,19 @@ void Graphics::CreateRenderTargets()
         if (!diffBuffer_)
         if (!diffBuffer_)
         {
         {
             diffBuffer_ = new Texture2D(context_);
             diffBuffer_ = new Texture2D(context_);
-            diffBuffer_->SetSize(0, 0, D3DFMT_A8R8G8B8, TEXTURE_RENDERTARGET);
+            diffBuffer_->SetSize(0, 0, GetRGBAFormat(), TEXTURE_RENDERTARGET);
         }
         }
         
         
         if (!normalBuffer_)
         if (!normalBuffer_)
         {
         {
             normalBuffer_ = new Texture2D(context_);
             normalBuffer_ = new Texture2D(context_);
-            normalBuffer_->SetSize(0, 0, D3DFMT_A8R8G8B8, TEXTURE_RENDERTARGET);
+            normalBuffer_->SetSize(0, 0, GetRGBAFormat(), TEXTURE_RENDERTARGET);
         }
         }
         
         
         if (!depthBuffer_)
         if (!depthBuffer_)
         {
         {
             depthBuffer_ = new Texture2D(context_);
             depthBuffer_ = new Texture2D(context_);
-            depthBuffer_->SetSize(0, 0, D3DFMT_R32F, TEXTURE_RENDERTARGET);
+            depthBuffer_->SetSize(0, 0, GetDepthFormat(), TEXTURE_RENDERTARGET);
         }
         }
         
         
         // If deferred mode temporal AA is used, reserve screen buffers
         // If deferred mode temporal AA is used, reserve screen buffers
@@ -2233,7 +2263,7 @@ void Graphics::CreateRenderTargets()
             for (unsigned i = 0; i < NUM_SCREEN_BUFFERS; ++i)
             for (unsigned i = 0; i < NUM_SCREEN_BUFFERS; ++i)
             {
             {
                 screenBuffer_[i] = new Texture2D(context_);
                 screenBuffer_[i] = new Texture2D(context_);
-                screenBuffer_[i]->SetSize(0, 0, D3DFMT_A8R8G8B8, TEXTURE_RENDERTARGET);
+                screenBuffer_[i]->SetSize(0, 0, GetRGBAFormat(), TEXTURE_RENDERTARGET);
             }
             }
         }
         }
         else
         else

+ 15 - 2
Engine/Graphics/Graphics.h

@@ -247,9 +247,9 @@ public:
     unsigned GetNumBatches() const { return numBatches_; }
     unsigned GetNumBatches() const { return numBatches_; }
     /// Return dummy color texture format for shadow maps. Is "NULL" (consume no video memory) if supported
     /// Return dummy color texture format for shadow maps. Is "NULL" (consume no video memory) if supported
     unsigned GetDummyColorFormat() const { return dummyColorFormat_; }
     unsigned GetDummyColorFormat() const { return dummyColorFormat_; }
-    /// Return shadow map depth texture format, or D3DFMT_UNKNOWN if not supported
+    /// Return shadow map depth texture format, or 0 if not supported
     unsigned GetShadowMapFormat() const { return shadowMapFormat_; }
     unsigned GetShadowMapFormat() const { return shadowMapFormat_; }
-    /// Return 24-bit shadow map depth texture format, or D3DFMT_UNKNOWN if not supported
+    /// Return 24-bit shadow map depth texture format, or 0 if not supported
     unsigned GetHiresShadowMapFormat() const { return hiresShadowMapFormat_; }
     unsigned GetHiresShadowMapFormat() const { return hiresShadowMapFormat_; }
     /// Return whether deferred rendering is supported
     /// Return whether deferred rendering is supported
     bool GetDeferredSupport() const { return deferredSupport_; }
     bool GetDeferredSupport() const { return deferredSupport_; }
@@ -363,6 +363,19 @@ public:
     /// Remove a GPU object. Called by GPUObject
     /// Remove a GPU object. Called by GPUObject
     void RemoveGPUObject(GPUObject* object);
     void RemoveGPUObject(GPUObject* object);
     
     
+    /// Return the API-specific alpha texture format
+    static unsigned GetAlphaFormat();
+    /// Return the API-specific luminance texture format
+    static unsigned GetLuminanceFormat();
+    /// Return the API-specific luminance alpha texture format
+    static unsigned GetLuminanceAlphaFormat();
+    /// Return the API-specific RGB texture format
+    static unsigned GetRGBFormat();
+    /// Return the API-specific RGBA texture format
+    static unsigned GetRGBAFormat();
+    /// Return the API-specific deferred rendering depth texture format
+    static unsigned GetDepthFormat();
+    
 private:
 private:
     /// Create the application window
     /// Create the application window
     bool OpenWindow(int width, int height);
     bool OpenWindow(int width, int height);

+ 1 - 1
Engine/Graphics/IndexBuffer.cpp

@@ -156,7 +156,7 @@ void* IndexBuffer::Lock(unsigned start, unsigned count, LockMode mode)
         return 0;
         return 0;
     }
     }
     
     
-    void* hwData;
+    void* hwData = 0;
     
     
     if (object_)
     if (object_)
     {
     {

+ 1 - 1
Engine/Graphics/Renderer.cpp

@@ -1257,7 +1257,7 @@ bool Renderer::CreateShadowMaps()
     unsigned dummyColorFormat = graphics_->GetDummyColorFormat();
     unsigned dummyColorFormat = graphics_->GetDummyColorFormat();
     bool hardwarePCF = graphics_->GetHardwareShadowSupport();
     bool hardwarePCF = graphics_->GetHardwareShadowSupport();
     
     
-    if (shadowMapFormat == D3DFMT_UNKNOWN)
+    if (!shadowMapFormat)
         return false;
         return false;
     
     
     if (!drawShadows_)
     if (!drawShadows_)

+ 2 - 2
Engine/Graphics/Texture.cpp

@@ -123,7 +123,7 @@ TextureUsage Texture::GetUsage() const
     return TEXTURE_STATIC;
     return TEXTURE_STATIC;
 }
 }
 
 
-unsigned Texture::GetCompressedD3DFormat(CompressedFormat format)
+unsigned Texture::GetDXTFormat(CompressedFormat format)
 {
 {
     switch (format)
     switch (format)
     {
     {
@@ -137,7 +137,7 @@ unsigned Texture::GetCompressedD3DFormat(CompressedFormat format)
         return D3DFMT_DXT5;
         return D3DFMT_DXT5;
     }
     }
     
     
-    return D3DFMT_UNKNOWN;
+    return 0;
 }
 }
 
 
 void Texture::LoadParameters()
 void Texture::LoadParameters()

+ 3 - 3
Engine/Graphics/Texture.h

@@ -38,7 +38,7 @@ struct LockedRect
 {
 {
     /// Texture data, format-specific
     /// Texture data, format-specific
     unsigned char* bits_;
     unsigned char* bits_;
-    /// Byte offset between this row and the next
+    /// Byte offset between rows
     unsigned pitch_;
     unsigned pitch_;
 };
 };
 
 
@@ -84,8 +84,8 @@ public:
     const Color& GetBorderColor() const { return borderColor_; }
     const Color& GetBorderColor() const { return borderColor_; }
     /// Return backup texture
     /// Return backup texture
     Texture* GetBackupTexture() const { return backupTexture_; }
     Texture* GetBackupTexture() const { return backupTexture_; }
-    /// Return compressed format
-    static unsigned GetCompressedD3DFormat(CompressedFormat format);
+    /// Return API-specific DXT compressed texture format
+    static unsigned GetDXTFormat(CompressedFormat format);
     
     
 protected:
 protected:
     /// Load parameters
     /// Load parameters

+ 7 - 7
Engine/Graphics/Texture2D.cpp

@@ -178,7 +178,7 @@ bool Texture2D::Load(SharedPtr<Image> image)
         int levelWidth = image->GetWidth();
         int levelWidth = image->GetWidth();
         int levelHeight = image->GetHeight();
         int levelHeight = image->GetHeight();
         unsigned components = image->GetComponents();
         unsigned components = image->GetComponents();
-        D3DFORMAT format = D3DFMT_UNKNOWN;
+        unsigned format = 0;
         
         
         // Discard unnecessary mip levels
         // Discard unnecessary mip levels
         for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
         for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
@@ -192,19 +192,19 @@ bool Texture2D::Load(SharedPtr<Image> image)
         switch (components)
         switch (components)
         {
         {
             case 1:
             case 1:
-            format = D3DFMT_L8;
+            format = Graphics::GetLuminanceFormat();
             break;
             break;
             
             
             case 2:
             case 2:
-            format = D3DFMT_A8L8;
+            format = Graphics::GetLuminanceAlphaFormat();
             break;
             break;
             
             
             case 3:
             case 3:
-            format = D3DFMT_X8R8G8B8;
+            format = Graphics::GetRGBFormat();
             break;
             break;
             
             
             case 4:
             case 4:
-            format = D3DFMT_A8R8G8B8;
+            format = Graphics::GetRGBAFormat();
             break;
             break;
         }
         }
         
         
@@ -266,7 +266,7 @@ bool Texture2D::Load(SharedPtr<Image> image)
         int width = image->GetWidth();
         int width = image->GetWidth();
         int height = image->GetHeight();
         int height = image->GetHeight();
         unsigned levels = image->GetNumCompressedLevels();
         unsigned levels = image->GetNumCompressedLevels();
-        D3DFORMAT format = (D3DFORMAT)GetCompressedD3DFormat(image->GetCompressedFormat());
+        unsigned format = GetDXTFormat(image->GetCompressedFormat());
         
         
         unsigned mipsToSkip = mipsToSkip_[quality];
         unsigned mipsToSkip = mipsToSkip_[quality];
         if (mipsToSkip >= levels)
         if (mipsToSkip >= levels)
@@ -303,7 +303,7 @@ bool Texture2D::Load(SharedPtr<Image> image)
     return true;
     return true;
 }
 }
 
 
-bool Texture2D::Lock(unsigned level, IntRect* rect, LockedRect& lockedRect)
+bool Texture2D::Lock(unsigned level, const IntRect* rect, LockedRect& lockedRect)
 {
 {
     if (!object_)
     if (!object_)
     {
     {

+ 2 - 2
Engine/Graphics/Texture2D.h

@@ -55,8 +55,8 @@ public:
     bool SetSize(int width, int height, unsigned format, TextureUsage usage = TEXTURE_STATIC);
     bool SetSize(int width, int height, unsigned format, TextureUsage usage = TEXTURE_STATIC);
     /// Load from an image. Return true if successful
     /// Load from an image. Return true if successful
     bool Load(SharedPtr<Image> image);
     bool Load(SharedPtr<Image> image);
-    /// Lock a rectangular area from a mipmap level. A null rectangle locks the entire texture. Return true if successful
-    bool Lock(unsigned level, IntRect* rect, LockedRect& lockedRect);
+    /// Lock a rectangular area from a mipmap level. A null rect locks the entire level. Return true if successful
+    bool Lock(unsigned level, const IntRect* rect, LockedRect& lockedRect);
     /// Unlock texture
     /// Unlock texture
     void Unlock();
     void Unlock();
     
     

+ 7 - 7
Engine/Graphics/TextureCube.cpp

@@ -227,7 +227,7 @@ bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image)
         int levelWidth = image->GetWidth();
         int levelWidth = image->GetWidth();
         int levelHeight = image->GetHeight();
         int levelHeight = image->GetHeight();
         unsigned components = image->GetComponents();
         unsigned components = image->GetComponents();
-        D3DFORMAT format = D3DFMT_UNKNOWN;
+        unsigned format = 0;
         
         
         if (levelWidth != levelHeight)
         if (levelWidth != levelHeight)
         {
         {
@@ -246,19 +246,19 @@ bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image)
         switch (components)
         switch (components)
         {
         {
             case 1:
             case 1:
-            format = D3DFMT_L8;
+            format = Graphics::GetLuminanceFormat();
             break;
             break;
             
             
             case 2:
             case 2:
-            format = D3DFMT_A8L8;
+            format = Graphics::GetLuminanceAlphaFormat();
             break;
             break;
             
             
             case 3:
             case 3:
-            format = D3DFMT_X8R8G8B8;
+            format = Graphics::GetRGBFormat();
             break;
             break;
             
             
             case 4:
             case 4:
-            format = D3DFMT_A8R8G8B8;
+            format = Graphics::GetRGBAFormat();
             break;
             break;
         }
         }
         
         
@@ -336,7 +336,7 @@ bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image)
         int width = image->GetWidth();
         int width = image->GetWidth();
         int height = image->GetHeight();
         int height = image->GetHeight();
         unsigned levels = image->GetNumCompressedLevels();
         unsigned levels = image->GetNumCompressedLevels();
-        D3DFORMAT format = (D3DFORMAT)GetCompressedD3DFormat(image->GetCompressedFormat());
+        unsigned format = GetDXTFormat(image->GetCompressedFormat());
         
         
         if (width != height)
         if (width != height)
         {
         {
@@ -400,7 +400,7 @@ bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image)
     return true;
     return true;
 }
 }
 
 
-bool TextureCube::Lock(CubeMapFace face, unsigned level, IntRect* rect, LockedRect& lockedRect)
+bool TextureCube::Lock(CubeMapFace face, unsigned level, const IntRect* rect, LockedRect& lockedRect)
 {
 {
     if (!object_)
     if (!object_)
     {
     {

+ 2 - 2
Engine/Graphics/TextureCube.h

@@ -58,8 +58,8 @@ public:
     bool Load(CubeMapFace face, Deserializer& source);
     bool Load(CubeMapFace face, Deserializer& source);
     /// Load one face from an image. Return true if successful
     /// Load one face from an image. Return true if successful
     bool Load(CubeMapFace face, SharedPtr<Image> image);
     bool Load(CubeMapFace face, SharedPtr<Image> image);
-    /// Lock a rectangular area from one face and mipmap level. A null rectangle locks the entire face. Return true if successful
-    bool Lock(CubeMapFace face, unsigned level, IntRect* rect, LockedRect& lockedRect);
+    /// Lock a rectangular area from one face and mipmap level. A null rect locks the entire face. Return true if successful
+    bool Lock(CubeMapFace face, unsigned level, const IntRect* rect, LockedRect& lockedRect);
     /// Unlock texture
     /// Unlock texture
     void Unlock();
     void Unlock();
     
     

+ 1 - 2
Engine/UI/Font.cpp

@@ -27,7 +27,6 @@
 #include "Deserializer.h"
 #include "Deserializer.h"
 #include "Font.h"
 #include "Font.h"
 #include "Graphics.h"
 #include "Graphics.h"
-#include "GraphicsImpl.h"
 #include "Log.h"
 #include "Log.h"
 #include "Profiler.h"
 #include "Profiler.h"
 #include "Texture2D.h"
 #include "Texture2D.h"
@@ -276,7 +275,7 @@ const FontFace* Font::GetFace(int pointSize)
     texture->SetAddressMode(COORD_U, ADDRESS_BORDER);
     texture->SetAddressMode(COORD_U, ADDRESS_BORDER);
     texture->SetAddressMode(COORD_V, ADDRESS_BORDER),
     texture->SetAddressMode(COORD_V, ADDRESS_BORDER),
     texture->SetBorderColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
     texture->SetBorderColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
-    if (!texture->SetSize(texWidth, texHeight, D3DFMT_A8))
+    if (!texture->SetSize(texWidth, texHeight, Graphics::GetAlphaFormat()))
     {
     {
         FT_Done_Face(face);
         FT_Done_Face(face);
         return 0;
         return 0;

+ 3 - 2
Engine/UI/UI.cpp

@@ -31,7 +31,6 @@
 #include "Font.h"
 #include "Font.h"
 #include "Graphics.h"
 #include "Graphics.h"
 #include "GraphicsEvents.h"
 #include "GraphicsEvents.h"
-#include "GraphicsImpl.h"
 #include "InputEvents.h"
 #include "InputEvents.h"
 #include "LineEdit.h"
 #include "LineEdit.h"
 #include "ListView.h"
 #include "ListView.h"
@@ -258,6 +257,8 @@ void UI::Render()
     ShaderProgram* ps = 0;
     ShaderProgram* ps = 0;
     ShaderProgram* vs = 0;
     ShaderProgram* vs = 0;
     
     
+    unsigned alphaFormat = Graphics::GetAlphaFormat();
+    
     for (unsigned i = 0; i < batches_.Size(); ++i)
     for (unsigned i = 0; i < batches_.Size(); ++i)
     {
     {
         // Choose shaders here so that UIBatch does not need to look up shaders each time
         // Choose shaders here so that UIBatch does not need to look up shaders each time
@@ -271,7 +272,7 @@ void UI::Render()
             // If texture contains only an alpha channel, use the alpha pixel shader
             // If texture contains only an alpha channel, use the alpha pixel shader
             vs = diffTextureVS_;
             vs = diffTextureVS_;
             
             
-            if (batches_[i].texture_->GetFormat() == D3DFMT_A8)
+            if (batches_[i].texture_->GetFormat() == alphaFormat)
                 ps = alphaTexturePS_;
                 ps = alphaTexturePS_;
             else
             else
                 ps = diffTexturePS_;
                 ps = diffTexturePS_;