Browse Source

Added LockedRect structure for textures so that application code does not need to use the corresponding Direct3D structures directly.
Fixed comments.

Lasse Öörni 14 years ago
parent
commit
fa3fb7b2fc

+ 15 - 31
Engine/Graphics/Graphics.cpp

@@ -671,7 +671,7 @@ void Graphics::Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCou
     }
     
     numPrimitives_ += primitiveCount;
-    numBatches_++;
+    ++numBatches_;
 }
 
 void Graphics::Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount)
@@ -697,7 +697,7 @@ void Graphics::Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount
     }
     
     numPrimitives_ += primitiveCount;
-    numBatches_++;
+    ++numBatches_;
 }
 
 void Graphics::DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount,
@@ -734,7 +734,7 @@ void Graphics::DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned i
     }
     
     numPrimitives_ += instanceCount * primitiveCount;
-    numBatches_++;
+    ++numBatches_;
 }
 
 void Graphics::SetVertexBuffer(VertexBuffer* buffer)
@@ -963,15 +963,12 @@ void Graphics::SetVertexShaderParameter(ShaderParameter param, float value)
     if (index >= MAX_CONSTANT_REGISTERS)
         return;
     
-    float data[4] =
-    {
-        0.0f,
-        0.0f,
-        0.0f,
-        0.0f
-    };
+    float data[4];
     
     data[0] = value;
+    data[1] = 0.0f;
+    data[2] = 0.0f;
+    data[3] = 0.0f;
     
     impl_->device_->SetVertexShaderConstantF(index, &data[0], 1);
 }
@@ -1015,17 +1012,12 @@ void Graphics::SetVertexShaderParameter(ShaderParameter param, const Vector3& ve
     if (index >= MAX_CONSTANT_REGISTERS)
         return;
     
-    float data[4] =
-    {
-        0.0f,
-        0.0f,
-        0.0f,
-        0.0f
-    };
+    float data[4];
     
     data[0] = vector.x_;
     data[1] = vector.y_;
     data[2] = vector.z_;
+    data[3] = 0.0f;
     
     impl_->device_->SetVertexShaderConstantF(index, &data[0], 1);
 }
@@ -1090,15 +1082,12 @@ void Graphics::SetPixelShaderParameter(ShaderParameter param, float value)
     if (index >= MAX_CONSTANT_REGISTERS)
         return;
     
-    float data[4] =
-    {
-        0.0f,
-        0.0f,
-        0.0f,
-        0.0f
-    };
+    float data[4];
     
     data[0] = value;
+    data[1] = 0.0f;
+    data[2] = 0.0f;
+    data[3] = 0.0f;
     
     impl_->device_->SetPixelShaderConstantF(index, &data[0], 1);
 }
@@ -1142,17 +1131,12 @@ void Graphics::SetPixelShaderParameter(ShaderParameter param, const Vector3& vec
     if (index >= MAX_CONSTANT_REGISTERS)
         return;
     
-    float data[4] =
-    {
-        0.0f,
-        0.0f,
-        0.0f,
-        0.0f
-    };
+    float data[4];
     
     data[0] = vector.x_;
     data[1] = vector.y_;
     data[2] = vector.z_;
+    data[3] = 0.0f;
     
     impl_->device_->SetPixelShaderConstantF(index, &data[0], 1);
 }

+ 2 - 2
Engine/Graphics/IndexBuffer.h

@@ -34,7 +34,7 @@ class IndexBuffer : public Object, public GPUObject
     OBJECT(IndexBuffer);
     
 public:
-    /// Construct with graphics subsystem pointer and whether is dynamic or not
+    /// Construct
     IndexBuffer(Context* context);
     /// Destruct
     virtual ~IndexBuffer();
@@ -76,7 +76,7 @@ private:
     /// Create buffer
     bool Create();
     
-    /// Fallback data when operating with a null graphics
+    /// Fallback data when operating with a null graphics subsystem
     SharedArrayPtr<unsigned char> fallbackData_;
     /// Memory pool
     unsigned pool_;

+ 9 - 0
Engine/Graphics/Texture.h

@@ -33,6 +33,15 @@ static const int MAX_TEXTURE_QUALITY_LEVELS = 3;
 
 class XMLFile;
 
+/// Locked texture rectangle structure
+struct LockedRect
+{
+    /// Texture data, format-specific
+    unsigned char* bits_;
+    /// Byte offset between this row and the next
+    unsigned pitch_;
+};
+
 /// Base class for texture resources
 class Texture : public Resource, public GPUObject
 {

+ 20 - 15
Engine/Graphics/Texture2D.cpp

@@ -214,15 +214,15 @@ bool Texture2D::Load(SharedPtr<Image> image)
         
         for (unsigned i = 0; i < manualLevels; ++i)
         {
-            D3DLOCKED_RECT hwRect;
-            if (!Lock(i, 0, &hwRect))
+            LockedRect hwRect;
+            if (!Lock(i, 0, hwRect))
                 return false;
             
             memoryUse += levelWidth * levelHeight * components;
             
             for (int y = 0; y < levelHeight; ++y)
             {
-                unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * y;
+                unsigned char* dest = hwRect.bits_ + hwRect.pitch_ * y;
                 unsigned char* src = levelData + components * levelWidth * y;
                 
                 switch (components)
@@ -284,13 +284,13 @@ bool Texture2D::Load(SharedPtr<Image> image)
             CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
             memoryUse += level.rows_ * level.rowSize_;
             
-            D3DLOCKED_RECT hwRect;
-            if (!Lock(i, 0, &hwRect))
+            LockedRect hwRect;
+            if (!Lock(i, 0, hwRect))
                 return false;
             
             for (unsigned j = 0; j < level.rows_; ++j)
             {
-                unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * j;
+                unsigned char* dest = hwRect.bits_ + hwRect.pitch_ * j;
                 unsigned char* src = level.data_ + level.rowSize_ * j;
                 memcpy(dest, src, level.rowSize_);
             }
@@ -303,15 +303,8 @@ bool Texture2D::Load(SharedPtr<Image> image)
     return true;
 }
 
-bool Texture2D::Lock(unsigned level, void* rect, void* hwRect)
+bool Texture2D::Lock(unsigned level, IntRect* rect, LockedRect& lockedRect)
 {
-    D3DLOCKED_RECT* lockedRect = (D3DLOCKED_RECT*)hwRect;
-    if (!lockedRect)
-    {
-        LOGERROR("Null locked rect structure");
-        return false;
-    }
-    
     if (!object_)
     {
         LOGERROR("No texture created, can not lock");
@@ -324,16 +317,28 @@ bool Texture2D::Lock(unsigned level, void* rect, void* hwRect)
         return false;
     }
     
+    D3DLOCKED_RECT d3dLockedRect;
+    RECT d3dRect;
+    if (rect)
+    {
+        d3dRect.left = rect->left_;
+        d3dRect.top = rect->top_;
+        d3dRect.right = rect->right_;
+        d3dRect.bottom = rect->bottom_;
+    }
+    
     DWORD flags = 0;
     if ((!rect) && (pool_ == D3DPOOL_DEFAULT))
         flags |= D3DLOCK_DISCARD;
     
-    if (FAILED(((IDirect3DTexture9*)object_)->LockRect(level, lockedRect, (RECT*)rect, flags)))
+    if (FAILED(((IDirect3DTexture9*)object_)->LockRect(level, &d3dLockedRect, rect ? &d3dRect : 0, flags)))
     {
         LOGERROR("Could not lock texture");
         return false;
     }
     
+    lockedRect.bits_ = (unsigned char*)d3dLockedRect.pBits;
+    lockedRect.pitch_ = d3dLockedRect.Pitch;
     lockedLevel_ = level;
     return true;
 }

+ 2 - 2
Engine/Graphics/Texture2D.h

@@ -55,8 +55,8 @@ public:
     bool SetSize(int width, int height, unsigned format, TextureUsage usage = TEXTURE_STATIC);
     /// Load from an image. Return true if successful
     bool Load(SharedPtr<Image> image);
-    /// Lock a rectangular area from a mipmap level. Return true if successful
-    bool Lock(unsigned level, void* rect, void* lockedRect);
+    /// 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);
     /// Unlock texture
     void Unlock();
     

+ 20 - 14
Engine/Graphics/TextureCube.cpp

@@ -284,15 +284,15 @@ bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image)
         
         for (unsigned i = 0; i < manualLevels; ++i)
         {
-            D3DLOCKED_RECT hwRect;
-            if (!Lock(face, i, 0, &hwRect))
+            LockedRect hwRect;
+            if (!Lock(face, i, 0, hwRect))
                 return false;
             
             memoryUse += levelWidth * levelHeight * components;
             
             for (int y = 0; y < levelHeight; ++y)
             {
-                unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * y;
+                unsigned char* dest = hwRect.bits_ + hwRect.pitch_ * y;
                 unsigned char* src = levelData + components * levelWidth * y;
                 
                 switch (components)
@@ -377,13 +377,13 @@ bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image)
             CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
             memoryUse += level.rows_ * level.rowSize_;
             
-            D3DLOCKED_RECT hwRect;
-            if (!Lock(face, i, 0, &hwRect))
+            LockedRect hwRect;
+            if (!Lock(face, i, 0, hwRect))
                 return false;
             
             for (unsigned j = 0; j < level.rows_; ++j)
             {
-                unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * j;
+                unsigned char* dest = hwRect.bits_ + hwRect.pitch_ * j;
                 unsigned char* src = level.data_ + level.rowSize_ * j;
                 memcpy(dest, src, level.rowSize_);
             }
@@ -400,14 +400,8 @@ bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image)
     return true;
 }
 
-bool TextureCube::Lock(CubeMapFace face, unsigned level, void* rect, void* hwRect)
+bool TextureCube::Lock(CubeMapFace face, unsigned level, IntRect* rect, LockedRect& lockedRect)
 {
-    D3DLOCKED_RECT* lockedRect = (D3DLOCKED_RECT*)hwRect;
-    if (!lockedRect)
-    {
-        LOGERROR("Null locked rect structure");
-        return false;
-    }
     if (!object_)
     {
         LOGERROR("No texture created, can not lock");
@@ -419,16 +413,28 @@ bool TextureCube::Lock(CubeMapFace face, unsigned level, void* rect, void* hwRec
         return false;
     }
     
+    D3DLOCKED_RECT d3dLockedRect;
+    RECT d3dRect;
+    if (rect)
+    {
+        d3dRect.left = rect->left_;
+        d3dRect.top = rect->top_;
+        d3dRect.right = rect->right_;
+        d3dRect.bottom = rect->bottom_;
+    }
+    
     DWORD flags = 0;
     if ((!rect) && (pool_ == D3DPOOL_DEFAULT))
         flags |= D3DLOCK_DISCARD;
     
-    if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, lockedRect, (RECT*)rect, flags)))
+    if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, rect ? &d3dRect : 0, flags)))
     {
         LOGERROR("Could not lock texture");
         return false;
     }
     
+    lockedRect.bits_ = (unsigned char*)d3dLockedRect.pBits;
+    lockedRect.pitch_ = d3dLockedRect.Pitch;
     lockedLevel_ = level;
     lockedFace_ = face;
     return true;

+ 2 - 2
Engine/Graphics/TextureCube.h

@@ -58,8 +58,8 @@ public:
     bool Load(CubeMapFace face, Deserializer& source);
     /// Load one face from an image. Return true if successful
     bool Load(CubeMapFace face, SharedPtr<Image> image);
-    /// Lock a rectangular area from one face and mipmap level. Return true if successful
-    bool Lock(CubeMapFace face, unsigned level, void* rect, void* lockedRect);
+    /// 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);
     /// Unlock texture
     void Unlock();
     

+ 1 - 1
Engine/Graphics/VertexBuffer.h

@@ -103,7 +103,7 @@ private:
     /// Create buffer
     bool Create();
     
-    /// Fallback data when operating with a null graphics
+    /// Fallback data when operating with a null graphics subsystem
     SharedArrayPtr<unsigned char> fallbackData_;
     /// Morph vertex range reset data
     SharedArrayPtr<unsigned char> morphRangeResetData_;

+ 5 - 5
Engine/UI/Font.cpp

@@ -282,8 +282,8 @@ const FontFace* Font::GetFace(int pointSize)
         return 0;
     }
     
-    D3DLOCKED_RECT hwRect;
-    if (!texture->Lock(0, 0, &hwRect))
+    LockedRect hwRect;
+    if (!texture->Lock(0, 0, hwRect))
     {
         FT_Done_Face(face);
         return 0;
@@ -292,7 +292,7 @@ const FontFace* Font::GetFace(int pointSize)
     // First clear the whole texture
     for (int y = 0; y < texHeight; ++y)
     {
-        unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * y;
+        unsigned char* dest = hwRect.bits_ + hwRect.pitch_ * y;
         memset(dest, 0, texWidth);
     }
     
@@ -309,7 +309,7 @@ const FontFace* Font::GetFace(int pointSize)
         for (int y = 0; y < newFace.glyphs_[i].height_; ++y)
         {
             unsigned char* src = slot->bitmap.buffer + slot->bitmap.pitch * y;
-            unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * (y + newFace.glyphs_[i].y_) + newFace.glyphs_[i].x_;
+            unsigned char* dest = hwRect.bits_ + hwRect.pitch_ * (y + newFace.glyphs_[i].y_) + newFace.glyphs_[i].x_;
             
             for (int x = 0; x < newFace.glyphs_[i].width_; ++x)
             {
@@ -336,7 +336,7 @@ const FontFace* Font::GetFace(int pointSize)
         {
             for (int y = 0; y < newFace.glyphs_[i].height_; ++y)
             {
-                unsigned char* dest = (unsigned char*)hwRect.pBits + hwRect.Pitch * (y + newFace.glyphs_[i].y_) + newFace.glyphs_[i].x_;
+                unsigned char* dest = hwRect.bits_ + hwRect.pitch_ * (y + newFace.glyphs_[i].y_) + newFace.glyphs_[i].x_;
                 for (int x = 0; x < newFace.glyphs_[i].width_; ++x)
                 {
                     int pixel = dest[x];