Sprite Batcher has fields for optional texture/sampler uniform names

This commit is contained in:
Noel Berry 2021-08-06 16:03:44 -07:00
parent ba3c60b6cd
commit 8f9c6aa9ff
4 changed files with 37 additions and 5 deletions

View File

@ -48,7 +48,9 @@ namespace Blah
{
public:
// The name of the Matrix Uniform in the Shader
// The name of the default uniforms to set
const char* texture_uniform;
const char* sampler_uniform;
const char* matrix_uniform;
// Default Sampler, set on clear

View File

@ -65,6 +65,9 @@ namespace Blah
// Gets a pointer to the values of the given Uniform, or nullptr if it doesn't exist.
const float* get_value(const char* name, i64* length = nullptr) const;
// Checks if the shader attached to the material has a uniform value with the given name
bool has_value(const char* name) const;
// Returns the internal Texture buffer
const Vector<TextureRef>& textures() const;

View File

@ -83,7 +83,7 @@ namespace
"};\n"
"Texture2D u_texture : register(t0);\n"
"SamplerState u_sampler : register(s0);\n"
"SamplerState u_texture_sampler : register(s0);\n"
"vs_out vs_main(vs_in input)\n"
"{\n"
@ -99,7 +99,7 @@ namespace
"float4 ps_main(vs_out input) : SV_TARGET\n"
"{\n"
" float4 color = u_texture.Sample(u_sampler, input.texcoord);\n"
" float4 color = u_texture.Sample(u_texture_sampler, input.texcoord);\n"
" return\n"
" input.mask.x * color * input.color + \n"
" input.mask.y * color.a * input.color + \n"
@ -202,6 +202,8 @@ ShaderRef Batch::m_default_shader;
Batch::Batch()
{
texture_uniform = "u_texture";
sampler_uniform = "u_texture_sampler";
matrix_uniform = "u_matrix";
clear();
}
@ -443,12 +445,23 @@ void Batch::render(const TargetRef& target, const Mat4x4& matrix)
void Batch::render_single_batch(RenderPass& pass, const DrawBatch& b, const Mat4x4& matrix)
{
// get the material
pass.material = b.material;
if (!pass.material)
pass.material = m_default_material;
// assign texture & sampler, fallback to whatever the first one is if the names are different
if (pass.material->has_value(texture_uniform))
pass.material->set_texture(texture_uniform, b.texture);
else
pass.material->set_texture(0, b.texture);
pass.material->set_texture(0, b.texture);
pass.material->set_sampler(0, b.sampler);
if (pass.material->has_value(sampler_uniform))
pass.material->set_sampler(sampler_uniform, b.sampler);
else
pass.material->set_sampler(0, b.sampler);
// assign the matrix uniform
pass.material->set_value(matrix_uniform, &matrix.m11, 16);
pass.blend = b.blend;

View File

@ -340,6 +340,20 @@ const float* Material::get_value(const char* name, i64* length) const
return nullptr;
}
bool Material::has_value(const char* name) const
{
BLAH_ASSERT(m_shader, "Material Shader is invalid");
if (name != nullptr && name[0] != '\0')
{
for (auto& uniform : m_shader->uniforms())
if (strcmp(uniform.name, name) == 0)
return true;
}
return false;
}
const Vector<TextureRef>& Material::textures() const
{
return m_textures;