moved Sprite Batcher's default shader to Renderer; no longer a global var

This commit is contained in:
Noel Berry
2022-02-10 01:13:20 -08:00
parent 2619d2d9e0
commit f56e3bb2b2
8 changed files with 167 additions and 216 deletions

View File

@ -72,7 +72,7 @@ namespace Blah
wglDeleteContext_fn delete_context;
wglMakeCurrent_fn make_current;
} gl;
};
} g_platform;
// Win32 File Class
class Win32File : public File
@ -187,8 +187,6 @@ namespace Blah
using namespace Blah;
namespace fs = std::filesystem;
static Blah::Win32Platform g_platform;
bool Platform::init(const Config& config)
{
// clear platform

View File

@ -22,6 +22,9 @@ namespace Blah
// Renderer Features
RendererFeatures features;
// Default Shader for the Batcher
ShaderRef default_batcher_shader;
virtual ~Renderer() = default;
// Initialize the Graphics

View File

@ -20,6 +20,63 @@
namespace Blah
{
const char* d3d11_batch_shader = ""
"cbuffer constants : register(b0)\n"
"{\n"
" row_major float4x4 u_matrix;\n"
"}\n"
"struct vs_in\n"
"{\n"
" float2 position : POS;\n"
" float2 texcoord : TEX;\n"
" float4 color : COL;\n"
" float4 mask : MASK;\n"
"};\n"
"struct vs_out\n"
"{\n"
" float4 position : SV_POSITION;\n"
" float2 texcoord : TEX;\n"
" float4 color : COL;\n"
" float4 mask : MASK;\n"
"};\n"
"Texture2D u_texture : register(t0);\n"
"SamplerState u_texture_sampler : register(s0);\n"
"vs_out vs_main(vs_in input)\n"
"{\n"
" vs_out output;\n"
" output.position = mul(float4(input.position, 0.0f, 1.0f), u_matrix);\n"
" output.texcoord = input.texcoord;\n"
" output.color = input.color;\n"
" output.mask = input.mask;\n"
" return output;\n"
"}\n"
"float4 ps_main(vs_out input) : SV_TARGET\n"
"{\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"
" input.mask.z * input.color;\n"
"}\n";
const ShaderData d3d11_batch_shader_data = {
d3d11_batch_shader,
d3d11_batch_shader,
{
{ "POS", 0 },
{ "TEX", 0 },
{ "COL", 0 },
{ "MASK", 0 },
}
};
class D3D11_Shader;
class Renderer_D3D11 : public Renderer
@ -787,6 +844,9 @@ namespace Blah
}
}
// create default sprite batch shader
default_batcher_shader = Shader::create(d3d11_batch_shader_data);
return true;
}

View File

@ -343,6 +343,51 @@ typedef void (APIENTRY* DEBUGPROC)(GLenum source,
namespace Blah
{
const ShaderData opengl_batch_shader_data = {
// vertex shader
#ifdef __EMSCRIPTEN__
"#version 300 es\n"
#else
"#version 330\n"
#endif
"uniform mat4 u_matrix;\n"
"layout(location=0) in vec2 a_position;\n"
"layout(location=1) in vec2 a_tex;\n"
"layout(location=2) in vec4 a_color;\n"
"layout(location=3) in vec4 a_type;\n"
"out vec2 v_tex;\n"
"out vec4 v_col;\n"
"out vec4 v_type;\n"
"void main(void)\n"
"{\n"
" gl_Position = u_matrix * vec4(a_position.xy, 0, 1);\n"
" v_tex = a_tex;\n"
" v_col = a_color;\n"
" v_type = a_type;\n"
"}",
// fragment shader
#ifdef __EMSCRIPTEN__
"#version 300 es\n"
"precision mediump float;\n"
#else
"#version 330\n"
#endif
"uniform sampler2D u_texture;\n"
"in vec2 v_tex;\n"
"in vec4 v_col;\n"
"in vec4 v_type;\n"
"out vec4 o_color;\n"
"void main(void)\n"
"{\n"
" vec4 color = texture(u_texture, v_tex);\n"
" o_color = \n"
" v_type.x * color * v_col + \n"
" v_type.y * color.a * v_col + \n"
" v_type.z * v_col;\n"
"}"
};
class Renderer_OpenGL : public Renderer
{
public:
@ -1158,6 +1203,9 @@ namespace Blah
features.origin_bottom_left = true;
features.max_texture_size = max_texture_size;
// create the default batch shader
default_batcher_shader = Shader::create(opengl_batch_shader_data);
return true;
}