Refactored Graphics to allow Renderer choice at runtime

This commit is contained in:
Noel Berry
2022-02-09 18:49:47 -08:00
parent ae6a2f12d4
commit f5e8de0b11
20 changed files with 1121 additions and 1099 deletions

View File

@ -118,12 +118,12 @@ namespace
};
const VertexFormat format = VertexFormat(
{
{ 0, VertexType::Float2, false },
{ 1, VertexType::Float2, false },
{ 2, VertexType::UByte4, true },
{ 3, VertexType::UByte4, true },
});
{
{ 0, VertexType::Float2, false },
{ 1, VertexType::Float2, false },
{ 2, VertexType::UByte4, true },
{ 3, VertexType::UByte4, true },
});
}
namespace
@ -378,7 +378,7 @@ void Batch::set_texture(const TextureRef& texture)
if (m_batch.texture != texture)
{
m_batch.texture = texture;
m_batch.flip_vertically = App::renderer_features().origin_bottom_left && texture && texture->is_framebuffer();
m_batch.flip_vertically = App::renderer().origin_bottom_left && texture && texture->is_framebuffer();
}
}
@ -392,13 +392,8 @@ void Batch::set_sampler(const TextureSampler& sampler)
void Batch::render(const TargetRef& target)
{
Point size;
if (!target)
size = Point(App::draw_width(), App::draw_height());
else
size = Point(target->width(), target->height());
render(target, Mat4x4f::create_ortho_offcenter(0, (float)size.x, (float)size.y, 0, 0.01f, 1000.0f));
TargetRef ref = (target ? target : App::backbuffer());
render(ref, Mat4x4f::create_ortho_offcenter(0, (float)ref->width(), (float)ref->height(), 0, 0.01f, 1000.0f));
}
void Batch::render(const TargetRef& target, const Mat4x4f& matrix)
@ -414,9 +409,9 @@ void Batch::render(const TargetRef& target, const Mat4x4f& matrix)
if (!m_default_shader)
{
if (App::renderer() == Renderer::OpenGL)
if (App::renderer().type == RendererType::OpenGL)
m_default_shader = Shader::create(opengl_shader_data);
else if (App::renderer() == Renderer::D3D11)
else if (App::renderer().type == RendererType::D3D11)
m_default_shader = Shader::create(d3d11_shader_data);
}

View File

@ -1,12 +1,16 @@
#include <blah/graphics/mesh.h>
#include "../internal/graphics.h"
#include "../internal/renderer.h"
using namespace Blah;
MeshRef Mesh::create()
{
return Graphics::create_mesh();
BLAH_ASSERT_RENDERER();
if (Renderer::instance)
return Renderer::instance->create_mesh();
return MeshRef();
}
VertexFormat::VertexFormat(std::initializer_list<VertexAttribute> attributes, int stride)

View File

@ -1,13 +1,13 @@
#include <blah/graphics/renderpass.h>
#include <blah/common.h>
#include "../internal/graphics.h"
#include "../internal/renderer.h"
using namespace Blah;
RenderPass::RenderPass()
{
blend = BlendMode::Normal;
target = App::backbuffer;
target = App::backbuffer();
mesh = MeshRef();
material = MaterialRef();
has_viewport = false;
@ -23,17 +23,21 @@ RenderPass::RenderPass()
void RenderPass::perform()
{
BLAH_ASSERT_RENDERER();
BLAH_ASSERT(material, "Trying to draw with an invalid Material");
BLAH_ASSERT(material->shader(), "Trying to draw with an invalid Shader");
BLAH_ASSERT(mesh, "Trying to draw with an invalid Mesh");
if (!Renderer::instance)
return;
// copy call
RenderPass pass = *this;
// Validate Backbuffer
if (!pass.target)
{
pass.target = App::backbuffer;
pass.target = App::backbuffer();
Log::warn("Trying to draw with an invalid Target; falling back to Back Buffer");
}
@ -86,5 +90,5 @@ void RenderPass::perform()
pass.scissor = pass.scissor.overlap_rect(Rectf(0, 0, draw_size.x, draw_size.y));
// perform render
Graphics::render(pass);
Renderer::instance->render(pass);
}

View File

@ -1,16 +1,20 @@
#include <blah/graphics/shader.h>
#include <blah/app.h>
#include "../internal/graphics.h"
#include "../internal/renderer.h"
using namespace Blah;
ShaderRef Shader::create(const ShaderData& data)
{
BLAH_ASSERT_RENDERER();
BLAH_ASSERT(data.vertex.length() > 0, "Must provide a Vertex Shader");
BLAH_ASSERT(data.fragment.length() > 0, "Must provide a Fragment Shader");
BLAH_ASSERT(data.hlsl_attributes.size() > 0 || App::renderer() != Renderer::D3D11, "D3D11 Shaders must have hlsl_attributes assigned");
BLAH_ASSERT(data.hlsl_attributes.size() > 0 || App::renderer().type != RendererType::D3D11, "D3D11 Shaders must have hlsl_attributes assigned");
auto shader = Graphics::create_shader(&data);
ShaderRef shader;
if (Renderer::instance)
shader = Renderer::instance->create_shader(&data);
// validate the shader
if (shader)

View File

@ -1,5 +1,5 @@
#include <blah/graphics/target.h>
#include "../internal/graphics.h"
#include "../internal/renderer.h"
using namespace Blah;
@ -10,6 +10,7 @@ TargetRef Target::create(int width, int height)
TargetRef Target::create(int width, int height, const AttachmentFormats& textures)
{
BLAH_ASSERT_RENDERER();
BLAH_ASSERT(width > 0 && height > 0, "Target width and height must be larger than 0");
BLAH_ASSERT(textures.size() > 0, "At least one texture must be provided");
@ -29,7 +30,10 @@ TargetRef Target::create(int width, int height, const AttachmentFormats& texture
BLAH_ASSERT(depth_count <= 1, "Target can only have 1 Depth/Stencil Texture");
BLAH_ASSERT(color_count <= Attachments::capacity - 1, "Exceeded maximum Color texture count");
return Graphics::create_target(width, height, textures.data(), textures.size());
if (Renderer::instance)
return Renderer::instance->create_target(width, height, textures.data(), textures.size());
return TargetRef();
}
TextureRef& Target::texture(int index)

View File

@ -2,7 +2,7 @@
#include <blah/images/image.h>
#include <blah/streams/stream.h>
#include <blah/common.h>
#include "../internal/graphics.h"
#include "../internal/renderer.h"
using namespace Blah;
@ -13,15 +13,21 @@ TextureRef Texture::create(const Image& image)
TextureRef Texture::create(int width, int height, TextureFormat format, unsigned char* data)
{
BLAH_ASSERT_RENDERER();
BLAH_ASSERT(width > 0 && height > 0, "Texture width and height must be larger than 0");
BLAH_ASSERT((int)format > (int)TextureFormat::None && (int)format < (int)TextureFormat::Count, "Invalid texture format");
auto tex = Graphics::create_texture(width, height, format);
if (Renderer::instance)
{
auto tex = Renderer::instance->create_texture(width, height, format);
if (tex && data != nullptr)
tex->set_data(data);
if (tex && data != nullptr)
tex->set_data(data);
return tex;
return tex;
}
return TextureRef();
}
TextureRef Texture::create(Stream& stream)