summaryrefslogtreecommitdiff
path: root/Runtime/Graphics
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-11-02 19:44:01 +0800
committerchai <chaifix@163.com>2021-11-02 19:44:01 +0800
commit72812a7b47f90f9460e54e8149ba9199a7841244 (patch)
tree9ebdb4b65d7cbbc0a3d7c436a9b45d4eff351b62 /Runtime/Graphics
parentb7511abf1a1f302b5c7ebf50faaf65b62d7bb6ed (diff)
! Text mesh done
Diffstat (limited to 'Runtime/Graphics')
-rw-r--r--Runtime/Graphics/Color.h14
-rw-r--r--Runtime/Graphics/DynamicVertexBuffer.cpp10
-rw-r--r--Runtime/Graphics/DynamicVertexBuffer.h6
-rw-r--r--Runtime/Graphics/GPUDataBuffer.h6
-rw-r--r--Runtime/Graphics/GfxDevice.cpp3
-rw-r--r--Runtime/Graphics/OpenGL.h8
-rw-r--r--Runtime/Graphics/Shader.cpp2
-rw-r--r--Runtime/Graphics/Texture.cpp4
-rw-r--r--Runtime/Graphics/VertexBuffer.cpp107
-rw-r--r--Runtime/Graphics/VertexBuffer.h14
10 files changed, 158 insertions, 16 deletions
diff --git a/Runtime/Graphics/Color.h b/Runtime/Graphics/Color.h
index af8d3ba..ebccafe 100644
--- a/Runtime/Graphics/Color.h
+++ b/Runtime/Graphics/Color.h
@@ -14,6 +14,13 @@ namespace Internal
this->b = b;
this->a = a;
}
+ void Set(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;
};
@@ -26,6 +33,13 @@ namespace Internal
this->b = b;
this->a = a;
}
+ void Set(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0, unsigned char a = 0)
+ {
+ this->r = r;
+ this->g = g;
+ this->b = b;
+ this->a = a;
+ }
unsigned char r, g, b, a;
};
diff --git a/Runtime/Graphics/DynamicVertexBuffer.cpp b/Runtime/Graphics/DynamicVertexBuffer.cpp
index 203aa26..cb59a8b 100644
--- a/Runtime/Graphics/DynamicVertexBuffer.cpp
+++ b/Runtime/Graphics/DynamicVertexBuffer.cpp
@@ -1,4 +1,5 @@
#include "DynamicVertexBuffer.h"
+#include "CustomVertexLayout.h"
#include "../Profiling/FrameStats.h"
DynamicVertexBuffer::DynamicVertexBuffer()
@@ -109,11 +110,12 @@ void DynamicVertexBuffer::GetChunk(uint sizePerVert, uint sizePerIndex, int maxV
{
Assert(out_vb && out_ib);
+ m_CurStride = sizePerVert;
uint vbufferSize = sizePerVert * maxVerts;
uint ibufferSize = sizePerIndex * maxIndices;
- const bool mapVertexBuffer = vbufferSize >= DataBufferThreshold;
- const bool mapIndexBuffer = ibufferSize >= DataBufferThreshold;
+ const bool mapVertexBuffer = vbufferSize >= kDataBufferThreshold;
+ const bool mapIndexBuffer = ibufferSize >= kDataBufferThreshold;
GLenum usage = GL_STREAM_DRAW;
@@ -221,7 +223,7 @@ void DynamicVertexBuffer::ReleaseChunk(int actualVerts, int actualIndices)
m_CurVB->FlushMapedRange(0, actualVBufferSize);
m_CurVB->UnMap();
}
- else if (actualVBufferSize >= DataBufferThreshold)
+ else if (actualVBufferSize >= kDataBufferThreshold)
{
m_CurVB = GPU::ClaimBuffer(actualVBufferSize, usage);
m_CurVB->RestoreWithData(actualVBufferSize, usage, &m_CurVBData[0]);
@@ -232,7 +234,7 @@ void DynamicVertexBuffer::ReleaseChunk(int actualVerts, int actualIndices)
m_CurIB->FlushMapedRange(0, actualIBufferSize);
m_CurIB->UnMap();
}
- else if (actualIBufferSize >= DataBufferThreshold)
+ else if (actualIBufferSize >= kDataBufferThreshold)
{
m_CurIB = GPU::ClaimBuffer(0, usage);
m_CurIB->RestoreWithData(0, usage, &m_CurIBData[0]);
diff --git a/Runtime/Graphics/DynamicVertexBuffer.h b/Runtime/Graphics/DynamicVertexBuffer.h
index 12aa48c..6f21010 100644
--- a/Runtime/Graphics/DynamicVertexBuffer.h
+++ b/Runtime/Graphics/DynamicVertexBuffer.h
@@ -35,8 +35,8 @@ private:
void Clean();
- // 如果数据大小超过这个限制,用内存数据,而不是glBufferData
- enum { DataBufferThreshold = 1024 };
+ // 如果数据大小小于这个限制,用内存数据,而不是glBufferData
+ static const int kDataBufferThreshold = 1024;
GPU::DataBuffer *m_CurVB;
GPU::DataBuffer *m_CurIB;
@@ -52,4 +52,4 @@ private:
uint m_CurVertexCount;
uint m_CurIndexCount;
-};
+}; \ No newline at end of file
diff --git a/Runtime/Graphics/GPUDataBuffer.h b/Runtime/Graphics/GPUDataBuffer.h
index f66d951..3d24514 100644
--- a/Runtime/Graphics/GPUDataBuffer.h
+++ b/Runtime/Graphics/GPUDataBuffer.h
@@ -15,6 +15,9 @@ namespace GPU
class DataBuffer
{
public:
+ DataBuffer();
+ ~DataBuffer();
+
void Upload(int offset, int size, const void* data);
void* Map(uint32 access);
void* MapRange(int offset, int size, uint32 access); // 性能最佳
@@ -34,9 +37,6 @@ namespace GPU
private:
friend class BufferPool;
- DataBuffer();
- ~DataBuffer();
-
GLuint m_Handle;
int m_Size;
GLenum m_Usage;
diff --git a/Runtime/Graphics/GfxDevice.cpp b/Runtime/Graphics/GfxDevice.cpp
index 9abea12..2c740ed 100644
--- a/Runtime/Graphics/GfxDevice.cpp
+++ b/Runtime/Graphics/GfxDevice.cpp
@@ -137,8 +137,7 @@ void GfxDevice::SetUniformTexture(const char* name, Texture* texture)
int texUnit = s_TextureUnitBucket.back();
s_TextureUnitBucket.pop_back();
glActiveTexture(GL_TEXTURE0 + texUnit);
- //glBindTexture(GL_TEXTURE_2D, texture->GetGpuID());
- glBindTexture(GL_TEXTURE_2D, 2);
+ glBindTexture(GL_TEXTURE_2D, texture->GetGpuID());
GLint loc = glGetUniformLocation(m_Shader.GetID(), name);
glUniform1i(loc, texUnit);
diff --git a/Runtime/Graphics/OpenGL.h b/Runtime/Graphics/OpenGL.h
index b1b777e..d93fb5a 100644
--- a/Runtime/Graphics/OpenGL.h
+++ b/Runtime/Graphics/OpenGL.h
@@ -15,6 +15,14 @@ if(true){ \
} \
}
+#define WipeGLError() \
+if(true){\
+ GLenum error; \
+ while ((error = glGetError()) != GL_NO_ERROR) { \
+ throw GLException(error); \
+ } \
+}
+
extern std::string g_sharedGLErrorMsg;
class GLException : public std::exception
diff --git a/Runtime/Graphics/Shader.cpp b/Runtime/Graphics/Shader.cpp
index 60e165f..013b1ba 100644
--- a/Runtime/Graphics/Shader.cpp
+++ b/Runtime/Graphics/Shader.cpp
@@ -88,7 +88,7 @@ void Shader::CompileProgram(const char* vert, const char* frag, bool keepSrc)
glAttachShader(m_ProgramID, m_VertID);
glAttachShader(m_ProgramID, m_FragID);
glLinkProgram(m_ProgramID);
- checkCompileshaderErrorors(m_FragID, "PROGRAM");
+ checkCompileshaderErrorors(m_ProgramID, "PROGRAM");
}
Shader::~Shader()
diff --git a/Runtime/Graphics/Texture.cpp b/Runtime/Graphics/Texture.cpp
index eed0b90..a1f4181 100644
--- a/Runtime/Graphics/Texture.cpp
+++ b/Runtime/Graphics/Texture.cpp
@@ -106,6 +106,8 @@ Texture::Texture(TextureSetting setting, int w, int h)
m_FilterMode = setting.filterMode;
m_KeepPixelData = setting.keepImageData;
+ WipeGLError();
+
glGenTextures(1, &m_GPUID);
glBindTexture(GL_TEXTURE_2D, m_GPUID);
@@ -170,6 +172,8 @@ Texture::Texture(TextureSetting setting, int w, int h)
glBindTexture(GL_TEXTURE_2D, 0);
throw TextureException(error);
);
+
+ glBindTexture(GL_TEXTURE_2D, 0);
}
Texture::~Texture()
diff --git a/Runtime/Graphics/VertexBuffer.cpp b/Runtime/Graphics/VertexBuffer.cpp
index 5d136ee..16b29d5 100644
--- a/Runtime/Graphics/VertexBuffer.cpp
+++ b/Runtime/Graphics/VertexBuffer.cpp
@@ -3,8 +3,10 @@
VertexBuffer::VertexBuffer(int vbSize, int ibSize, VertexBufferType type)
{
- m_VB = GPU::ClaimBuffer(vbSize, GL_ARRAY_BUFFER);
- m_IB = GPU::ClaimBuffer(ibSize, GL_ELEMENT_ARRAY_BUFFER);
+ m_VB = new GPU::DataBuffer();
+ m_IB = new GPU::DataBuffer();
+ //m_VB = GPU::ClaimBuffer(vbSize, GL_STATIC_DRAW);
+ //m_IB = GPU::ClaimBuffer(ibSize, GL_STATIC_DRAW);
}
VertexBuffer::~VertexBuffer()
@@ -12,3 +14,104 @@ VertexBuffer::~VertexBuffer()
GPU::ReleaseBuffer(m_VB);
GPU::ReleaseBuffer(m_IB);
}
+// GetChunk
+// <fill>
+// FlushChunk
+// DrawChunk
+void VertexBuffer::GetChunk(uint sizePerVert, uint sizePerIndex, int maxVerts, int maxIndices, EPrimitive primitive, void **out_vb, void **out_ib)
+{
+ m_SizePerVertex = sizePerVert;
+
+ uint vbufferSize = sizePerVert * maxVerts;
+ uint ibufferSize = sizePerIndex * maxIndices;
+
+ GLenum usage = GL_STATIC_DRAW;
+ m_VB->Restore(vbufferSize, usage);
+ m_IB->Restore(ibufferSize, usage);
+
+ const GLenum access = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
+
+ *out_vb = m_VB->MapRange(0, vbufferSize, access);
+ *out_ib = m_IB->MapRange(0, ibufferSize, access);
+
+ m_Primitive = primitive;
+
+ WipeGLError();
+}
+
+void VertexBuffer::FlushChunk(int actualVerts, int actualIndices)
+{
+ int actualVBufferSize = m_SizePerVertex * actualVerts;
+ int actualIBufferSize = VertexLayout::GetDefaultIndexSize() * actualIndices;
+
+ m_CurIndexCount = actualIndices;
+
+ m_VB->FlushMapedRange(0, actualVBufferSize);
+ m_IB->FlushMapedRange(0, actualIBufferSize);
+
+ // 一定要unmap
+ m_VB->UnMap();
+ m_IB->UnMap();
+
+ WipeGLError();
+}
+
+void VertexBuffer::Draw(CustomVertexLayout& layout)
+{
+ const byte* basepointer = 0;
+ const GLuint buffer = m_VB->GetHandle();
+
+ FillCustomVertexLayout(layout);
+
+ VertexLayout::SetupCustomVertexLayout(layout);
+
+ layout.RestorePointer();
+
+ const void* indexPtr = 0;
+
+ CheckGLError(
+ throw GLException(error);
+ );
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IB->GetHandle());
+
+ CheckGLError(
+ throw GLException(error);
+ );
+ GLenum indexFormat = VertexLayout::GetDefaultIndexFormat();
+
+ switch (m_Primitive)
+ {
+ case Primitive_Triangle:
+ glDrawElements(GL_TRIANGLES, m_CurIndexCount, indexFormat, indexPtr);
+ g_FrameStats.AddDrawCall();
+ g_FrameStats.AddTrianglesCount(m_CurIndexCount / 3);
+ break;
+ case Primitive_Line:
+ glDrawElements(GL_LINE, m_CurIndexCount, indexFormat, indexPtr);
+ g_FrameStats.AddDrawCall();
+ break;
+ case Primitive_Point:
+ glDrawElements(GL_POINT, m_CurIndexCount, indexFormat, indexPtr);
+ g_FrameStats.AddDrawCall();
+ break;
+ }
+
+ CheckGLError(
+ throw GLException(error);
+ );
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+}
+
+void VertexBuffer::FillCustomVertexLayout(CustomVertexLayout& dst)
+{
+ const byte* basepointer = 0;
+ const GLuint buffer = m_VB->GetHandle();
+
+ dst.buffer = buffer;
+
+ for (int i = 0; i < dst.attributes.size(); ++i)
+ {
+ int offset = dst.attributes[i].startOffset;
+ dst.attributes[i].pointer = basepointer + offset;
+ }
+} \ No newline at end of file
diff --git a/Runtime/Graphics/VertexBuffer.h b/Runtime/Graphics/VertexBuffer.h
index c651bdb..2d3a9f5 100644
--- a/Runtime/Graphics/VertexBuffer.h
+++ b/Runtime/Graphics/VertexBuffer.h
@@ -26,16 +26,28 @@ public:
VertexBuffer(int vbSize, int ibSize, VertexBufferType type);
~VertexBuffer();
+ // 填充数据
+ void GetChunk(uint sizePerVert, uint sizePerIndex, int maxVerts, int maxIndices, EPrimitive primitive, void **out_vb, void **out_ib);
+ // 提交数据
+ void FlushChunk(int actualVerts, int actualIndices);
+ GET(GPU::DataBuffer*, VB, m_VB);
+ GET(GPU::DataBuffer*, IB, m_IB);
- void Draw();
+ void Draw(CustomVertexLayout& layout);
private:
+ void FillCustomVertexLayout(CustomVertexLayout& dst);
+
VertexBufferType m_Type;
GPU::DataBuffer* m_VB;
GPU::DataBuffer* m_IB;
+ int m_SizePerVertex;
+
+ int m_CurIndexCount;
+
EPrimitive m_Primitive;
};