Multiple texture slots working in the quad shader

master
Joey Pollack 3 years ago
parent 96c404c11c
commit 777b4bd2f3

@ -6,7 +6,7 @@
Renderer rewrite:
✔ Batch rendering minimally working @done(22-08-12 19:19)
See if it's possible/better to move the matrix math into the shaders @high
See if it's possible/better to move the matrix math into the shaders @high @done(22-08-15 14:23)
☐ Textures in batch renderer @high
☐ DrawSprite method
☐ Sprite shader

@ -56,7 +56,7 @@ namespace lunarium
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 3 }); // Position
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 2 }); // Tex_coords
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 4 }); // Color
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::INT32, 1 }); // Texture Sampler Index
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // Texture Sampler Index
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 3 }); // translation
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // angle
mQuadData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 3 }); // Scale
@ -66,6 +66,14 @@ namespace lunarium
mQuadData.mRawVertexBuffer = new QuadData::Vertex[mQuadData.MaxVertices];
mQuadData.mRawBufferIndex = 0;
int layout_size = mQuadData.mBufferLayout.GetLayoutSizeInBytes();
int struct_size = sizeof(QuadData::Vertex);
if (struct_size != layout_size)
{
Logger::Error(LogCategory::GRAPHICS,
"Quad::Vertex struct size does not match the vertex layout size! struct: %d, layout: %d", struct_size, layout_size);
}
//mQuadData.mIndexBuffer = new IndexBuffer(mQuadData.MaxIndices * sizeof(u32));
std::string vert_source = File::ReadTextFile("quad.vert");
@ -80,8 +88,9 @@ namespace lunarium
// TODO: INIT DEBUG TEXTURE
u8 data[4] = {255, 128, 0, 0};
mpDebugTexture = Texture::Create(data, 1, 1);
u8 data[4] = {255, 255, 255, 255};
mpWhiteTexture = Texture::Create(data, 1, 1);
mLoadedTextures.push_back(mpWhiteTexture);
// DEBUG: INIT TEST DATA
// mTestData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 2}); // Position
@ -171,52 +180,28 @@ namespace lunarium
/////////////////////////////////////////////////////////////////////
void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle)
{
/*
mTestData.color = color;
//mTestData.mVertexBuffer->Bind();
mTestData.mQuadShader->Use();
Uniform uni { "spriteColor", UniformType::F3, -1 };
mTestData.mQuadShader->SetUniform(uni, color.arr).LogIfFailed(LogCategory::GRAPHICS, "Failed to set uniform for the test shader!");
//glBindVertexArray(mTVAO);
GLfloat vertices[6][2] = {
{ -0.5f, 0.5f, },
{ 0.5f, -0.5f, },
{ -0.5f, -0.5f, },
{ -0.5f, 0.5f, },
{ 0.5f, 0.5f, },
{ 0.5f, -0.5f, }
};
mTestData.mVertexBuffer->PushVertices(vertices, 6);
mTestData.mVertexBuffer->Bind();
// mTestData.mIndexBuffer->Bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
//glDrawArrays(GL_TRIANGLES, 0, 6);
mTestData.mVertexBuffer->Clear();
*/
int texture_slot = 0;
// int texture_slot = -1;
// if (texture)
// {
// for (int i = 0; i < mLoadedTextures.size(); i++)
// {
// if (texture == mLoadedTextures[i])
// {
// texture_slot = i;
// break;
// }
// }
int texture_slot = -1;
if (texture)
{
for (int i = 0; i < mLoadedTextures.size(); i++)
{
if (texture == mLoadedTextures[i])
{
texture_slot = i;
break;
}
}
// if (-1 == texture_slot)
// {
// mLoadedTextures.push_back(texture);
// texture_slot = mLoadedTextures.size() - 1;
// }
// }
if (-1 == texture_slot)
{
mLoadedTextures.push_back(texture);
texture_slot = mLoadedTextures.size() - 1;
}
}
else
{
texture_slot = 0;
}
//unsigned char* vertices_wl = (unsigned char*)vertices; // vertices write location pointer
@ -238,7 +223,7 @@ namespace lunarium
// FIRST
QuadData::Vertex v1;
int vert_size = sizeof(QuadData::Vertex);
v1.pos = /*model * */mQuadData.vert_pos[0];
v1.pos = mQuadData.vert_pos[0];
v1.tex_coord = mQuadData.vert_tex[0];
v1.color = color;
v1.tex_slot = texture_slot;
@ -250,7 +235,7 @@ namespace lunarium
// SECOND
QuadData::Vertex v2;
v2.pos = /*model * */ mQuadData.vert_pos[1];
v2.pos = mQuadData.vert_pos[1];
v2.tex_coord = mQuadData.vert_tex[1];
v2.color = color;
v2.tex_slot = texture_slot;
@ -262,7 +247,7 @@ namespace lunarium
// THIRD
QuadData::Vertex v3;
v3.pos = /*model * */ mQuadData.vert_pos[2];
v3.pos = mQuadData.vert_pos[2];
v3.tex_coord = mQuadData.vert_tex[2];
v3.color = color;
v3.tex_slot = texture_slot;
@ -274,7 +259,7 @@ namespace lunarium
// FOURTH
QuadData::Vertex v4;
v4.pos = /*model * */ mQuadData.vert_pos[3];
v4.pos = mQuadData.vert_pos[3];
v4.tex_coord = mQuadData.vert_tex[3];
v4.color = color;
v4.tex_slot = texture_slot;
@ -309,81 +294,6 @@ namespace lunarium
}
void Renderer2D::DrawQuads(Rectangle* quads, u32 num_quads, Color* pColors)
{
for (int i = 0; i < num_quads; i++)
{
float vertices[40];
unsigned char* vertices_wl = (unsigned char*)vertices; // vertices write location pointer
// glm::mat4 model(1.0f);
// model = glm::translate(model, glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f));
// model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(quads[i].X, quads[i].Y, 0.0f));
model = glm::rotate(model, 1.0f, glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::scale(model, glm::vec3(quads[i].HalfWidth * 2, quads[i].HalfHeight * 2, 1.0f));
// FIRST
QuadData::Vertex v1;
int vert_size = sizeof(QuadData::Vertex);
v1.pos = model * mQuadData.vert_pos[0];
v1.tex_coord = mQuadData.vert_tex[0];
v1.color = pColors[i];
v1.tex_slot = 0;
memcpy(vertices_wl, &v1, vert_size);
vertices_wl += vert_size;
// SECOND
QuadData::Vertex v2;
v2.pos = model * mQuadData.vert_pos[1];
v2.tex_coord = mQuadData.vert_tex[1];
v2.color = pColors[i];
v2.tex_slot = 0;
memcpy(vertices_wl, &v2, vert_size);
vertices_wl += vert_size;
// THIRD
QuadData::Vertex v3;
v3.pos =model * mQuadData.vert_pos[2];
v3.tex_coord = mQuadData.vert_tex[2];
v3.color = pColors[i];
v3.tex_slot = 0;
memcpy(vertices_wl, &v3, vert_size);
vertices_wl += vert_size;
// FOURTH
QuadData::Vertex v4;
v4.pos = model * mQuadData.vert_pos[3];
v4.tex_coord = mQuadData.vert_tex[3];
v4.color = pColors[i];
v4.tex_slot = 0;
memcpy(vertices_wl, &v4, vert_size);
if (!mQuadData.mVertexBuffer->PushVertices(vertices, 4))
{
//Logger::Info(LogCategory::GRAPHICS, "Quad VertexBuffer is full, flushing and retrying");
Flush();
i--;
continue;
}
// INDICES
// if (!mQuadData.mVertexBuffer->PushIndices(mQuadData.indices, 6))
// {
// Logger::Error(LogCategory::GRAPHICS, "Quad IndexBuffer is full - This shouldn't happen because the VertexBuffer should fill up first!");
// return;
// }
mQuadData.mNumQuads++;
mFrameStats.NumTris += 2;
}
}
void Renderer2D::DrawSprite(Texture& image, glm::vec2 position, Color color, float angle)
{
Logger::Warn(LogCategory::GRAPHICS, "Renderer2D::DrawSprite is not yet implemented");
@ -467,7 +377,7 @@ namespace lunarium
//mQuadData.mQuadShader->SetUniform(umodel, (void*)glm::value_ptr(glm::mat4(1.0f))).LogIfFailed(LogCategory::GRAPHICS);
mQuadData.mQuadShader->SetUniform(uprojview, (void*)glm::value_ptr(mpCamera->GetViewProjection())).LogIfFailed(LogCategory::GRAPHICS);
Uniform uni { "spriteColor", UniformType::F3, -1 };
//Uniform uni { "spriteColor", UniformType::F3, -1 };
//Color c = ;
//mQuadData.mQuadShader->SetUniform(uni, Color::Red().arr).LogIfFailed(LogCategory::GRAPHICS, "Failed to set uniform for the test shader!");
@ -477,13 +387,17 @@ namespace lunarium
mFrameStats.DrawCalls++;
}
// Reset drawing data
for (int i = 0; i < mLoadedTextures.size(); i++)
{
mLoadedTextures[i]->Unbind();
}
mLoadedTextures.clear();
mQuadData.mVertexBuffer->Clear();
//mQuadData.mIndexBuffer->Clear();
mQuadData.mNumQuads = 0;
mQuadData.mRawBufferIndex = 0;
// TODO: Add the debug texture back to the map
mLoadedTextures.push_back(mpDebugTexture);
mLoadedTextures.push_back(mpWhiteTexture);
}
}

@ -94,7 +94,7 @@ namespace lunarium
glm::vec3 pos;
glm::vec2 tex_coord;
glm::vec4 color;
int tex_slot;
float tex_slot;
glm::vec3 translation;
float angle;
glm::vec3 scale;
@ -115,7 +115,7 @@ namespace lunarium
FrameStats mFrameStats;
OrthographicCamera* mpCamera;
std::vector<Texture*> mLoadedTextures;
Texture* mpDebugTexture;
Texture* mpWhiteTexture;
Color mClearColor;
/// DEBUG STUFF

@ -1,12 +1,30 @@
#version 450 core
//in vec2 TexCoords;
in vec4 vert_color;
out vec4 color;
layout (location = 0) in vec2 f_tex_coords;
layout (location = 1) in vec4 f_vert_color;
layout (location = 2) flat in int f_tex_index;
//uniform sampler2D image;
uniform vec3 spriteColor;
out vec4 color;
layout (binding = 0) uniform sampler2D textures[32];
void main()
{
color = vert_color; //vec4(spriteColor, 1.0); // * texture(image, TexCoords);
//color = f_vert_color * texture(textures[int(f_tex_index)], f_tex_coords);
//color = vec4(float(f_tex_index) * 0.01, 0.0 , 0.0, 1.0);
color = f_vert_color * texture(textures[f_tex_index], f_tex_coords);
// switch(f_tex_index)
// {
// case 0: color = vec4(1.0, 0.0, 0.0, 1.0); break; //texture(textures[ 0], f_tex_coords); break;
// case 1: color = vec4(0.0, 1.0, 0.0, 1.0); break; //texture(textures[ 1], f_tex_coords); break;
// case 2: color = vec4(0.0, 0.0, 1.0, 1.0); break; //texture(textures[ 1], f_tex_coords); break;
// default: color = vec4(1.0, 1.0, 1.0, 1.0); break;
// }
// if (f_tex_index > 1000)
// {
// color = vec4(1.0, 0.0, 0.0, 1.0);
// }
// else
// {
// color = vec4(0.0, 0.0, 1.0, 1.0);
// }
}

@ -3,24 +3,54 @@
layout(location = 0) in vec3 pos;
layout(location = 1) in vec2 tex_coords;
layout(location = 2) in vec4 color;
layout(location = 3) in int tex_slot;
layout(location = 3) in float tex_slot;
layout(location = 4) in vec3 translation;
layout(location = 5) in float angle;
layout(location = 6) in vec3 scale;
//out vec2 TexCoords;
out vec4 vert_color;
layout (location = 0) out vec2 f_tex_coords;
layout (location = 1) out vec4 f_vert_color;
layout (location = 2) flat out int f_tex_index;
uniform mat4 projview;
void main()
{
vert_color = color;
mat4 ModelTrans = mat4(
vec4( scale.x * cos(angle), scale.x * -sin(angle), 0.0, 0.0),
vec4( scale.y * sin(angle), scale.y * cos(angle), 0.0, 0.0),
vec4( 0.0, 0.0, scale.z, 0.0),
vec4( translation.xyz, 1.0)
);
gl_Position = projview * ModelTrans * vec4(pos, 1.0);
f_tex_index = int(tex_slot);
f_tex_coords = tex_coords;
f_vert_color = color;
// mat4 ModelTrans = mat4(
// vec4( scale.x * cos(angle), scale.x * -sin(angle), 0.0, 0.0),
// vec4( scale.y * sin(angle), scale.y * cos(angle), 0.0, 0.0),
// vec4( 0.0, 0.0, scale.z, 0.0),
// vec4( translation.xyz, 1.0)
// );
mat4 Translation = mat4(
vec4( 1.0, 0.0, 0.0, 0.0),
vec4( 0.0, 1.0, 0.0, 0.0),
vec4( 0.0, 0.0, 1.0, 0.0),
vec4( translation.xyz, 1.0));
mat4 Rotation = mat4(
vec4( cos(angle), -sin(angle), 0.0, 0.0),
vec4( sin(angle), cos(angle), 0.0, 0.0),
vec4( 0.0, 0.0, 1.0, 0.0),
vec4( 0.0, 0.0, 0.0, 1.0));
mat4 Scale = mat4(
vec4(scale.x, 0.0, 0.0, 0.0),
vec4(0.0, scale.y, 0.0, 0.0),
vec4(0.0, 0.0,scale.z, 0.0),
vec4(0.0, 0.0, 0.0, 1.0));
// mat4 model = Translation * RotationScale;
//gl_Position = Translation * vec4(pos, 1.0);
mat4 model = mat4( vec4(1.0, 0.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));
model = model * Translation;
model = model * Rotation;
model = model * Scale;
gl_Position = projview * model * vec4(pos, 1.0);
}

@ -64,8 +64,9 @@ namespace lunarium
void Texture::Bind(u32 slot) const
{
glBindTextureUnit(slot, mGLID);
// glBindTextureUnit(slot, mGLID);
glActiveTexture(slot + GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mGLID);
}
void Texture::Unbind() const

@ -18,6 +18,9 @@
#include <utils/stb/std_image_write.h>
#include <utils/stb/stb_image.h>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <ctime>
@ -49,18 +52,30 @@ namespace lunarium
mFrameBufferTwo = FrameBuffer::Create(1024, 1024); //Core::Graphics().CreateRenderTexture(1024, 1024, 4);
// Load test image
// int w, h, n;
// stbi_set_flip_vertically_on_load(1);
// unsigned char* buffer = stbi_load("LinkToThePast1_sized.png", &w, &h, &n, 0);
int w, h, n;
stbi_set_flip_vertically_on_load(1);
unsigned char* buffer = stbi_load("LinkToThePast1_sized.png", &w, &h, &n, 0);
// TextureFormat format = TextureFormat::RGBA;
// if (n == 3)
// {
// format = TextureFormat::RGB;
// }
// mpTestImageLoad = Texture::Create(buffer, w, h, format);
// mSrcWidth = w;
// mSrcHeight = h;
TextureFormat format = TextureFormat::RGBA;
if (n == 3)
{
format = TextureFormat::RGB;
}
mpTestImageLoad = Texture::Create(buffer, w, h, format);
mSrcWidth = w;
mSrcHeight = h;
//delete[] buffer;
stbi_set_flip_vertically_on_load(1);
buffer = stbi_load("lunarium_test_image.png", &w, &h, &n, 0);
format = TextureFormat::RGBA;
if (n == 3)
{
format = TextureFormat::RGB;
}
mpTestImageLoad2 = Texture::Create(buffer, w, h, format);
//delete[] buffer;
mQuads = new Rectangle[NUM_QUADS];
for (int i = 0; i < NUM_QUADS; i++)
@ -74,9 +89,11 @@ namespace lunarium
}
mQuadColors = new Color[NUM_QUADS];
mQuadTexures = new Texture*[NUM_QUADS];
for (int i = 0; i < NUM_QUADS; i++)
{
mQuadTexures[i] = (i % 2 == 0) ? mpTestImageLoad : mpTestImageLoad2;
mQuadColors[i].R = ((float)(rand() % 10000)) / 10000.0f;
mQuadColors[i].G = ((float)(rand() % 10000)) / 10000.0f;
mQuadColors[i].B = ((float)(rand() % 10000)) / 10000.0f;
@ -198,13 +215,14 @@ namespace lunarium
// g.DrawQuad(Rectangle(300, 300, 150, 150), Color::Red());
// g.DrawQuad(Rectangle(200, 300, 100, 100), Color(0.2f, 0.5f, 0.4f, 1.0f), nullptr, 45.0f);
// //g.DrawFilledEllipse(glm::vec2(600, 300), glm::vec2(100, 150), Color(1.0f, 0.0f, 1.0f, 1.0f), 100);
// // g.DrawString("This is a test of the text renderer!", Rectangle::MakeFromTopLeft(100, 200, mTextBoxWidth, 300),
// // Color(0.0f, 1.0f, 1.0f, 1.0f), 0.5f, g.DefaultFont());
// //mpRenderedImage = Core::GetInstance().EndRenderToTexture();
// g.EndDraw();
mpRenderedImage = mFrameBufferOne->GetTexture();
// mpRenderedImage = mFrameBufferOne->GetTexture();
box_angle += 0.01f;
int w, h;
@ -232,6 +250,7 @@ namespace lunarium
mNumFrames++;
mRenderStats = Core::Graphics().GetFrameStats();
Core::Graphics().ResetFrameStats();
//std::this_thread::sleep_for (std::chrono::seconds(2));
}
void SimpleRenderScene::RenderScene(Renderer2D& g)
@ -264,17 +283,19 @@ namespace lunarium
void SimpleRenderScene::RenderBatchStressTest(Renderer2D& g)
{
for (int i = 0; i < NUM_QUADS; i++)
for (int i = 0; i < mNumQuadsToRender; i++)
{
g.DrawQuad(mQuads[i], mQuadColors[i], nullptr, box_angle + (i / 10.0f));
Texture* t = mUseTextures ? mQuadTexures[i] : nullptr;
g.DrawQuad(mQuads[i], mQuadColors[i], t, box_angle + (i / 10.0f));
}
}
void SimpleRenderScene::DrawStatsGUI()
{
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(400, 300));
ImGui::SetNextWindowSize(ImVec2(400, 600));
ImGui::Begin("RENDER INFO");
ImGui::BeginChild("Per Frame", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true);
ImGui::Text("Per Frame");
@ -282,12 +303,21 @@ namespace lunarium
ImGui::Text("Number of triangles: %d", mRenderStats.NumTris);
ImGui::Text("Draw calls: %d", mRenderStats.DrawCalls);
ImGui::EndChild();
ImGui::BeginChild("General", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true);
ImGui::BeginChild("General", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 4.5f), true);
ImGui::Text("General Info");
ImGui::Separator();
ImGui::Text("Average Render time: %f", mFrameTime/ mNumFrames);
ImGui::Text("Average Render time: %f", Core::GetInstance().GetFrameData().AverageFrameTime);
ImGui::Text("FPS: %d", Core::GetInstance().GetFrameData().CurrentFPS);
ImGui::Text("Frame Number: %d", mNumFrames);
ImGui::EndChild();
ImGui::BeginChild("Settings", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true);
ImGui::Text("Settings");
ImGui::Separator();
ImGui::InputInt("Number of Quads to draw", &mNumQuadsToRender);
ImGui::Checkbox("Draw Textures", &mUseTextures);
ImGui::EndChild();
ImGui::End();
mNumQuadsToRender = Math::ClampI(mNumQuadsToRender, 0, NUM_QUADS);
}
}

@ -35,6 +35,7 @@ namespace lunarium
Sizei mImageSize;
Texture* mpRenderedImage;
Texture* mpTestImageLoad;
Texture* mpTestImageLoad2;
FrameBuffer* mFrameBufferOne;
FrameBuffer* mFrameBufferTwo;
float angle;
@ -50,9 +51,12 @@ namespace lunarium
bool mStressTest;
int mNumQuadsToRender = 500;
bool mUseTextures = false;
const int NUM_QUADS = 50000;
Color* mQuadColors;
Rectangle* mQuads;
Texture** mQuadTexures;
struct GridTestObj
{

Loading…
Cancel
Save