When utilizing uniform buffers do you have to set the dimensions of the buffer to essentially massive to have the ability to use several types of shaders with totally different variety of parameters. for instance shall we say i’ve a fabric class that could be a pbr materials that has a vec4, and three floats and one other materials class that has 1 float or perhaps 10 params would you create a uniform buffer for every materials or use one, proper now i’ve a renderer that create a uniform buffer with a selected measurement, that will get up to date per body for every materials would this trigger an issue later if i made a decision so as to add a brand new materials with many extra params? When making an attempt to provide every object its personal information to submit and creating an uniform buffer each time a fabric was created the final values set would override the primary however since its within the renderer it doesnt have this downside. this the fabric code a have which is easy
class Materials : public MaterialInterface
{
public:
Materials(const Ref<Shader>& shader, MaterialFlags::Flag flags = 0)
digital ~Materials();
Ref<Shader>& GetShader() override { return m_Shader; }
digital void Bind() override;
digital void UnBind() override;
void SetTexture(const std::string& identify, const Ref<Texture>& texture);
MaterialFlags::Flag m_Flags;
digital uint32_t GetParametersSize() override = 0;
digital ShaderSamplerMetaData& GetTextureParameters() { return m_Textures; };
protected:
Ref<Shader> m_Shader;
//Ref<UniformBuffer> m_MaterialBuffer;
Ref<Texture> m_WhiteTexture;
Ref<Texture> m_BlueTexture;
Ref<Texture> m_BlackTexture;
ShaderSamplerMetaData m_Textures;
personal:
void SendParameterDataToShader();
void SendTextureIDsToShader();
};
struct PBRParameters : MaterialParameters
{
glm::vec4 AlbedoColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
float Metallic = 1.0f;
float Roughness = 0.1f;
float Emissive = 1.0f;
};
class PBRMaterial : public Materials
{
public:
PBRMaterial(const Ref<Shader>& shader, MaterialFlags::Flag flags = 0);
digital MaterialParameters* GetParameters() override { return &m_Parameters; }
digital uint32_t GetParametersSize() override { return sizeof(PBRParameters); }
PBRParameters m_Parameters;
}; struct LineVertex
{
glm::vec3 m_Position;
glm::vec4 m_Color;
};
struct RenderData
{
struct CameraData
{
glm::mat4 m_ViewProjection;
glm::vec3 m_Position;
float m_Time;
};
struct PointLightData
{
struct alignas(16) PointLight
{
glm::vec4 m_Position{};
glm::vec4 m_Color{};
float m_Constant{};
float m_Linear{};
float m_Quadratic{};
};
int m_NumPointLights = 0;
PointLight m_PointLights[100] = {};
static uint32_t GetSize()
{
return (uint32_t)(sizeof(PointLightData) * 100 + sizeof(int));
}
};
CameraData m_CameraBuffer;
PointLightData m_LightBuffer;
Ref<UniformBuffer> CameraUniformBuffer;
Ref<UniformBuffer> TranformUniformBuffer;
Ref<UniformBuffer> LightUniformBuffer;
Ref<UniformBuffer> MaterialUniformBuffer;
};
static RenderData s_RenderData;
void Renderer::Init()
{
RenderCommand::Init();
RenderCommand::SetLineWidth(1.0f);
s_RenderData.CameraUniformBuffer = UniformBuffer::Create(sizeof(RenderData::CameraData), 0);
s_RenderData.TranformUniformBuffer = UniformBuffer::Create(sizeof(glm::mat4), 1);
s_RenderData.LightUniformBuffer = UniformBuffer::Create(RenderData::PointLightData::GetSize(), 3);
s_RenderData.MaterialUniformBuffer = UniformBuffer::Create(sizeof(PBRParameters), 2);
RenderCommand::SetClearColor({ 0.2f, 0.2f, 0.2f, 1.0f });
}
void Renderer::ShutDown()
{
}
void Renderer::BeginScene(Digital camera* digicam, const Remodel& cameraTransform)
{
s_RenderData.m_CameraBuffer.m_ViewProjection = camera->GetProjection() * glm::inverse(cameraTransform.GetTransform());
s_RenderData.m_CameraBuffer.m_Position = cameraTransform.m_Translation;
s_RenderData.m_CameraBuffer.m_Time = Time::GetTime();
s_RenderData.CameraUniformBuffer->SetData(&s_RenderData.m_CameraBuffer, sizeof(RenderData::CameraData));
}
void Renderer::SubmitLightData(Gentle* gentle, const Remodel& lightTransform)
{
if (auto point_light = Forged<PointLight>(gentle))
{
SubmitPointLightData(point_light, lightTransform);
}
}
void Renderer::EndScene()
{
s_RenderData.m_LightBuffer.m_NumPointLights = 0;
}
void Renderer::OnWindowResize(uint32_t width, int32_t top)
{
RenderCommand::SetViewport(width, top);
}
void Renderer::Submit(const Ref<MaterialInterface>& materialinterface, const Ref<VertexArray>& vertexArray, const Remodel& rework)
{
if (materialinterface)
{
materialinterface->Bind();
}
auto transformmatrix = rework.GetTransform();
s_RenderData.TranformUniformBuffer->SetData(&transformmatrix, sizeof(glm::mat4));
auto parameters = materialinterface->GetParameters();
s_RenderData.MaterialUniformBuffer->SetData(parameters, materialinterface->GetParametersSize());
vertexArray->Bind();
RenderCommand::DrawIndexed(vertexArray);
if (materialinterface)
{
materialinterface->UnBind();
}
}
void Renderer::SubmitLightUniformBufferData()
{
s_RenderData.LightUniformBuffer->SetData(&s_RenderData.m_LightBuffer, RenderData::PointLightData::GetSize());
}
void Renderer::SubmitPointLightData(PointLight* pointLight, const Remodel& lightTransform)
{
int index = s_RenderData.m_LightBuffer.m_NumPointLights++;
s_RenderData.m_LightBuffer.m_PointLights[index].m_Constant = pointLight->m_Constant;
s_RenderData.m_LightBuffer.m_PointLights[index].m_Linear = pointLight->m_Linear;
s_RenderData.m_LightBuffer.m_PointLights[index].m_Quadratic = pointLight->m_Quadratic;
s_RenderData.m_LightBuffer.m_PointLights[index].m_Color = glm::vec4(pointLight->m_Color, 0.0f);
s_RenderData.m_LightBuffer.m_PointLights[index].m_Position = glm::vec4(lightTransform.m_Translation, 0.0f);
}