diff options
author | chai <chaifix@163.com> | 2021-10-27 23:37:24 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-27 23:37:24 +0800 |
commit | 305ca0a09d4e750186b5190432de47f3493e806a (patch) | |
tree | d82f9ef73191abc2acbcbfdca4b184a28e6c381b | |
parent | 51ced5a191078ce4ef08d57e343e91db007f556f (diff) |
*GfxDevice
-rw-r--r-- | Data/Resources/Metatable/Excel/Icons.xlsx | bin | 12404 -> 12425 bytes | |||
-rw-r--r-- | Data/Resources/Metatable/Icons.csv | 8 | ||||
-rw-r--r-- | Projects/VisualStudio/Editor/Editor.vcxproj | 8 | ||||
-rw-r--r-- | Projects/VisualStudio/Editor/Editor.vcxproj.filters | 20 | ||||
-rw-r--r-- | Runtime/Graphics/Color.h | 25 | ||||
-rw-r--r-- | Runtime/Graphics/Device.cpp | 83 | ||||
-rw-r--r-- | Runtime/Graphics/Device.h | 83 | ||||
-rw-r--r-- | Runtime/Graphics/DeviceDefine.h | 46 | ||||
-rw-r--r-- | Runtime/Graphics/GPUDataBuffer.cpp | 206 | ||||
-rw-r--r-- | Runtime/Graphics/GPUDataBuffer.h | 75 | ||||
-rw-r--r-- | Runtime/Graphics/GPUDataBuffers.cpp | 42 | ||||
-rw-r--r-- | Runtime/Graphics/GPUDataBuffers.h | 160 | ||||
-rw-r--r-- | Runtime/Graphics/GfxDevice.cpp | 88 | ||||
-rw-r--r-- | Runtime/Graphics/GfxDevice.h | 76 | ||||
-rw-r--r-- | Runtime/Graphics/PolyLine.cpp | 4 | ||||
-rw-r--r-- | Runtime/Graphics/Quad.cpp | 104 | ||||
-rw-r--r-- | Runtime/Graphics/Quad.h | 50 | ||||
-rw-r--r-- | Runtime/Graphics/Texture.cpp | 1 | ||||
-rw-r--r-- | Runtime/Graphics/VertexBuffer.cpp | 67 | ||||
-rw-r--r-- | Runtime/Graphics/VertexBuffer.h | 57 |
20 files changed, 645 insertions, 558 deletions
diff --git a/Data/Resources/Metatable/Excel/Icons.xlsx b/Data/Resources/Metatable/Excel/Icons.xlsx Binary files differindex 5f21f2d..7259175 100644 --- a/Data/Resources/Metatable/Excel/Icons.xlsx +++ b/Data/Resources/Metatable/Excel/Icons.xlsx diff --git a/Data/Resources/Metatable/Icons.csv b/Data/Resources/Metatable/Icons.csv index 85ea0fd..a4af4b4 100644 --- a/Data/Resources/Metatable/Icons.csv +++ b/Data/Resources/Metatable/Icons.csv @@ -1,7 +1,7 @@ -Name,Category,Path,Filter,Wrap,Slicing,Left,Right,Top,Bottom, -btn_add,Button,buttons/add.png,,,,,,,, -btn_alpha,Button,buttons/alpha.png,Nearest,,,,,,, -btn_checker,Button,buttons/checker.png,,Repeat,,,,,, +Name,Path,Category,Filter,Wrap,Slicing,Left,Right,Top,Bottom, +btn_add,buttons/add.png,Button,,,,,,,, +btn_alpha,buttons/alpha.png,Button,Nearest,,,,,,, +btn_checker,buttons/checker.png,Button,,Repeat,,,,,, ,,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,, diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj b/Projects/VisualStudio/Editor/Editor.vcxproj index 23f2a61..de5e835 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj +++ b/Projects/VisualStudio/Editor/Editor.vcxproj @@ -177,10 +177,10 @@ <ClCompile Include="..\..\..\Runtime\FileSystem\ImageJobs.cpp" />
<ClCompile Include="..\..\..\Runtime\FileSystem\Path.cpp" />
<ClCompile Include="..\..\..\Runtime\FileSystem\Unzip.cpp" />
- <ClCompile Include="..\..\..\Runtime\Graphics\Device.cpp" />
+ <ClCompile Include="..\..\..\Runtime\Graphics\GfxDevice.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\FrameBuffer.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\GlyphAtlas.cpp" />
- <ClCompile Include="..\..\..\Runtime\Graphics\GPUDataBuffers.cpp" />
+ <ClCompile Include="..\..\..\Runtime\Graphics\GPUDataBuffer.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\ImageData.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\OpenGL.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\Point.cpp" />
@@ -249,11 +249,11 @@ <ClInclude Include="..\..\..\Runtime\FileSystem\Path.h" />
<ClInclude Include="..\..\..\Runtime\FileSystem\Unzip.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\Color.h" />
- <ClInclude Include="..\..\..\Runtime\Graphics\Device.h" />
+ <ClInclude Include="..\..\..\Runtime\Graphics\GfxDevice.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\DeviceDefine.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\FrameBuffer.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\GlyphAtlas.h" />
- <ClInclude Include="..\..\..\Runtime\Graphics\GPUDataBuffers.h" />
+ <ClInclude Include="..\..\..\Runtime\Graphics\GPUDataBuffer.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\ImageData.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\OpenGL.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\Point.h" />
diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj.filters b/Projects/VisualStudio/Editor/Editor.vcxproj.filters index 2ec2b77..9bda8cd 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj.filters +++ b/Projects/VisualStudio/Editor/Editor.vcxproj.filters @@ -252,9 +252,6 @@ <ClCompile Include="..\..\..\Runtime\Lua\LuaBind\LuaBindHelper.cpp">
<Filter>Runtime\Lua\LuaBind</Filter>
</ClCompile>
- <ClCompile Include="..\..\..\Runtime\Graphics\Device.cpp">
- <Filter>Runtime\Graphics</Filter>
- </ClCompile>
<ClCompile Include="..\..\..\Runtime\Graphics\FrameBuffer.cpp">
<Filter>Runtime\Graphics</Filter>
</ClCompile>
@@ -351,10 +348,13 @@ <ClCompile Include="..\..\..\Editor\Utils\EditorUtils.cpp">
<Filter>Editor\Utils</Filter>
</ClCompile>
- <ClCompile Include="..\..\..\Runtime\Graphics\GPUDataBuffers.cpp">
+ <ClCompile Include="..\..\..\Runtime\Graphics\ShaderCompiler.cpp">
+ <Filter>Runtime\Graphics</Filter>
+ </ClCompile>
+ <ClCompile Include="..\..\..\Runtime\Graphics\GfxDevice.cpp">
<Filter>Runtime\Graphics</Filter>
</ClCompile>
- <ClCompile Include="..\..\..\Runtime\Graphics\ShaderCompiler.cpp">
+ <ClCompile Include="..\..\..\Runtime\Graphics\GPUDataBuffer.cpp">
<Filter>Runtime\Graphics</Filter>
</ClCompile>
</ItemGroup>
@@ -515,9 +515,6 @@ <ClInclude Include="..\..\..\Runtime\Graphics\Color.h">
<Filter>Runtime\Graphics</Filter>
</ClInclude>
- <ClInclude Include="..\..\..\Runtime\Graphics\Device.h">
- <Filter>Runtime\Graphics</Filter>
- </ClInclude>
<ClInclude Include="..\..\..\Runtime\Graphics\DeviceDefine.h">
<Filter>Runtime\Graphics</Filter>
</ClInclude>
@@ -587,10 +584,13 @@ <ClInclude Include="..\..\..\Editor\Utils\EditorUtils.h">
<Filter>Editor\Utils</Filter>
</ClInclude>
- <ClInclude Include="..\..\..\Runtime\Graphics\GPUDataBuffers.h">
+ <ClInclude Include="..\..\..\Runtime\Graphics\ShaderCompiler.h">
+ <Filter>Runtime\Graphics</Filter>
+ </ClInclude>
+ <ClInclude Include="..\..\..\Runtime\Graphics\GfxDevice.h">
<Filter>Runtime\Graphics</Filter>
</ClInclude>
- <ClInclude Include="..\..\..\Runtime\Graphics\ShaderCompiler.h">
+ <ClInclude Include="..\..\..\Runtime\Graphics\GPUDataBuffer.h">
<Filter>Runtime\Graphics</Filter>
</ClInclude>
</ItemGroup>
diff --git a/Runtime/Graphics/Color.h b/Runtime/Graphics/Color.h index 461e7af..3cff7a5 100644 --- a/Runtime/Graphics/Color.h +++ b/Runtime/Graphics/Color.h @@ -3,20 +3,31 @@ #include "../Utilities/Type.h" -class ColorRGBAf +class Color { public: - float r, g, b, a; - + Color(float r = 0, float g = 0, float b = 0, float a = 0) + { + this->r = r; + this->g = g; + this->b = b; + this->a = a; + } + float r, g, b, a; }; -class ColorRGBA32 +class Color32 { public: - uint8 r, g, b, a; + Color32(int r = 0, int g = 0, int b = 0, int a = 0) + { + this->r = r; + this->g = g; + this->b = b; + this->a = a; + } + int r, g, b, a; }; -using Color = ColorRGBAf; - #endif
\ No newline at end of file diff --git a/Runtime/Graphics/Device.cpp b/Runtime/Graphics/Device.cpp deleted file mode 100644 index 9ba2e23..0000000 --- a/Runtime/Graphics/Device.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "Device.h" - -bool deviceInited = false; -Device g_Device; - -Device::Device() -{ - //Assert(deviceInited); - deviceInited = true; -} - -Device::~Device() -{ - -} - -void Device::Initialize(DeviceSetting& setting) -{ - //GPU::BufferPool::Instance()->Initialize(); -} - -void Device::Enable(DeviceEnable enabled) -{ - -} - -void Device::Disable(DeviceEnable enabled) -{ - -} - -void Device::IsEnable(uint flag) -{ - -} - -void Device::SetDepthTest(DepthTest testing) -{ - -} - -void Device::SetCullFace(CullFace face) -{ - -} - -void Device::SetStencilMask(byte stencilMask) -{ - -} - -void Device::SetStencilOp(StencilOp op) -{ - -} - -void Device::SetAntiAliasing(int level /*= 0*/) -{ - -} - -void Device::Clear(int clearFlag) -{ - -} - -void Device::BeginFrame() -{ - m_IsInsideFrame = true; - -} - -void Device::EndFrame() -{ - //GPU::BufferPool::Instance()->OnEndFrame(); - - m_IsInsideFrame = false; -} - -void Device::PresentFrame() -{ -// swap buffers -}
\ No newline at end of file diff --git a/Runtime/Graphics/Device.h b/Runtime/Graphics/Device.h deleted file mode 100644 index 56cb197..0000000 --- a/Runtime/Graphics/Device.h +++ /dev/null @@ -1,83 +0,0 @@ -#ifndef DEVICE_H -#define DEVICE_H - -#include "../Utilities/NonCopyable.h" -#include "../Utilities/Type.h" -#include "../Utilities/Assert.h" - -#include "Texture.h" -#include "Color.h" -#include "DeviceDefine.h" -#include "VertexBuffer.h" - -struct DeviceSetting -{ - ETextureFilterMode texFilter; - ETextureWrapMode texWrap; -}; - -class Device : public NonCopyable -{ -public: - Device(); - ~Device(); - - void Initialize(DeviceSetting& setting); - - void Enable(DeviceEnable enabled); - void Disable(DeviceEnable enabled); - void IsEnable(uint flag); - - void SetDepthTest(DepthTest testing); - void SetCullFace(CullFace face); - void SetStencilMask(byte stencilMask); - void SetStencilOp(StencilOp op); - - void SetAntiAliasing(int level = 0); - - void Clear(int clearFlag); - - void BeginFrame(); - void EndFrame(); - void PresentFrame(); - - //GET(SharedVertexBuffer*, SharedVBO, m_SharedVBO); - //SharedVertexBuffer* GetSharedVBO() { return &m_SharedVBO; } - - GET_SET(Color, ClearColor, m_ClearColor); - - GET_SET(ETextureFilterMode, TextureFilter, m_TexFilter); - GET_SET(ETextureWrapMode, TextureWrap, m_TexWrap); - - inline bool IsInsideFrame(); - -private: - uint m_EnableFlag; - - // Global texture setting - ETextureFilterMode m_TexFilter; - ETextureWrapMode m_TexWrap; - - Color m_ClearColor; - - DepthTest m_DepthTest; - StencilTest m_StencilTest; - StencilOp m_StencilOp; - byte m_StencilMask; - - //SharedVertexBuffer m_SharedVBO; - - bool m_IsInsideFrame; - -}; - -extern Device g_Device; - -#define g_SharedVBO (*g_Device.GetSharedVBO()) - -inline bool Device::IsInsideFrame() -{ - return m_IsInsideFrame; -} - -#endif
\ No newline at end of file diff --git a/Runtime/Graphics/DeviceDefine.h b/Runtime/Graphics/DeviceDefine.h index 2f3cb6b..db1c138 100644 --- a/Runtime/Graphics/DeviceDefine.h +++ b/Runtime/Graphics/DeviceDefine.h @@ -1,18 +1,18 @@ #ifndef DEVICE_DEFINE_H #define DEVICE_DEFINE_H -enum DeviceEnable +enum EDeviceEnable { - DeviceEnable_DepthTest = 1, - DeviceEnable_DepthWrite = 1 << 1, - DeviceEnable_StencilTest = 1 << 2, - DeviceEnable_StencilWrite = 1 << 3, - DeviceEnable_Cull = 1 << 4, - DeviceEnable_Blend = 1 << 5, - DeviceEnable_AntiAliasing = 1 << 6, + Enable_DepthTest = 1, + Enable_DepthWrite = 1 << 1, + Enable_StencilTest = 1 << 2, + Enable_StencilWrite = 1 << 3, + Enable_Cull = 1 << 4, + Enable_Blend = 1 << 5, + Enable_AntiAliasing = 1 << 6, }; -enum DepthTest +enum EDepthTest { DepthTest_Greater = 1, DepthTest_GreaterEqual, @@ -23,7 +23,7 @@ enum DepthTest DepthTest_Always, }; -enum StencilTest { +enum EStencilTest { StencilTest_Always, StencilTest_Never, StencilTest_Less, @@ -34,7 +34,7 @@ enum StencilTest { StencilTest_GreaterEqual, }; -enum StencilOp { +enum EStencilOp { StencilOp_Keep, StencilOp_Zero, StencilOp_Replace, @@ -45,25 +45,25 @@ enum StencilOp { StencilOp_Invert, }; -enum DeviceClear +enum EDeviceClear { - DeviceClear_Depth = 1, - DeviceClear_Stencil = 1 << 1, - DeviceClear_Color = 1 << 2, + Clear_DepthBuffer = 1, + Clear_StencilBuffer = 1 << 1, + Clear_ColorBuffer = 1 << 2, }; -enum CullFace +enum ECullFace { - CullFace_Front = 1, - CullFace_Back = 2, - CullFace_None = 3, - CullFace_All = 4, + Cull_Front = 1, + Cull_Back = 2, + Cull_None = 3, + Cull_All = 4, }; -enum BlendMode +enum EBlendMode { - BlendMode_Additive = 1, - BlendMode_Substract = 1, + Blend_Additive = 1, + Blend_Substract = 1, }; #endif
\ No newline at end of file diff --git a/Runtime/Graphics/GPUDataBuffer.cpp b/Runtime/Graphics/GPUDataBuffer.cpp new file mode 100644 index 0000000..707cacb --- /dev/null +++ b/Runtime/Graphics/GPUDataBuffer.cpp @@ -0,0 +1,206 @@ +#include <math.h> + +#include "GPUDataBuffer.h" + +namespace GPU +{ + + // 修改buffer数据要绑定到这个目标上,其他情况下用GetHandle()绑定到其他目标 + // GL_COPY_READ_BUFFER + static const GLenum kBufferTarget = GL_COPY_WRITE_BUFFER; + + DataBuffer::DataBuffer() + { + glGenBuffers(1, &m_Handle); + } + + DataBuffer::~DataBuffer() + { + glDeleteBuffers(1, &m_Handle); + } + + void DataBuffer::Restore(int size, GLenum usage) + { + RestoreWithData(size, usage, 0); + } + + void DataBuffer::RestoreWithData(int size, GLenum usage, const void* data) + { + glBindBuffer(kBufferTarget, m_Handle); + glBufferData(kBufferTarget, size, data, usage); + glBindBuffer(kBufferTarget, 0); + m_Size = size; + m_Usage = usage; + } + + // glBufferSubData + // glMapBuffer + // glMapBufferRange (best one) + + void DataBuffer::Upload(int offset, int size, const void* data) + { + glBindBuffer(kBufferTarget, m_Handle); + glBufferSubData(kBufferTarget, offset, size, data); + glBindBuffer(kBufferTarget, 0); + } + + void* DataBuffer::Map(uint32 access) + { + glBindBuffer(kBufferTarget, m_Handle); + void* ptr = glMapBuffer(kBufferTarget, access); + glBindBuffer(kBufferTarget, 0); + return ptr; + } + + void* DataBuffer::MapRange(int offset, int size, uint32 access) + { + glBindBuffer(kBufferTarget, m_Handle); + void* ptr = glMapBufferRange(kBufferTarget, offset, size, access); + glBindBuffer(kBufferTarget, 0); + return ptr; + } + + void DataBuffer::FlushMapedRange(int offset, int size) + { + glBindBuffer(kBufferTarget, m_Handle); + glFlushMappedBufferRange(kBufferTarget, offset, size); + glBindBuffer(kBufferTarget, 0); + } + + void DataBuffer::UnMap() + { + glBindBuffer(kBufferTarget, m_Handle); + glUnmapBuffer(kBufferTarget); + glBindBuffer(kBufferTarget, 0); + } + + void DataBuffer::Orphan() + { + glBindBuffer(kBufferTarget, m_Handle); + glBufferData(kBufferTarget, 0, 0, GL_STREAM_DRAW); + glBindBuffer(kBufferTarget, 0); + } + +//--------------------------------------------------------------------------------------- + + static bool IsBufferPoolCreated = false; + + BufferPool::BufferPool() + :m_LiveBuffers() + { + Assert(!IsBufferPoolCreated); + IsBufferPoolCreated = true; + } + + BufferPool::~BufferPool() + { + } + + static const float kBufferAllocateWeight = 10.0f; //! Default weight for buffer allocation from scratch. + static const float kBufferSizeWeightFactor = 1.f / 8096.f; //!< Weight factor for size difference. + static const float kBufferUsageDiffWeight = 8.f; //!< Weight factor for usage difference. + + static int ComputeBufferWeight(DataBuffer* buffer, int desiredSize, GLenum desiredUsage) + { + const int bufferSize = buffer->GetSize(); + const GLenum bufferUsage = buffer->GetUsage(); + + if (bufferSize == 0) + return kBufferAllocateWeight; + + const int sizeDiff = std::abs(bufferSize - desiredSize); + + return float(sizeDiff)*kBufferSizeWeightFactor + ((bufferUsage != desiredUsage) ? kBufferUsageDiffWeight : 0.f); + } + + DataBuffer* BufferPool::ClaimBuffer(int size, GLenum usage) + { + const float maxWeight = kBufferAllocateWeight; + const int maxCandidates = 5; // Number of potential candidates to consider actually. + + const int sizeClass = GetSizeClass(size); + int numCandidates = 0; // Number of potential candidates considered + int bestBufferNdx = -1; + float bestWeight = std::numeric_limits<float>::infinity(); + + for (int idx = 0; idx < m_LiveBuffers[sizeClass].size(); ++idx) + { + DataBuffer* buffer = m_LiveBuffers[sizeClass][idx]; + const float weight = ComputeBufferWeight(buffer, size, usage); + + if (weight < maxWeight && weight < bestWeight) + { + bestWeight = weight; + bestBufferNdx = idx; + ++numCandidates; + } + + if (numCandidates >= maxCandidates) + break; // Do not try other buffers, sorry. + } + + if (bestBufferNdx >= 0) + { + DataBuffer* selectedBuffer = m_LiveBuffers[sizeClass][bestBufferNdx]; + + if (bestBufferNdx + 1 != m_LiveBuffers[sizeClass].size()) + std::swap(m_LiveBuffers[sizeClass][bestBufferNdx], m_LiveBuffers[sizeClass].back()); + m_LiveBuffers[sizeClass].pop_back(); + + return selectedBuffer; + } + else + return new DataBuffer(); + + } + + void BufferPool::ReleaseBuffer(DataBuffer* buffer) + { + InsertToLive(buffer); + } + + void BufferPool::OnEndFrame() + { + UpdatePendingBuffersArray(); + } + + void BufferPool::UpdatePendingBuffersArray() + { + } + + void BufferPool::InsertToLive(DataBuffer* buffer) + { + const int bufferSize = buffer->GetSize(); + const int sizeClass = GetSizeClass(bufferSize); + + m_LiveBuffers[sizeClass].push_back(buffer); + } + + uint BufferPool::GetSizeClass(uint bufferSize) + { + for (int idx = 0; idx < kSizeClassCount; ++idx) + { + if (bufferSize < GetSizeClassLimit(idx)) + return idx; + } + Assert(false); + return 0; + } + + DataBuffer* ClaimBuffer(int size, GLenum usage) + { + return BufferPool::Instance()->ClaimBuffer(size, usage); + } + + void ReleaseBuffer(DataBuffer* buffer) + { + BufferPool::Instance()->ReleaseBuffer(buffer); + } + + int BufferPool::GetSizeClassLimit(int classNdx) + { + // (0, 2^10] 2^11 2^12 2^13 2^14 2^15 INT_MAX + return classNdx + 1 < kSizeClassCount ? (1 << (classNdx*kSizeClassStepLog2 + kSizeClassBaseLog2)) : INT_MAX; + } + +} diff --git a/Runtime/Graphics/GPUDataBuffer.h b/Runtime/Graphics/GPUDataBuffer.h new file mode 100644 index 0000000..f66d951 --- /dev/null +++ b/Runtime/Graphics/GPUDataBuffer.h @@ -0,0 +1,75 @@ +#ifndef GPU_DATABUFFER_H +#define GPU_DATABUFFER_H + +#include <vector> + +#include "../Utilities/Type.h" +#include "../Utilities/Singleton.h" +#include "../Utilities/UtilMacros.h" +#include "../Utilities/Assert.h" +#include "OpenGL.h" + +namespace GPU +{ + + class DataBuffer + { + public: + void Upload(int offset, int size, const void* data); + void* Map(uint32 access); + void* MapRange(int offset, int size, uint32 access); // 性能最佳 + void FlushMapedRange(int offset, int size); + + void UnMap(); + + void Orphan(); + + void Restore(int size, GLenum usage); + void RestoreWithData(int size, GLenum usage, const void* data); + + GET(int, Size, m_Size); + GET(GLenum, Usage, m_Usage); + GET(GLuint, Handle, m_Handle); + + private: + friend class BufferPool; + + DataBuffer(); + ~DataBuffer(); + + GLuint m_Handle; + int m_Size; + GLenum m_Usage; + }; + + class BufferPool : public Singleton<BufferPool> + { + public: + BufferPool(); + ~BufferPool(); + + DataBuffer* ClaimBuffer(int size, GLenum usage); + void ReleaseBuffer(DataBuffer* buffer); + + void OnEndFrame(); + + private: + static const int kSizeClassBaseLog2 = 10; + static const int kSizeClassStepLog2 = 1; + static const int kSizeClassCount = 7; + + int GetSizeClassLimit(int classNdx); + void UpdatePendingBuffersArray(); + void InsertToLive(DataBuffer* buffer); + uint GetSizeClass(uint bufferSize); + + std::vector<DataBuffer*> m_LiveBuffers[kSizeClassCount]; + + }; + + DataBuffer* ClaimBuffer(int size = 0, GLenum usage = GL_ARRAY_BUFFER); + void ReleaseBuffer(DataBuffer* buffer); + +} + +#endif
\ No newline at end of file diff --git a/Runtime/Graphics/GPUDataBuffers.cpp b/Runtime/Graphics/GPUDataBuffers.cpp deleted file mode 100644 index 9e8df9f..0000000 --- a/Runtime/Graphics/GPUDataBuffers.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "GPUDataBuffers.h"
-
-void foo()
-{
- GPUDataBuffer<float> buf = GPUDataBuffer<float>(GL_ARRAY_BUFFER, GL_STATIC_DRAW);
- int n = buf.GetComponentSize();
-}
-
-VBO::VBO() - : GPUDataBuffer<float>(GL_ARRAY_BUFFER, GL_STATIC_DRAW) -{ -} -VBO::VBO(GLenum usage) - : GPUDataBuffer<float>(GL_ARRAY_BUFFER, usage) -{ -} -
-VBO::~VBO() -{ -} - -void VBO::AddVertexAttribute(int index, int numOfComps, int stride) -{ - VertexAttributeDescriptor attribute = VertexAttributeDescriptor();
- attribute.index = index;
- attribute.numOfComponents = numOfComps;
- attribute.stride = stride;
- m_Layout.attributes.push_back(attribute);
-} -void VBO::AddVertexAttribute(VertexAttributeDescriptor& attribute) -{ - m_Layout.attributes.push_back(attribute);
-} -int VBO::GetVertexAttributesCount() -{ - return m_Layout.attributes.size(); -} -void VBO::ClearVertexAttribute() -{ - m_Layout.attributes.clear();
-} - diff --git a/Runtime/Graphics/GPUDataBuffers.h b/Runtime/Graphics/GPUDataBuffers.h deleted file mode 100644 index 62e0413..0000000 --- a/Runtime/Graphics/GPUDataBuffers.h +++ /dev/null @@ -1,160 +0,0 @@ -#pragma once - -#include <vector> - -#include "Runtime/Lua/LuaHelper.h" -#include "../Utilities/Type.h" -#include "../Utilities/Singleton.h" -#include "../Utilities/UtilMacros.h" -#include "../Utilities/Assert.h" -#include "OpenGL.h" - -template<typename T> -// GPU侧的通用缓冲区,最底层的实现 -class GPUDataBuffer -{ -public: - GPUDataBuffer(GLenum target, GLenum usage)/*throw GLException*/ - : m_Target(target) - , m_Usage(usage) - { - glGenBuffers(1, &m_Handle); - - CheckGLError( - glDeleteBuffers(1, &m_Handle); - throw GLException(error); - ); - } - virtual ~GPUDataBuffer() - { - glDeleteBuffers(1, &m_Handle); - } - - // 提交\更新缓冲区数据 - void Upload(int offset, int size, const T* data) - { - glBindBuffer(m_Target, m_Handle); - glBufferSubData(m_Target, offset, size, data); - glBindBuffer(m_Target, 0); - } - - // 更新缓冲区数据(性能优于Upload) - T* Map(uint32 access) - { - glBindBuffer(m_Target, m_Handle); - void* ptr = glMapBuffer(m_Target, access); - glBindBuffer(m_Target, 0); - return ptr; - } - T* MapRange(int offset, int size, uint32 access) // 性能最佳 - { - glBindBuffer(m_Target, m_Handle); - void* ptr = glMapBufferRange(m_Target, offset, size, access); - glBindBuffer(m_Target, 0); - return ptr; - } - void FlushMapedRange(int offset, int size) - { - glBindBuffer(m_Target, m_Handle); - glFlushMappedBufferRange(m_Target, offset, size); - glBindBuffer(m_Target, 0); - } - void UnMap() - { - glBindBuffer(m_Target, m_Handle); - glUnmapBuffer(m_Target); - glBindBuffer(m_Target, 0); - } - - // 重置缓冲区 - void Restore(int size, GLenum usage) - { - RestoreWithData(size, usage, 0); - } - void RestoreWithData(int size, GLenum usage, const T* data) - { - glBindBuffer(m_Target, m_Handle); - glBufferData(m_Target, size, data, usage); - glBindBuffer(m_Target, 0); - m_Size = size; - m_Usage = usage; - } - - void Orphan() - { - glBindBuffer(m_Target, m_Handle); - glBufferData(m_Target, 0, 0, GL_STREAM_DRAW); - glBindBuffer(m_Target, 0); - } - - int GetComponentSize() - { - return sizeof(T); - } - - GET(int, Size, m_Size); - GET(GLenum, Usage, m_Usage); - GET(GLuint, Handle, m_Handle); - -protected: - GLuint m_Handle; - - GLenum m_Target; - GLenum m_Usage; - - int m_Size; - -}; - -struct VertexAttributeDescriptor -{ - int index; // attribute layout index //glBindAttribLocation - int numOfComponents; // 1,2,3,4 - int stride; -}; - -struct VertexDataLayout -{ - std::vector<VertexAttributeDescriptor> attributes; -}; - -class DynamicVBO; -//顶点数据流 -class VBO : public GPUDataBuffer<float> -{ -public: - VBO(); - virtual ~VBO(); - - void AddVertexAttribute(int index, int numOfComps, int stride); - void AddVertexAttribute(VertexAttributeDescriptor& attribute); - int GetVertexAttributesCount(); - void ClearVertexAttribute(); - - static const GLenum componentType = GL_FLOAT; - -protected: - friend class DynamicVBO; - VBO(GLenum usage); - - VertexDataLayout m_Layout; - -}; - -// 频繁读写的共享vbo -class DynamicVBO : public VBO -{ -public: - DynamicVBO(); - ~DynamicVBO(); - -}; - -// 索引数据流 -class IBO : GPUDataBuffer<UInt16> -{ -public: - static const GLenum componentType = GL_UNSIGNED_SHORT; - - -};
\ No newline at end of file diff --git a/Runtime/Graphics/GfxDevice.cpp b/Runtime/Graphics/GfxDevice.cpp new file mode 100644 index 0000000..dd077b8 --- /dev/null +++ b/Runtime/Graphics/GfxDevice.cpp @@ -0,0 +1,88 @@ +#include "GfxDevice.h" + +static bool deviceInited = false; + +GfxDevice g_GfxDevice; + +GfxDevice::GfxDevice() +{ + Assert(!deviceInited); + deviceInited = true; +} + +GfxDevice::~GfxDevice() +{ + +} + +void GfxDevice::Initialize(GfxDeviceSetting setting) +{ +} + +void GfxDevice::Enable(EDeviceEnable enabled) +{ + +} + +void GfxDevice::Disable(EDeviceEnable enabled) +{ + +} + +void GfxDevice::IsEnable(uint flag) +{ + +} + +void GfxDevice::SetDepthTest(EDepthTest testing) +{ + +} + +void GfxDevice::SetCullFace(ECullFace face) +{ + +} + +void GfxDevice::SetStencilMask(byte stencilMask) +{ + +} + +void GfxDevice::SetStencilOp(EStencilOp op) +{ + +} + +void GfxDevice::SetAntiAliasing(int level /*= 0*/) +{ + +} + +void GfxDevice::Clear(int clearFlag) +{ + +} + +void GfxDevice::BeginFrame() +{ + m_IsInsideFrame = true; + +} + +void GfxDevice::EndFrame() +{ + //GPU::BufferPool::Instance()->OnEndFrame(); + + m_IsInsideFrame = false; +} + +void GfxDevice::PresentFrame() +{ +// swap buffers +} + +bool GfxDevice::IsInsideFrame() +{ + return m_IsInsideFrame; +} diff --git a/Runtime/Graphics/GfxDevice.h b/Runtime/Graphics/GfxDevice.h new file mode 100644 index 0000000..70ce8e6 --- /dev/null +++ b/Runtime/Graphics/GfxDevice.h @@ -0,0 +1,76 @@ +#ifndef DEVICE_H +#define DEVICE_H + +#include "../Utilities/NonCopyable.h" +#include "../Utilities/Type.h" +#include "../Utilities/Assert.h" + +#include "Texture.h" +#include "DeviceDefine.h" +#include "VertexBuffer.h" +#include "Color.h" + +struct GfxDeviceSetting +{ + ETextureFilterMode filterMode; // 默认的贴图过滤模式 + ETextureWrapMode wrapMode; // 默认的贴图平铺模式 +}; + +// 对渲染相关API的封装 +class GfxDevice : public NonCopyable +{ +public: + GfxDevice(); + ~GfxDevice(); + + void Initialize(GfxDeviceSetting setting); + + void Enable(EDeviceEnable enabled); + void Disable(EDeviceEnable enabled); + void IsEnable(uint flag); + + void SetDepthTest(EDepthTest testing); + void SetCullFace(ECullFace face); + void SetStencilMask(byte stencilMask); + void SetStencilOp(EStencilOp op); + + void SetAntiAliasing(int level = 0); + + void Clear(int clearFlag); + + void BeginFrame(); + void EndFrame(); + void PresentFrame(); + + bool IsInsideFrame(); + + SharedVertexBuffer* GetSharedVBO() { return &m_SharedVBO; } + + GET_SET(Color, ClearColor, m_ClearColor); + GET_SET(ETextureFilterMode, DefaultFilterMode, m_DefaultFilterMode); + GET_SET(ETextureWrapMode, DefaultWrapMode, m_DefaultWrapMode); + +private: + bool m_IsInsideFrame; + + // 渲染状态 + uint m_EnableFlag; + Color m_ClearColor; + EDepthTest m_DepthTest; + EStencilTest m_StencilTest; + EStencilOp m_StencilOp; + byte m_StencilMask; + + // 贴图默认设置 + ETextureFilterMode m_DefaultFilterMode; + ETextureWrapMode m_DefaultWrapMode; + + SharedVertexBuffer m_SharedVBO; // 共享的VBO,用来做立即渲染 + +}; + +extern GfxDevice g_GfxDevice; + +#define g_SharedVBO (*g_GfxDevice.GetSharedVBO()) + +#endif
\ No newline at end of file diff --git a/Runtime/Graphics/PolyLine.cpp b/Runtime/Graphics/PolyLine.cpp index 794819a..d8ab85b 100644 --- a/Runtime/Graphics/PolyLine.cpp +++ b/Runtime/Graphics/PolyLine.cpp @@ -5,7 +5,5 @@ struct PolyLineVBOLayout { Vector3 position; - ColorRGBA32 color; + Color32 color; }; - - diff --git a/Runtime/Graphics/Quad.cpp b/Runtime/Graphics/Quad.cpp index aa4c9f5..b98f4f2 100644 --- a/Runtime/Graphics/Quad.cpp +++ b/Runtime/Graphics/Quad.cpp @@ -1,52 +1,52 @@ -//#include "../Math/Vector2.h" -//#include "../Math/Vector3.h" -//#include "Quad.h" -//#include "Device.h" -// -//struct QuadVBOLayout -//{ -// Vector3 position; -// Vector2 uv; -//}; -// -//void Quad::Draw() -//{ -// const int nVerts = 4; -// const int nIndices = 6; -// -// float pos[] = { -// m_Left, m_Bottom, 0, // left-bottom -// m_Right, m_Bottom, 0, // right-bottom -// m_Right, m_Top, 0, // right-top -// m_Left, m_Top, 0, // top-left -// }; -// float uv[] = { -// 0, 0, -// 1, 0, -// 1, 1, -// 0, 1, -// }; -// int indices[] = { -// 0, 1, 3, // right-top -// 1, 2, 3, // left-bottom -// }; -// -// uint8* vb; -// uint16* ib; -// -// uint attrs = Mask(VertexAttr_Position) | Mask(VertexAttr_TexCoord0); -// g_SharedVBO.GetChunk(attrs, 4, 6, RenderMode_Triangle, (void**)&vb, (void**)&ib); -// -// QuadVBOLayout* dst = (QuadVBOLayout*)vb; -// for (int i = 0; i < nVerts; ++i) -// { -// dst[i].position.Set(pos[3 * i], pos[3 * i + 1], pos[3 * i + 2]); -// dst[i].uv.Set(uv[2 * i], uv[2 * i + 1]); -// } -// -// for (int i = 0; i < nIndices; ++i) -// ib[i] = indices[i]; -// -// g_SharedVBO.ReleaseChunk(4, 6); -// g_SharedVBO.DrawChunk(); -//}
\ No newline at end of file +#include "../Math/Vector2.h" +#include "../Math/Vector3.h" +#include "Quad.h" +#include "GfxDevice.h" + +struct QuadVBOLayout +{ + Vector3 position; + Vector2 uv; +}; + +void Quad::Draw() +{ + const int nVerts = 4; + const int nIndices = 6; + + float pos[] = { + m_Left, m_Bottom, 0, // left-bottom + m_Right, m_Bottom, 0, // right-bottom + m_Right, m_Top, 0, // right-top + m_Left, m_Top, 0, // top-left + }; + float uv[] = { + 0, 0, + 1, 0, + 1, 1, + 0, 1, + }; + int indices[] = { + 0, 1, 3, // right-top + 1, 2, 3, // left-bottom + }; + + uint8* vb; + uint16* ib; + + uint attrs = Mask(VertexAttr_Position) | Mask(VertexAttr_UV); + g_SharedVBO.GetChunk(attrs, 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib); + + QuadVBOLayout* dst = (QuadVBOLayout*)vb; + for (int i = 0; i < nVerts; ++i) + { + dst[i].position.Set(pos[3 * i], pos[3 * i + 1], pos[3 * i + 2]); + dst[i].uv.Set(uv[2 * i], uv[2 * i + 1]); + } + + for (int i = 0; i < nIndices; ++i) + ib[i] = indices[i]; + + g_SharedVBO.ReleaseChunk(4, 6); + g_SharedVBO.DrawChunk(); +}
\ No newline at end of file diff --git a/Runtime/Graphics/Quad.h b/Runtime/Graphics/Quad.h index 58db95a..0e38ec4 100644 --- a/Runtime/Graphics/Quad.h +++ b/Runtime/Graphics/Quad.h @@ -1,25 +1,25 @@ -//#ifndef QUAD_H -//#define QUAD_H -// -//#include "../Utilities/UtilMacros.h" -// -//class Quad -//{ -//public: -// Quad(float l, float r, float t, float b); -// -// void Set(float l, float r, float t, float b); -// -// GET_SET(float, Left, m_Left); -// GET_SET(float, Right, m_Right); -// GET_SET(float, Top, m_Top); -// GET_SET(float, Bottom, m_Bottom); -// -// void Draw(); -// -//private: -// float m_Left, m_Right, m_Top, m_Bottom; -// -//}; -// -//#endif
\ No newline at end of file +#ifndef QUAD_H +#define QUAD_H + +#include "../Utilities/UtilMacros.h" + +class Quad +{ +public: + Quad(float l, float r, float t, float b); + + void Set(float l, float r, float t, float b); + + GET_SET(float, Left, m_Left); + GET_SET(float, Right, m_Right); + GET_SET(float, Top, m_Top); + GET_SET(float, Bottom, m_Bottom); + + void Draw(); + +private: + float m_Left, m_Right, m_Top, m_Bottom; + +}; + +#endif
\ No newline at end of file diff --git a/Runtime/Graphics/Texture.cpp b/Runtime/Graphics/Texture.cpp index 8f4f245..1fdc0ca 100644 --- a/Runtime/Graphics/Texture.cpp +++ b/Runtime/Graphics/Texture.cpp @@ -79,7 +79,6 @@ Texture::Texture(LuaBind::VM* vm, TextureSetting setting, ImageData* imgData) SetMemberRef(state, m_ImageData, -1);
state.Pop(1);
}
-
}
Texture::~Texture()
diff --git a/Runtime/Graphics/VertexBuffer.cpp b/Runtime/Graphics/VertexBuffer.cpp index 7861d6f..7ea3acd 100644 --- a/Runtime/Graphics/VertexBuffer.cpp +++ b/Runtime/Graphics/VertexBuffer.cpp @@ -1,7 +1,7 @@ #include "VertexBuffer.h" #include "../Profiling/FrameStats.h" -void SetupDefaultVertexArray(const VertexArrayInfo& info); +void SetupDefaultVertexArray(const VertexDataInfo& info); void InvalidateVertexInputCache(); // LUTs of vertex attributes @@ -10,8 +10,10 @@ static const int kVertexAttrSize[VertexAttr_Count] = { 3 * sizeof(float), // normal 4 * sizeof(float), // tangent 1 * sizeof(uint32), // color - 2 * sizeof(float), // uv0 - 2 * sizeof(float), // uv1 + 2 * sizeof(float), // uv + 2 * sizeof(float), // uv2 + 2 * sizeof(float), // uv3 + 2 * sizeof(float), // uv4 }; static const int kVertexAttrDimension[VertexAttr_Count] = { @@ -19,8 +21,10 @@ static const int kVertexAttrDimension[VertexAttr_Count] = { 3, // normal 4, // tangent 1, // color - 2, // uv0 - 2 // uv1 + 2, // uv + 2, // uv2 + 2, // uv3 + 2, // uv4 }; enum VertexAttrFormat @@ -34,11 +38,11 @@ enum VertexAttrFormat }; // map VertexAttrFormat to OpenGL type -static GLenum kGLVertexAttrFormat [VertexAttr_Count] = { - GL_FLOAT, // VertexAttrFormat_Float - GL_HALF_FLOAT, // VertexAttrFormat_Float16 +static GLenum kGLVertexAttrFormat[VertexAttrFormat_Count] = { + GL_FLOAT, // VertexAttrFormat_Float + GL_HALF_FLOAT, // VertexAttrFormat_Float16 GL_UNSIGNED_BYTE, // VertexAttrFormat_Color - GL_BYTE // VertexAttrFormat_Byte + GL_BYTE // VertexAttrFormat_Byte }; static bool IsGLVertexAttrNeedNormalized(uint attr, uint format) @@ -94,6 +98,8 @@ VertexBuffer::~VertexBuffer() { } +//------------------------------------------------------------------------------------------------------------ + SharedVertexBuffer::SharedVertexBuffer() { } @@ -106,7 +112,7 @@ SharedVertexBuffer::~SharedVertexBuffer() //-> ReleaseChunk //-> DrawChunk -void SharedVertexBuffer::GetChunk(uint attrsMask, int maxVerts, int maxIndices, RenderMode mode, void **out_vb, void **out_ib) +void SharedVertexBuffer::GetChunk(uint attrsMask, int maxVerts, int maxIndices, EPrimitive primitive, void **out_vb, void **out_ib) { Assert(out_vb && out_ib); @@ -147,7 +153,7 @@ void SharedVertexBuffer::GetChunk(uint attrsMask, int maxVerts, int maxIndices, m_CurVB = vertexBuffer; m_CurIB = indexBuffer; - m_CurRenderMode = mode; + m_CurPrimitive = primitive; m_CurAttrMask = attrsMask; m_CurStride = stride; } @@ -187,7 +193,7 @@ void SharedVertexBuffer::ReleaseChunk(int actualVerts, int actualIndices) void SharedVertexBuffer::DrawChunk() { - VertexArrayInfo vertexArray; + VertexDataInfo vertexArray; FillVertexArrayInfo(vertexArray); // bind vertex attributes data @@ -198,18 +204,18 @@ void SharedVertexBuffer::DrawChunk() if (m_CurIB) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_CurIB->GetHandle()); - switch (m_CurRenderMode) + switch (m_CurPrimitive) { - case RenderMode_Triangle: + case Primitive_Triangle: glDrawElements(GL_TRIANGLES, m_CurIndexCount, kGLIndexFormat, indexPtr); g_FrameStats.AddDrawCall(); g_FrameStats.AddTrianglesCount(m_CurIndexCount / 3); break; - case RenderMode_Line: + case Primitive_Line: glDrawElements(GL_LINE, m_CurIndexCount, kGLIndexFormat, indexPtr); g_FrameStats.AddDrawCall(); break; - case RenderMode_Point: + case Primitive_Point: glDrawElements(GL_POINT, m_CurIndexCount, kGLIndexFormat, indexPtr); g_FrameStats.AddDrawCall(); break; @@ -222,11 +228,11 @@ void SharedVertexBuffer::DrawChunk() Clean(); } -void SharedVertexBuffer::FillVertexArrayInfo(VertexArrayInfo& dst) +void SharedVertexBuffer::FillVertexArrayInfo(VertexDataInfo& dst) { const byte* basepointer = m_CurVB ? 0 : &m_CurVBData[0]; const GLuint buffer = m_CurVB ? m_CurVB->GetHandle() : 0; - + int attrOffsets[VertexAttr_Count] = {0}; { @@ -241,12 +247,12 @@ void SharedVertexBuffer::FillVertexArrayInfo(VertexArrayInfo& dst) } } + dst.buffer = buffer; + for (uint32 attrIdx = 0; attrIdx < VertexAttr_Count; ++attrIdx) { if (m_CurAttrMask & Mask(attrIdx)) { - dst.buffers[attrIdx] = buffer; - dst.attributes[attrIdx].pointer = basepointer + attrOffsets[attrIdx]; dst.attributes[attrIdx].componentType = GetDefaultShaderChannelFormat(attrIdx); dst.attributes[attrIdx].componentNum = GetDefaultShaderChannelDimension(attrIdx); @@ -271,7 +277,7 @@ void SharedVertexBuffer::Clean() m_CurIB = 0; } - m_CurRenderMode = (RenderMode)0; + m_CurPrimitive = Primitive_Triangle; m_CurAttrMask = 0; m_CurStride = 0; m_CurVertexCount = 0; @@ -283,9 +289,9 @@ void SharedVertexBuffer::Clean() static uint32 sEnabledArrays = 0; -void SetupDefaultVertexArray(const VertexArrayInfo& info) +void SetupDefaultVertexArray(const VertexDataInfo& info) { - GLuint curBoundBuffer = 0; + glBindBuffer(GL_ARRAY_BUFFER, info.buffer); for (int attrIdx = 0; attrIdx < VertexAttr_Count; ++attrIdx) { @@ -293,19 +299,12 @@ void SetupDefaultVertexArray(const VertexArrayInfo& info) { if (!sEnabledArrays & Mask(attrIdx)) glEnableVertexAttribArray(attrIdx); - const GLuint buffer = info.buffers[attrIdx]; - const int numCompo = info.attributes[attrIdx].componentNum; - const GLenum compoType = kGLVertexAttrFormat[info.attributes[attrIdx].componentType]; - const bool normalized = IsGLVertexAttrNeedNormalized(attrIdx, compoType); - const uint stride = info.attributes[attrIdx].stride; + int numCompo = info.attributes[attrIdx].componentNum; + GLenum compoType = kGLVertexAttrFormat[info.attributes[attrIdx].componentType]; + bool normalized = IsGLVertexAttrNeedNormalized(attrIdx, compoType); + uint stride = info.attributes[attrIdx].stride; const void* pointer = info.attributes[attrIdx].pointer; - if (curBoundBuffer != buffer) - { - glBindBuffer(GL_ARRAY_BUFFER, buffer); - curBoundBuffer = buffer; - } - glVertexAttribPointer(attrIdx, numCompo, compoType, normalized ? GL_TRUE : GL_FALSE, stride, pointer); } else if(sEnabledArrays & Mask(attrIdx)) diff --git a/Runtime/Graphics/VertexBuffer.h b/Runtime/Graphics/VertexBuffer.h index cf33cc5..b38b0b6 100644 --- a/Runtime/Graphics/VertexBuffer.h +++ b/Runtime/Graphics/VertexBuffer.h @@ -7,41 +7,44 @@ #include "../Shaders/ShaderChannel.h" #include "OpenGL.h" -#include "GPUDataBuffers.h" +#include "GPUDataBuffer.h" -enum VertexAttr +// 默认的顶点属性,够用了 +enum EVertexAttr { VertexAttr_Position = 0, - VertexAttr_Normal = 1, - VertexAttr_Tangent = 2, - VertexAttr_Color = 3, - VertexAttr_TexCoord0 = 4, - VertexAttr_TexCoord1 = 5, - - VertexAttr_Count = 6 + VertexAttr_Normal, + VertexAttr_Tangent, + VertexAttr_Color, + VertexAttr_UV, + VertexAttr_UV2, + VertexAttr_UV3, + VertexAttr_UV4, + + VertexAttr_Count }; -enum RenderMode +enum EPrimitive { - RenderMode_Triangle = 1, - RenderMode_Line = 2, - RenderMode_Point = 3, + Primitive_Triangle = 1, + Primitive_Line = 2, + Primitive_Point = 3, }; -struct VertexInputInfo +struct VertexAttributeDescriptor { - const void* pointer; // either cpu memory or gpu memory - uint componentType; // one of VertexAttrFormat + const void* pointer; // 内存或显存 + uint componentType; uint componentNum; uint16 stride; }; -// gpu side vertex attributes array -struct VertexArrayInfo +// GPU侧的顶点属性 +struct VertexDataInfo { uint32 enableMask; - GLuint buffers[VertexAttr_Count]; - VertexInputInfo attributes[VertexAttr_Count]; + GLuint buffer; + VertexAttributeDescriptor attributes[VertexAttr_Count]; }; class VertexBuffer @@ -62,8 +65,8 @@ public: private: VertexBufferType m_Type; - VBO *m_VB; - IBO *m_IB;// vertex buffer and index buffer + GPU::DataBuffer *m_VB; + GPU::DataBuffer *m_IB;// vertex buffer and index buffer }; @@ -73,14 +76,14 @@ public: SharedVertexBuffer(); ~SharedVertexBuffer(); - void GetChunk(uint attrs, int maxVerts, int maxIndices, RenderMode mode, void **out_vb, void **out_ib); + void GetChunk(uint attrs, int maxVerts, int maxIndices, EPrimitive primitive, void **out_vb, void **out_ib); void ReleaseChunk(int actualVerts, int actualIndices); void DrawChunk(); private: - void FillVertexArrayInfo(VertexArrayInfo& dst); + void FillVertexArrayInfo(VertexDataInfo& dst); void Clean(); @@ -88,14 +91,14 @@ private: enum { DataBufferThreshold = 1024 }; // shared vbo and ibo - VBO *m_CurVB; - IBO *m_CurIB; + GPU::DataBuffer *m_CurVB; + GPU::DataBuffer *m_CurIB; // restore vbo and ibo data if not using pinned memory mapping std::vector<byte> m_CurVBData; std::vector<uint16> m_CurIBData; - RenderMode m_CurRenderMode; + EPrimitive m_CurPrimitive; uint m_CurAttrMask; uint m_CurStride; |