@@ -9,23 +9,22 @@ namespace OverEngine
99
1010 struct Vertex
1111 {
12- Vector3 a_Position0 = Vector3(0 .0f );
13- Vector3 a_Position1 = Vector3(0 .0f );
14- Vector3 a_Position2 = Vector3(0 .0f );
15- Vector3 a_Position3 = Vector3(0 .0f );
12+ Vector3 a_Position = Vector3(0 .0f );
1613
1714 Color a_Color = Color(1 .0f );
1815 int a_TexSlot = -1 ;
19- Vector4 a_TexCoord = Vector4(0 , 0 , 1 , 1 );
16+ Vector2 a_TexCoord = Vector4(0 , 0 , 1 , 1 );
2017 Vector4 a_TexRegion = Vector4(0 .0f );
2118 int a_TexRepeat = 0 ;
2219
23- int a_LiquidGlass = false ;
20+ Vector3 a_MidPoint = Vector3(0 .0f );
21+ Vector2 a_QuadNDC2ScreenNDCScale = Vector3(1 .0f );
22+ int a_LiquidGlass = 0 ;
2423 };
2524
2625 // Hard-coded Limits
2726 static constexpr uint32_t MaxTextureCount = 32 ;
28- static constexpr uint32_t MaxQuadCount = 1000000 ;
27+ static constexpr uint32_t MaxQuadCount = 1000 ;
2928
3029 struct Renderer2DData
3130 {
@@ -46,6 +45,8 @@ namespace OverEngine
4645
4746 Mat4x4 ViewProjectionMatrix;
4847 bool DepthSorting;
48+
49+ // static constexpr float Qua
4950 };
5051
5152 static Renderer2DData* s_Data;
@@ -57,37 +58,43 @@ namespace OverEngine
5758 s_Data->QuadVA = VertexArray::Create ();
5859
5960 s_Data->QuadVB = VertexBuffer::Create ();
60- s_Data->QuadVB ->AllocateStorage (MaxQuadCount * sizeof (Vertex));
61+ s_Data->QuadVB ->AllocateStorage (MaxQuadCount * 4 * sizeof (Vertex));
6162 s_Data->QuadVB ->SetLayout ({
62- { ShaderDataType::Float3, " a_Position0" },
63- { ShaderDataType::Float3, " a_Position1" },
64- { ShaderDataType::Float3, " a_Position2" },
65- { ShaderDataType::Float3, " a_Position3" },
63+ { ShaderDataType::Float3, " a_Position" },
6664
6765 { ShaderDataType::Float4, " a_Color" },
6866 { ShaderDataType::Int, " a_TexSlot" },
69- { ShaderDataType::Float4 , " a_TexCoord" },
67+ { ShaderDataType::Float2 , " a_TexCoord" },
7068 { ShaderDataType::Float4, " a_TexRegion" },
7169 { ShaderDataType::Int, " a_TexRepeat" },
7270
71+ { ShaderDataType::Float3, " a_MidPoint" },
72+ { ShaderDataType::Float2, " v_QuadNDC2ScreenNDCScale" },
7373 { ShaderDataType::Int, " a_LiquidGlass" }
7474 });
7575 s_Data->QuadVA ->AddVertexBuffer (s_Data->QuadVB );
7676
77+ // TODO: Dynamically scale
7778 {
7879 auto quadIB = IndexBuffer::Create ();
79- uint32_t * indices = new uint32_t [MaxQuadCount];
80-
81- for (uint32_t i = 0 ; i < MaxQuadCount; i++)
82- indices[i] = i;
80+ uint32_t * indices = new uint32_t [6 * MaxQuadCount];
81+
82+ for (uint32_t i = 0 ; i < MaxQuadCount; i++) {
83+ indices[6 * i + 0 ] = 4 * i + 0 ;
84+ indices[6 * i + 1 ] = 4 * i + 1 ;
85+ indices[6 * i + 2 ] = 4 * i + 3 ;
86+ indices[6 * i + 3 ] = 4 * i + 3 ;
87+ indices[6 * i + 4 ] = 4 * i + 2 ;
88+ indices[6 * i + 5 ] = 4 * i + 0 ;
89+ }
8390
8491 quadIB->BufferData (indices, MaxQuadCount);
8592 delete[] indices;
8693
8794 s_Data->QuadVA ->SetIndexBuffer (quadIB);
8895 }
8996
90- s_Data->QuadBufferBasePtr = new Vertex[MaxQuadCount];
97+ s_Data->QuadBufferBasePtr = new Vertex[4 * MaxQuadCount];
9198 s_Data->QuadBufferPtr = s_Data->QuadBufferBasePtr ;
9299
93100 s_Data->Shader = Shader::Create (" assets/shaders/BatchRenderer2D.glsl" );
@@ -171,12 +178,12 @@ namespace OverEngine
171178 {
172179 std::sort (s_Data->QuadBufferBasePtr , s_Data->QuadBufferBasePtr + s_Data->QuadCount , [](const Vertex& a, const Vertex& b)
173180 {
174- return a.a_Position0 .z > b.a_Position0 .z ;
181+ return a.a_Position .z > b.a_Position .z ;
175182 });
176183 }
177184
178185 // Upload Data
179- s_Data->QuadVB ->BufferSubData ((void *)s_Data->QuadBufferBasePtr , s_Data->QuadCount * sizeof (Vertex));
186+ s_Data->QuadVB ->BufferSubData ((void *)s_Data->QuadBufferBasePtr , 4 * s_Data->QuadCount * sizeof (Vertex));
180187
181188 // Bind Textures
182189 for (uint8_t i = 0 ; i < s_Data->TextureCount ; i++)
@@ -187,7 +194,7 @@ namespace OverEngine
187194 s_Data->Shader ->Bind ();
188195
189196 // DrawCall
190- RenderCommand::DrawIndexed (s_Data->QuadVA , s_Data->QuadCount , DrawType::Points );
197+ RenderCommand::DrawIndexed (s_Data->QuadVA , 6 * s_Data->QuadCount , DrawType::Triangles );
191198 s_Statistics.DrawCalls ++;
192199 }
193200
@@ -220,23 +227,35 @@ namespace OverEngine
220227
221228 auto mat = s_Data->ViewProjectionMatrix * transform;
222229
223- s_Data->QuadBufferPtr ->a_Position0 = Vector3 (mat * Vector4 (-0.5 , -0.5 , 0.0 , 1.0 ));
224- s_Data->QuadBufferPtr ->a_Position1 = Vector3 (mat * Vector4 ( 0.5 , -0.5 , 0.0 , 1.0 ));
225- s_Data->QuadBufferPtr ->a_Position2 = Vector3 (mat * Vector4 (-0.5 , 0.5 , 0.0 , 1.0 ));
226- s_Data->QuadBufferPtr ->a_Position3 = Vector3 (mat * Vector4 ( 0.5 , 0.5 , 0.0 , 1.0 ));
227-
228230 // OE_CORE_INFO("Emitting {}: ({}, {}) ({}, {}) ({}, {}) ({}, {})", liquidGlass,
229231 // s_Data->QuadBufferPtr->a_Position0.x, s_Data->QuadBufferPtr->a_Position0.y,
230232 // s_Data->QuadBufferPtr->a_Position1.x, s_Data->QuadBufferPtr->a_Position1.y,
231233 // s_Data->QuadBufferPtr->a_Position2.x, s_Data->QuadBufferPtr->a_Position2.y,
232234 // s_Data->QuadBufferPtr->a_Position3.x, s_Data->QuadBufferPtr->a_Position3.y);
233235
234- s_Data->QuadBufferPtr ->a_Color = color;
235- s_Data->QuadBufferPtr ->a_TexSlot = -1 ;
236+ s_Data->QuadBufferPtr [0 ].a_Position = Vector3 (mat * Vector4 (-0.5 , -0.5 , 0.0 , 1.0 ));
237+ s_Data->QuadBufferPtr [1 ].a_Position = Vector3 (mat * Vector4 ( 0.5 , -0.5 , 0.0 , 1.0 ));
238+ s_Data->QuadBufferPtr [2 ].a_Position = Vector3 (mat * Vector4 (-0.5 , 0.5 , 0.0 , 1.0 ));
239+ s_Data->QuadBufferPtr [3 ].a_Position = Vector3 (mat * Vector4 ( 0.5 , 0.5 , 0.0 , 1.0 ));
240+
241+ s_Data->QuadBufferPtr [0 ].a_TexCoord = Vector2 (0.0 , 0.0 );
242+ s_Data->QuadBufferPtr [1 ].a_TexCoord = Vector2 (1.0 , 0.0 );
243+ s_Data->QuadBufferPtr [2 ].a_TexCoord = Vector2 (0.0 , 1.0 );
244+ s_Data->QuadBufferPtr [3 ].a_TexCoord = Vector2 (1.0 , 1.0 );
245+
246+ Vector3 midPoint = (s_Data->QuadBufferPtr [0 ].a_Position + s_Data->QuadBufferPtr [3 ].a_Position ) * 0 .5f ;
247+ Vector3 quadNDC2ScreenNDCScale = (s_Data->QuadBufferPtr [3 ].a_Position - s_Data->QuadBufferPtr [0 ].a_Position ) * 0 .5f ;
248+
249+ for (int i = 0 ; i < 4 ; i++) {
250+ s_Data->QuadBufferPtr [i].a_Color = color;
251+ s_Data->QuadBufferPtr [i].a_TexSlot = -1 ;
236252
237- s_Data->QuadBufferPtr ->a_LiquidGlass = liquidGlass;
253+ s_Data->QuadBufferPtr [i].a_MidPoint = midPoint;
254+ s_Data->QuadBufferPtr [i].a_QuadNDC2ScreenNDCScale = quadNDC2ScreenNDCScale;
255+ s_Data->QuadBufferPtr [i].a_LiquidGlass = liquidGlass;
256+ }
238257
239- s_Data->QuadBufferPtr ++ ;
258+ s_Data->QuadBufferPtr += 4 ;
240259 s_Data->QuadCount ++;
241260 s_Statistics.QuadCount ++;
242261 }
@@ -271,39 +290,55 @@ namespace OverEngine
271290 if (s_Data->QuadCount + 1 >= MaxQuadCount)
272291 NextBatch ();
273292
293+ uint8_t slot;
274294 {
275295 Ref<Texture2D> gpuTex = (props.Sprite ->GetType () == TextureType::SubTexture) ? std::dynamic_pointer_cast<SubTexture2D>(props.Sprite )->GetMasterTexture () : props.Sprite ;
276296
277297 auto end = s_Data->TextureBindList .begin () + s_Data->TextureCount ;
278298 auto it = std::find (s_Data->TextureBindList .begin (), end, gpuTex);
279299 if (it == end)
280300 {
281- uint8_t slot = s_Data->TextureCount ;
301+ slot = s_Data->TextureCount ;
282302 if (slot + 1u > MaxTextureCount)
283303 {
284304 NextBatch ();
285305 slot = 0 ;
286306 }
287307 s_Data->TextureBindList [slot] = gpuTex;
288308 s_Data->TextureCount ++;
289- s_Data->QuadBufferPtr ->a_TexSlot = slot;
290309 }
291310 else
292311 {
293- s_Data-> QuadBufferPtr -> a_TexSlot = static_cast <int >(it - s_Data->TextureBindList .begin ());
312+ slot = static_cast <int >(it - s_Data->TextureBindList .begin ());
294313 }
295314 }
296315
297316 auto mat = s_Data->ViewProjectionMatrix * transform;
298317
299- s_Data->QuadBufferPtr -> a_Position0 = Vector3 (mat * Vector4 (-0.5 , -0.5 , 0.0 , 1.0 ));
300- s_Data->QuadBufferPtr -> a_Position1 = Vector3 (mat * Vector4 ( 0.5 , -0.5 , 0.0 , 1.0 ));
301- s_Data->QuadBufferPtr -> a_Position2 = Vector3 (mat * Vector4 (-0.5 , 0.5 , 0.0 , 1.0 ));
302- s_Data->QuadBufferPtr -> a_Position3 = Vector3 (mat * Vector4 ( 0.5 , 0.5 , 0.0 , 1.0 ));
318+ s_Data->QuadBufferPtr [ 0 ]. a_Position = Vector3 (mat * Vector4 (-0.5 , -0.5 , 0.0 , 1.0 ));
319+ s_Data->QuadBufferPtr [ 1 ]. a_Position = Vector3 (mat * Vector4 ( 0.5 , -0.5 , 0.0 , 1.0 ));
320+ s_Data->QuadBufferPtr [ 2 ]. a_Position = Vector3 (mat * Vector4 (-0.5 , 0.5 , 0.0 , 1.0 ));
321+ s_Data->QuadBufferPtr [ 3 ]. a_Position = Vector3 (mat * Vector4 ( 0.5 , 0.5 , 0.0 , 1.0 ));
303322
304- s_Data->QuadBufferPtr ->a_Color = props.Tint ;
305- s_Data->QuadBufferPtr ->a_TexCoord = (props.Sprite ->GetType () == TextureType::Master) ? Vector4 (0 , 0 , 1 , 1 ) : std::dynamic_pointer_cast<SubTexture2D>(props.Sprite )->GetRect ();
306- s_Data->QuadBufferPtr ->a_TexRepeat = props.ForceTile ;
323+ Vector4 texCoord = (props.Sprite ->GetType () == TextureType::Master)
324+ ? Vector4 (0 , 0 , 1 , 1 )
325+ : std::dynamic_pointer_cast<SubTexture2D>(props.Sprite )->GetRect ();
326+
327+ s_Data->QuadBufferPtr [0 ].a_TexCoord = Vector2 (texCoord.x , texCoord.y ) + Vector2 (0.0 , texCoord.w );
328+ s_Data->QuadBufferPtr [1 ].a_TexCoord = Vector2 (texCoord.x , texCoord.y ) + Vector2 (texCoord.z , texCoord.w );
329+ s_Data->QuadBufferPtr [2 ].a_TexCoord = Vector2 (texCoord.x , texCoord.y );
330+ s_Data->QuadBufferPtr [3 ].a_TexCoord = Vector2 (texCoord.x , texCoord.y ) + Vector2 (texCoord.z , 0.0 );
331+
332+ for (int i = 0 ; i < 4 ; i++) {
333+ s_Data->QuadBufferPtr [i].a_Color = props.Tint ;
334+ s_Data->QuadBufferPtr [i].a_TexSlot = slot;
335+ s_Data->QuadBufferPtr [i].a_TexRepeat = props.ForceTile ;
336+ // s_Data->QuadBufferPtr[i].a_TexRegion = s_Data->QuadBufferPtr->a_TexCoord;
337+
338+ s_Data->QuadBufferPtr [i].a_LiquidGlass = 0 ;
339+ }
340+
341+ /*
307342
308343 if (props.ForceTile)
309344 {
@@ -330,7 +365,9 @@ namespace OverEngine
330365 s_Data->QuadBufferPtr->a_TexCoord.y += (props.Flip & TextureFlip_X) * s_Data->QuadBufferPtr->a_TexCoord.w;
331366 s_Data->QuadBufferPtr->a_TexCoord.w *= -1 + (int)(!(props.Flip & TextureFlip_Y)) * 2;
332367
333- s_Data->QuadBufferPtr ++;
368+ */
369+
370+ s_Data->QuadBufferPtr += 4 ;
334371 s_Data->QuadCount ++;
335372 s_Statistics.QuadCount ++;
336373 }
0 commit comments