cleaning up render pass

This commit is contained in:
Noel Berry 2020-12-24 00:33:38 -08:00
parent 8c0ef3e769
commit dbafc6ec65
3 changed files with 55 additions and 83 deletions

View File

@ -29,7 +29,7 @@ namespace Blah
void after_render(); void after_render();
// Performs a draw call // Performs a draw call
void render(RenderCall* call); void render(const RenderCall& call);
// Clears a buffer // Clears a buffer
void clear(const FrameBufferRef& target, uint32_t rgba); void clear(const FrameBufferRef& target, uint32_t rgba);

View File

@ -1227,11 +1227,11 @@ namespace Blah
return MeshRef(resource); return MeshRef(resource);
} }
void GraphicsBackend::render(RenderCall* call) void GraphicsBackend::render(const RenderCall& call)
{ {
// Bind the Target // Bind the Target
Point size; Point size;
if (!call->target) if (!call.target)
{ {
gl.BindFramebuffer(GL_FRAMEBUFFER, 0); gl.BindFramebuffer(GL_FRAMEBUFFER, 0);
size.x = App::draw_width(); size.x = App::draw_width();
@ -1239,15 +1239,15 @@ namespace Blah
} }
else else
{ {
auto framebuffer = (OpenGL_FrameBuffer*)call->target.get(); auto framebuffer = (OpenGL_FrameBuffer*)call.target.get();
gl.BindFramebuffer(GL_FRAMEBUFFER, framebuffer->gl_id()); gl.BindFramebuffer(GL_FRAMEBUFFER, framebuffer->gl_id());
size.x = call->target->width(); size.x = call.target->width();
size.y = call->target->height(); size.y = call.target->height();
} }
auto shader_ref = call->material->shader(); auto shader_ref = call.material->shader();
auto shader = (OpenGL_Shader*)shader_ref.get(); auto shader = (OpenGL_Shader*)shader_ref.get();
auto mesh = (OpenGL_Mesh*)call->mesh.get(); auto mesh = (OpenGL_Mesh*)call.mesh.get();
// Use the Shader // Use the Shader
// TODO: I don't love how material values are assigned or set here // TODO: I don't love how material values are assigned or set here
@ -1269,7 +1269,7 @@ namespace Blah
{ {
for (int n = 0; n < uniform.array_length; n++) for (int n = 0; n < uniform.array_length; n++)
{ {
auto tex = call->material->get_texture(i, n); auto tex = call.material->get_texture(i, n);
gl.ActiveTexture(GL_TEXTURE0 + texture_slot); gl.ActiveTexture(GL_TEXTURE0 + texture_slot);
@ -1291,60 +1291,60 @@ namespace Blah
// Float // Float
else if (uniform.type == UniformType::Float) else if (uniform.type == UniformType::Float)
{ {
gl.Uniform1fv(location, (GLint)uniform.array_length, (const GLfloat*)call->material->get_value(i)); gl.Uniform1fv(location, (GLint)uniform.array_length, (const GLfloat*)call.material->get_value(i));
} }
// Float2 // Float2
else if (uniform.type == UniformType::Float2) else if (uniform.type == UniformType::Float2)
{ {
gl.Uniform2fv(location, (GLint)uniform.array_length, (const GLfloat*)call->material->get_value(i)); gl.Uniform2fv(location, (GLint)uniform.array_length, (const GLfloat*)call.material->get_value(i));
} }
// Float3 // Float3
else if (uniform.type == UniformType::Float3) else if (uniform.type == UniformType::Float3)
{ {
gl.Uniform3fv(location, (GLint)uniform.array_length, (const GLfloat*)call->material->get_value(i)); gl.Uniform3fv(location, (GLint)uniform.array_length, (const GLfloat*)call.material->get_value(i));
} }
// Float4 // Float4
else if (uniform.type == UniformType::Float4) else if (uniform.type == UniformType::Float4)
{ {
gl.Uniform4fv(location, (GLint)uniform.array_length, (const GLfloat*)call->material->get_value(i)); gl.Uniform4fv(location, (GLint)uniform.array_length, (const GLfloat*)call.material->get_value(i));
} }
// Matrix3x2 // Matrix3x2
else if (uniform.type == UniformType::Mat3x2) else if (uniform.type == UniformType::Mat3x2)
{ {
gl.UniformMatrix3x2fv(location, (GLint)uniform.array_length, 0, (const GLfloat*)call->material->get_value(i)); gl.UniformMatrix3x2fv(location, (GLint)uniform.array_length, 0, (const GLfloat*)call.material->get_value(i));
} }
// Matrix4x4 // Matrix4x4
else if (uniform.type == UniformType::Mat4x4) else if (uniform.type == UniformType::Mat4x4)
{ {
gl.UniformMatrix4fv(location, (GLint)uniform.array_length, 0, (const GLfloat*)call->material->get_value(i)); gl.UniformMatrix4fv(location, (GLint)uniform.array_length, 0, (const GLfloat*)call.material->get_value(i));
} }
} }
} }
// Blend Mode // Blend Mode
{ {
GLenum colorOp = gl_get_blend_func(call->blend.colorOp); GLenum colorOp = gl_get_blend_func(call.blend.colorOp);
GLenum alphaOp = gl_get_blend_func(call->blend.alphaOp); GLenum alphaOp = gl_get_blend_func(call.blend.alphaOp);
GLenum colorSrc = gl_get_blend_factor(call->blend.colorSrc); GLenum colorSrc = gl_get_blend_factor(call.blend.colorSrc);
GLenum colorDst = gl_get_blend_factor(call->blend.colorDst); GLenum colorDst = gl_get_blend_factor(call.blend.colorDst);
GLenum alphaSrc = gl_get_blend_factor(call->blend.alphaSrc); GLenum alphaSrc = gl_get_blend_factor(call.blend.alphaSrc);
GLenum alphaDst = gl_get_blend_factor(call->blend.alphaDst); GLenum alphaDst = gl_get_blend_factor(call.blend.alphaDst);
gl.Enable(GL_BLEND); gl.Enable(GL_BLEND);
gl.BlendEquationSeparate(colorOp, alphaOp); gl.BlendEquationSeparate(colorOp, alphaOp);
gl.BlendFuncSeparate(colorSrc, colorDst, alphaSrc, alphaDst); gl.BlendFuncSeparate(colorSrc, colorDst, alphaSrc, alphaDst);
gl.ColorMask( gl.ColorMask(
((int)call->blend.mask & (int)BlendMask::Red), ((int)call.blend.mask & (int)BlendMask::Red),
((int)call->blend.mask & (int)BlendMask::Green), ((int)call.blend.mask & (int)BlendMask::Green),
((int)call->blend.mask & (int)BlendMask::Blue), ((int)call.blend.mask & (int)BlendMask::Blue),
((int)call->blend.mask & (int)BlendMask::Alpha)); ((int)call.blend.mask & (int)BlendMask::Alpha));
unsigned char r = call->blend.rgba >> 24; unsigned char r = call.blend.rgba >> 24;
unsigned char g = call->blend.rgba >> 16; unsigned char g = call.blend.rgba >> 16;
unsigned char b = call->blend.rgba >> 8; unsigned char b = call.blend.rgba >> 8;
unsigned char a = call->blend.rgba; unsigned char a = call.blend.rgba;
gl.BlendColor( gl.BlendColor(
r / 255.0f, r / 255.0f,
@ -1355,7 +1355,7 @@ namespace Blah
// Depth Function // Depth Function
{ {
if (call->depth == Compare::None) if (call.depth == Compare::None)
{ {
gl.Disable(GL_DEPTH_TEST); gl.Disable(GL_DEPTH_TEST);
} }
@ -1363,7 +1363,7 @@ namespace Blah
{ {
gl.Enable(GL_DEPTH_TEST); gl.Enable(GL_DEPTH_TEST);
switch (call->depth) switch (call.depth)
{ {
case Compare::None: break; case Compare::None: break;
case Compare::Always: case Compare::Always:
@ -1396,7 +1396,7 @@ namespace Blah
// Cull Mode // Cull Mode
{ {
if (call->cull == Cull::None) if (call.cull == Cull::None)
{ {
gl.Disable(GL_CULL_FACE); gl.Disable(GL_CULL_FACE);
} }
@ -1404,9 +1404,9 @@ namespace Blah
{ {
gl.Enable(GL_CULL_FACE); gl.Enable(GL_CULL_FACE);
if (call->cull == Cull::Back) if (call.cull == Cull::Back)
gl.CullFace(GL_BACK); gl.CullFace(GL_BACK);
else if (call->cull == Cull::Front) else if (call.cull == Cull::Front)
gl.CullFace(GL_FRONT); gl.CullFace(GL_FRONT);
else else
gl.CullFace(GL_FRONT_AND_BACK); gl.CullFace(GL_FRONT_AND_BACK);
@ -1415,7 +1415,7 @@ namespace Blah
// Viewport // Viewport
{ {
Rect viewport = call->viewport; Rect viewport = call.viewport;
viewport.y = size.y - viewport.y - viewport.h; viewport.y = size.y - viewport.y - viewport.h;
gl.Viewport((GLint)viewport.x, (GLint)viewport.y, (GLint)viewport.w, (GLint)viewport.h); gl.Viewport((GLint)viewport.x, (GLint)viewport.y, (GLint)viewport.w, (GLint)viewport.h);
@ -1423,13 +1423,13 @@ namespace Blah
// Scissor // Scissor
{ {
if (!call->has_scissor) if (!call.has_scissor)
{ {
gl.Disable(GL_SCISSOR_TEST); gl.Disable(GL_SCISSOR_TEST);
} }
else else
{ {
Rect scissor = call->scissor; Rect scissor = call.scissor;
scissor.y = size.y - scissor.y - scissor.h; scissor.y = size.y - scissor.y - scissor.h;
if (scissor.w < 0) if (scissor.w < 0)
@ -1446,22 +1446,22 @@ namespace Blah
{ {
gl.BindVertexArray(mesh->gl_id()); gl.BindVertexArray(mesh->gl_id());
if (call->instance_count > 0) if (call.instance_count > 0)
{ {
gl.DrawElementsInstanced( gl.DrawElementsInstanced(
GL_TRIANGLES, GL_TRIANGLES,
(GLint)(call->index_count), (GLint)(call.index_count),
GL_UNSIGNED_INT, GL_UNSIGNED_INT,
(void*)(sizeof(int) * call->index_start), (void*)(sizeof(int) * call.index_start),
(GLint)call->instance_count); (GLint)call.instance_count);
} }
else else
{ {
gl.DrawElements( gl.DrawElements(
GL_TRIANGLES, GL_TRIANGLES,
(GLint)(call->index_count), (GLint)(call.index_count),
GL_UNSIGNED_INT, GL_UNSIGNED_INT,
(void*)(sizeof(int) * call->index_start)); (void*)(sizeof(int) * call.index_start));
} }
gl.BindVertexArray(0); gl.BindVertexArray(0);

View File

@ -221,59 +221,31 @@ void Graphics::render(const RenderCall& render_call)
call.instance_count = instance_count; call.instance_count = instance_count;
} }
// get the total drawable size
Point draw_size;
if (!call.target)
draw_size = Point(App::draw_width(), App::draw_height());
else
draw_size = Point(call.target->width(), call.target->height());
// Validate Viewport // Validate Viewport
if (!call.has_viewport) if (!call.has_viewport)
{ {
Point drawSize;
if (!call.target)
drawSize = Point(App::draw_width(), App::draw_height());
else
drawSize = Point(call.target->width(), call.target->height());
call.viewport.x = 0; call.viewport.x = 0;
call.viewport.y = 0; call.viewport.y = 0;
call.viewport.w = (float)drawSize.x; call.viewport.w = (float)draw_size.x;
call.viewport.h = (float)drawSize.y; call.viewport.h = (float)draw_size.y;
} }
else else
{ {
if (call.viewport.x < 0) call.viewport = call.viewport.overlap_rect(Rect(0, 0, draw_size.x, draw_size.y));
{
call.viewport.w += call.viewport.x;
call.viewport.x = 0;
}
if (call.viewport.y < 0)
{
call.viewport.h += call.viewport.y;
call.viewport.y = 0;
}
if (call.viewport.w < 0)
call.viewport.w = 0;
if (call.viewport.h < 0)
call.viewport.h = 0;
} }
// Validate Scissor // Validate Scissor
if (call.has_scissor) if (call.has_scissor)
{ call.scissor = call.scissor.overlap_rect(Rect(0, 0, draw_size.x, draw_size.y));
if (call.scissor.x < 0)
{
call.scissor.w += call.scissor.x;
call.scissor.x = 0;
}
if (call.scissor.y < 0)
{
call.scissor.h += call.scissor.y;
call.scissor.y = 0;
}
if (call.scissor.w < 0)
call.scissor.w = 0;
if (call.scissor.h < 0)
call.scissor.h = 0;
}
GraphicsBackend::render(&call); GraphicsBackend::render(call);
} }
void Graphics::clear(const FrameBufferRef& target, uint32_t rgba) void Graphics::clear(const FrameBufferRef& target, uint32_t rgba)