mirror of
https://github.com/NoelFB/blah.git
synced 2025-06-29 19:25:26 +08:00
refactoring rendering backend a little
This commit is contained in:
@ -3,9 +3,9 @@
|
||||
#include <blah/time.h>
|
||||
#include <blah/math/point.h>
|
||||
|
||||
#include <blah/internal/platform.h>
|
||||
#include <blah/internal/graphics.h>
|
||||
#include <blah/internal/input.h>
|
||||
#include <blah/internal/platform_backend.h>
|
||||
#include <blah/internal/graphics_backend.h>
|
||||
#include <blah/internal/input_backend.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
@ -24,7 +24,6 @@ Config::Config()
|
||||
target_framerate = 60;
|
||||
max_updates = 5;
|
||||
|
||||
graphics = GfxAPI::Any;
|
||||
on_startup = nullptr;
|
||||
on_shutdown = nullptr;
|
||||
on_update = nullptr;
|
||||
@ -50,54 +49,43 @@ bool App::run(const Config* c)
|
||||
|
||||
Log::print("Starting Up ...");
|
||||
|
||||
// figure out the graphics api
|
||||
if (app_config.graphics == GfxAPI::Any)
|
||||
{
|
||||
app_config.graphics = Internal::Graphics::pick_api();
|
||||
if (app_config.graphics == GfxAPI::Any)
|
||||
{
|
||||
Log::error("Failed to find a supported graphics api");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// initialize the system
|
||||
if (!Internal::Platform::init(&app_config))
|
||||
if (!PlatformBackend::init(&app_config))
|
||||
{
|
||||
Log::error("Failed to initialize system module");
|
||||
Log::error("Failed to initialize Platform module");
|
||||
return false;
|
||||
}
|
||||
|
||||
// initialize graphics
|
||||
if (!Internal::Graphics::init(app_config.graphics))
|
||||
if (!GraphicsBackend::init())
|
||||
{
|
||||
Log::error("Failed to initialize graphics module");
|
||||
Log::error("Failed to initialize Graphics module");
|
||||
return false;
|
||||
}
|
||||
|
||||
// input
|
||||
Internal::Input::init();
|
||||
InputBackend::init();
|
||||
|
||||
// startup
|
||||
if (app_config.on_startup != nullptr)
|
||||
app_config.on_startup();
|
||||
|
||||
uint64_t time_last = Internal::Platform::time();
|
||||
uint64_t time_last = PlatformBackend::time();
|
||||
uint64_t time_accumulator = 0;
|
||||
|
||||
// display window
|
||||
Internal::Platform::ready();
|
||||
PlatformBackend::ready();
|
||||
|
||||
while (!app_is_exiting)
|
||||
{
|
||||
// poll system events
|
||||
Internal::Platform::frame();
|
||||
PlatformBackend::frame();
|
||||
|
||||
// update at a fixed timerate
|
||||
// TODO: allow a non-fixed step update?
|
||||
{
|
||||
uint64_t time_target = (uint64_t)((1.0f / app_config.target_framerate) * 1000);
|
||||
uint64_t time_curr = Internal::Platform::time();
|
||||
uint64_t time_curr = PlatformBackend::time();
|
||||
uint64_t time_diff = time_curr - time_last;
|
||||
time_last = time_curr;
|
||||
time_accumulator += time_diff;
|
||||
@ -105,9 +93,9 @@ bool App::run(const Config* c)
|
||||
// do not let us run too fast
|
||||
while (time_accumulator < time_target)
|
||||
{
|
||||
Internal::Platform::sleep((int)(time_target - time_accumulator));
|
||||
PlatformBackend::sleep((int)(time_target - time_accumulator));
|
||||
|
||||
time_curr = Internal::Platform::time();
|
||||
time_curr = PlatformBackend::time();
|
||||
time_diff = time_curr - time_last;
|
||||
time_last = time_curr;
|
||||
time_accumulator += time_diff;
|
||||
@ -139,8 +127,8 @@ bool App::run(const Config* c)
|
||||
Time::previous_elapsed = Time::elapsed;
|
||||
Time::elapsed += Time::delta;
|
||||
|
||||
Internal::Input::frame();
|
||||
Internal::Graphics::frame();
|
||||
InputBackend::frame();
|
||||
GraphicsBackend::frame();
|
||||
|
||||
if (app_config.on_update != nullptr)
|
||||
app_config.on_update();
|
||||
@ -149,13 +137,13 @@ bool App::run(const Config* c)
|
||||
|
||||
// render
|
||||
{
|
||||
Internal::Graphics::before_render();
|
||||
GraphicsBackend::before_render();
|
||||
|
||||
if (app_config.on_render != nullptr)
|
||||
app_config.on_render();
|
||||
|
||||
Internal::Graphics::after_render();
|
||||
Internal::Platform::present();
|
||||
GraphicsBackend::after_render();
|
||||
PlatformBackend::present();
|
||||
}
|
||||
|
||||
}
|
||||
@ -166,8 +154,8 @@ bool App::run(const Config* c)
|
||||
if (app_config.on_shutdown != nullptr)
|
||||
app_config.on_shutdown();
|
||||
|
||||
Internal::Graphics::shutdown();
|
||||
Internal::Platform::shutdown();
|
||||
GraphicsBackend::shutdown();
|
||||
PlatformBackend::shutdown();
|
||||
|
||||
// clear static state
|
||||
Log::print("Exited");
|
||||
@ -198,48 +186,48 @@ const Config* App::config()
|
||||
|
||||
const char* App::path()
|
||||
{
|
||||
return Internal::Platform::app_path();
|
||||
return PlatformBackend::app_path();
|
||||
}
|
||||
|
||||
const char* App::user_path()
|
||||
{
|
||||
return Internal::Platform::user_path();
|
||||
return PlatformBackend::user_path();
|
||||
}
|
||||
|
||||
int App::width()
|
||||
{
|
||||
int w, h;
|
||||
Internal::Platform::get_size(&w, &h);
|
||||
PlatformBackend::get_size(&w, &h);
|
||||
return w;
|
||||
}
|
||||
|
||||
int App::height()
|
||||
{
|
||||
int w, h;
|
||||
Internal::Platform::get_size(&w, &h);
|
||||
PlatformBackend::get_size(&w, &h);
|
||||
return h;
|
||||
}
|
||||
|
||||
int App::draw_width()
|
||||
{
|
||||
int w, h;
|
||||
Internal::Platform::get_draw_size(&w, &h);
|
||||
PlatformBackend::get_draw_size(&w, &h);
|
||||
return w;
|
||||
}
|
||||
|
||||
int App::draw_height()
|
||||
{
|
||||
int w, h;
|
||||
Internal::Platform::get_draw_size(&w, &h);
|
||||
PlatformBackend::get_draw_size(&w, &h);
|
||||
return h;
|
||||
}
|
||||
|
||||
float App::content_scale()
|
||||
{
|
||||
return Internal::Platform::get_content_scale();
|
||||
return PlatformBackend::get_content_scale();
|
||||
}
|
||||
|
||||
void App::fullscreen(bool enabled)
|
||||
{
|
||||
Internal::Platform::set_fullscreen(enabled);
|
||||
PlatformBackend::set_fullscreen(enabled);
|
||||
}
|
@ -2,13 +2,6 @@
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
enum class GfxAPI
|
||||
{
|
||||
Any = -1,
|
||||
OpenGL,
|
||||
Count
|
||||
};
|
||||
|
||||
struct Config
|
||||
{
|
||||
const char* name;
|
||||
@ -17,8 +10,6 @@ namespace Blah
|
||||
int max_updates;
|
||||
int target_framerate;
|
||||
|
||||
GfxAPI graphics;
|
||||
|
||||
void (*on_startup)();
|
||||
void (*on_shutdown)();
|
||||
void (*on_update)();
|
||||
|
@ -135,15 +135,20 @@ Batch::~Batch()
|
||||
dispose();
|
||||
}
|
||||
|
||||
void Batch::push_matrix(const Mat3x2& matrix)
|
||||
void Batch::push_matrix(const Mat3x2& matrix, bool absolute)
|
||||
{
|
||||
m_matrix_stack.push_back(m_matrix);
|
||||
m_matrix = matrix * m_matrix;
|
||||
if (absolute)
|
||||
m_matrix = matrix;
|
||||
else
|
||||
m_matrix = matrix * m_matrix;
|
||||
}
|
||||
|
||||
void Batch::pop_matrix()
|
||||
Mat3x2 Batch::pop_matrix()
|
||||
{
|
||||
auto was = m_matrix;
|
||||
m_matrix = m_matrix_stack.pop();
|
||||
return was;
|
||||
}
|
||||
|
||||
void Batch::push_scissor(const Rect& scissor)
|
||||
@ -212,7 +217,7 @@ void Batch::pop_color_mode()
|
||||
|
||||
void Batch::set_texture(const TextureRef& texture)
|
||||
{
|
||||
if (m_batch.elements > 0 && texture != m_batch.texture && m_batch.texture && m_batch.texture->is_valid())
|
||||
if (m_batch.elements > 0 && texture != m_batch.texture && m_batch.texture)
|
||||
{
|
||||
m_batches.push_back(m_batch);
|
||||
m_batch.offset += m_batch.elements;
|
||||
@ -222,14 +227,14 @@ void Batch::set_texture(const TextureRef& texture)
|
||||
if (m_batch.texture != texture)
|
||||
{
|
||||
m_batch.texture = texture;
|
||||
m_batch.flip_vertically = Graphics::info()->origin_bottom_left && texture && texture->is_valid() && texture->is_framebuffer();
|
||||
m_batch.flip_vertically = Graphics::info()->origin_bottom_left && texture && texture->is_framebuffer();
|
||||
}
|
||||
}
|
||||
|
||||
void Batch::render(const FrameBufferRef& target)
|
||||
{
|
||||
Point size;
|
||||
if (!target || !target->is_valid())
|
||||
if (!target)
|
||||
size = Point(App::draw_width(), App::draw_height());
|
||||
else
|
||||
size = Point(target->width(), target->height());
|
||||
@ -245,7 +250,7 @@ void Batch::render(const FrameBufferRef& target, const Mat4x4& matrix)
|
||||
|
||||
// define defaults
|
||||
{
|
||||
if (!m_mesh || !m_mesh->is_valid())
|
||||
if (!m_mesh)
|
||||
{
|
||||
m_mesh = Graphics::create_mesh();
|
||||
m_mesh->vertex_format(attributes, 4, sizeof(Vertex));
|
||||
@ -281,7 +286,7 @@ void Batch::render(const FrameBufferRef& target, const Mat4x4& matrix)
|
||||
void Batch::render_single_batch(RenderCall& call, const DrawBatch& b, const Mat4x4& matrix)
|
||||
{
|
||||
call.material = b.material;
|
||||
if (!call.material || !call.material->is_valid())
|
||||
if (!call.material)
|
||||
call.material = m_default_material;
|
||||
|
||||
call.material->set_texture(texture_uniform, b.texture, 0);
|
||||
@ -635,6 +640,11 @@ void Batch::semi_circle_line(Vec2 center, float start_radians, float end_radians
|
||||
}
|
||||
|
||||
void Batch::circle(const Vec2 center, float radius, int steps, Color color)
|
||||
{
|
||||
circle(center, radius, steps, color, color);
|
||||
}
|
||||
|
||||
void Batch::circle(const Vec2 center, float radius, int steps, Color center_color, Color outer_color)
|
||||
{
|
||||
Vec2 last = Vec2(center.x + radius, center.y);
|
||||
|
||||
@ -643,7 +653,7 @@ void Batch::circle(const Vec2 center, float radius, int steps, Color color)
|
||||
const auto radians = (i / (float)steps) * Calc::TAU;
|
||||
const auto next = Vec2(center.x + Calc::cos(radians) * radius, center.y + Calc::sin(radians) * radius);
|
||||
|
||||
tri(last, next, center, color);
|
||||
tri(last, next, center, outer_color, outer_color, center_color);
|
||||
|
||||
last = next;
|
||||
}
|
||||
@ -761,7 +771,7 @@ void Batch::tex()
|
||||
|
||||
void Batch::tex(const Subtexture& sub, const Vec2& pos, Color color)
|
||||
{
|
||||
if (!sub.texture || !sub.texture->is_valid())
|
||||
if (!sub.texture)
|
||||
{
|
||||
PUSH_QUAD(
|
||||
pos.x + sub.draw_coords[0].x, pos.y + sub.draw_coords[0].y,
|
||||
@ -794,7 +804,7 @@ void Batch::tex(const Subtexture& sub, const Vec2& pos, const Vec2& origin, cons
|
||||
{
|
||||
push_matrix(Mat3x2::create_transform(pos, origin, scale, rotation));
|
||||
|
||||
if (!sub.texture || !sub.texture->is_valid())
|
||||
if (!sub.texture)
|
||||
{
|
||||
PUSH_QUAD(
|
||||
sub.draw_coords[0].x, sub.draw_coords[0].y,
|
||||
|
@ -44,8 +44,8 @@ namespace Blah
|
||||
Batch& operator=(const Batch& other) = delete;
|
||||
~Batch();
|
||||
|
||||
void push_matrix(const Mat3x2& matrix);
|
||||
void pop_matrix();
|
||||
void push_matrix(const Mat3x2& matrix, bool absolute = false);
|
||||
Mat3x2 pop_matrix();
|
||||
void push_scissor(const Rect& scissor);
|
||||
void pop_scissor();
|
||||
void push_blend(const BlendMode& blend);
|
||||
@ -90,6 +90,7 @@ namespace Blah
|
||||
void semi_circle_line(Vec2 center, float start_radians, float end_radians, float radius, int steps, float t, Color color);
|
||||
|
||||
void circle(const Vec2 center, float radius, int steps, Color color);
|
||||
void circle(const Vec2 center, float radius, int steps, Color center_color, Color outer_color);
|
||||
void circle_line(const Vec2 center, float raidus, float t, int steps, Color color);
|
||||
|
||||
void quad(const Vec2& pos0, const Vec2& pos1, const Vec2& pos2, const Vec2& pos3, Color color);
|
||||
|
@ -24,7 +24,7 @@ void Subtexture::update()
|
||||
draw_coords[3].x = -frame.x;
|
||||
draw_coords[3].y = -frame.y + source.h;
|
||||
|
||||
if (texture && texture->is_valid())
|
||||
if (texture)
|
||||
{
|
||||
float uvx = 1.0f / (float)texture->width();
|
||||
float uvy = 1.0f / (float)texture->height();
|
||||
|
@ -1,31 +1,31 @@
|
||||
#include <blah/filesystem.h>
|
||||
#include <blah/internal/platform.h>
|
||||
#include <blah/internal/platform_backend.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
bool File::exists(const FilePath& path)
|
||||
{
|
||||
return Internal::Platform::file_exists(path.cstr());
|
||||
return PlatformBackend::file_exists(path.cstr());
|
||||
}
|
||||
|
||||
bool File::remove(const FilePath& path)
|
||||
{
|
||||
return Internal::Platform::file_delete(path.cstr());
|
||||
return PlatformBackend::file_delete(path.cstr());
|
||||
}
|
||||
|
||||
bool Directory::create(const FilePath& path)
|
||||
{
|
||||
return Internal::Platform::dir_create(path.cstr());
|
||||
return PlatformBackend::dir_create(path.cstr());
|
||||
}
|
||||
|
||||
bool Directory::exists(const FilePath& path)
|
||||
{
|
||||
return Internal::Platform::dir_exists(path.cstr());
|
||||
return PlatformBackend::dir_exists(path.cstr());
|
||||
}
|
||||
|
||||
bool Directory::remove(const FilePath& path)
|
||||
{
|
||||
return Internal::Platform::dir_delete(path.cstr());
|
||||
return PlatformBackend::dir_delete(path.cstr());
|
||||
}
|
||||
|
||||
Vector<FilePath> Directory::enumerate(const FilePath& path, bool recursive)
|
||||
@ -33,7 +33,7 @@ Vector<FilePath> Directory::enumerate(const FilePath& path, bool recursive)
|
||||
Vector<FilePath> list;
|
||||
|
||||
// get files
|
||||
Internal::Platform::dir_enumerate(list, path.cstr(), recursive);
|
||||
PlatformBackend::dir_enumerate(list, path.cstr(), recursive);
|
||||
|
||||
// normalize path names
|
||||
for (auto& it : list)
|
||||
@ -44,7 +44,7 @@ Vector<FilePath> Directory::enumerate(const FilePath& path, bool recursive)
|
||||
|
||||
void Directory::explore(const FilePath& path)
|
||||
{
|
||||
Internal::Platform::dir_explore(path);
|
||||
PlatformBackend::dir_explore(path);
|
||||
}
|
||||
|
||||
FilePath Path::get_file_name(const FilePath& path)
|
||||
|
@ -19,17 +19,17 @@ namespace Blah
|
||||
// Gets the list of Attachments from the FrameBuffer
|
||||
virtual const Attachments& attachments() const = 0;
|
||||
|
||||
// Gets the Attachment at a given index from the FrameBuffer
|
||||
virtual TextureRef& attachment(int index) = 0;
|
||||
|
||||
// Gets the Attachment at a given index from the FrameBuffer
|
||||
virtual const TextureRef& attachment(int index) const = 0;
|
||||
|
||||
// Gets the width of the FrameBuffer
|
||||
virtual int width() const = 0;
|
||||
|
||||
// Gets the height of the FrameBuffer
|
||||
virtual int height() const = 0;
|
||||
|
||||
// Returns true if the FrameBuffer is valid
|
||||
virtual bool is_valid() const = 0;
|
||||
|
||||
// Destroys the given FrameBuffer
|
||||
virtual void dispose() = 0;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<FrameBuffer> FrameBufferRef;
|
||||
|
@ -5,27 +5,12 @@
|
||||
#include <blah/graphics/material.h>
|
||||
#include <blah/graphics/shader.h>
|
||||
#include <blah/log.h>
|
||||
#include <blah/internal/graphics.h>
|
||||
#include <blah/internal/graphics_backend.h>
|
||||
#include <blah/images/image.h>
|
||||
#include <string.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
namespace
|
||||
{
|
||||
// active graphics device
|
||||
Internal::GraphicsDevice* device;
|
||||
|
||||
// active graphics device info
|
||||
Internal::GraphicsDeviceInfo* device_info;
|
||||
|
||||
// list of possible device info
|
||||
Internal::GraphicsDeviceInfo* device_options[] =
|
||||
{
|
||||
&Internal::OpenGL_DeviceInfo
|
||||
};
|
||||
}
|
||||
|
||||
const BlendMode BlendMode::Normal = BlendMode(
|
||||
BlendOp::Add,
|
||||
BlendFactor::One,
|
||||
@ -37,74 +22,25 @@ const BlendMode BlendMode::Normal = BlendMode(
|
||||
0xffffffff
|
||||
);
|
||||
|
||||
GfxAPI Internal::Graphics::pick_api()
|
||||
{
|
||||
for (int i = 0; i < (int)GfxAPI::Count; i++)
|
||||
{
|
||||
if (device_options[i]->supported())
|
||||
return device_options[i]->api;
|
||||
}
|
||||
|
||||
return GfxAPI::Any;
|
||||
}
|
||||
|
||||
bool Internal::Graphics::init(GfxAPI api)
|
||||
{
|
||||
for (int i = 0; i < (int)GfxAPI::Count; i++)
|
||||
{
|
||||
if (device_options[i]->api == api)
|
||||
{
|
||||
device_info = device_options[i];
|
||||
device = device_info->create();
|
||||
if (device != nullptr)
|
||||
{
|
||||
device->startup();
|
||||
if (device->valid)
|
||||
break;
|
||||
|
||||
device_info->destroy(device);
|
||||
device = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return device != nullptr && device->valid;
|
||||
}
|
||||
const BlendMode BlendMode::Subtract = BlendMode(
|
||||
BlendOp::ReverseSubtract,
|
||||
BlendFactor::One,
|
||||
BlendFactor::One,
|
||||
BlendOp::Add,
|
||||
BlendFactor::One,
|
||||
BlendFactor::One,
|
||||
BlendMask::RGBA,
|
||||
0xffffffff
|
||||
);
|
||||
|
||||
const GraphicsInfo* Graphics::info()
|
||||
{
|
||||
if (device == nullptr || !device->valid)
|
||||
return nullptr;
|
||||
return &device->info;
|
||||
return GraphicsBackend::info();
|
||||
}
|
||||
|
||||
void Internal::Graphics::shutdown()
|
||||
GraphicsRenderer Graphics::renderer()
|
||||
{
|
||||
if (device != nullptr && device_info != nullptr)
|
||||
{
|
||||
device->shutdown();
|
||||
device_info->destroy(device);
|
||||
device = nullptr;
|
||||
device_info = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Internal::Graphics::frame()
|
||||
{
|
||||
if (device != nullptr && device->valid)
|
||||
device->update();
|
||||
}
|
||||
|
||||
void Internal::Graphics::before_render()
|
||||
{
|
||||
if (device != nullptr && device->valid)
|
||||
device->before_render();
|
||||
}
|
||||
|
||||
void Internal::Graphics::after_render()
|
||||
{
|
||||
if (device != nullptr && device->valid)
|
||||
device->after_render();
|
||||
return GraphicsBackend::renderer();
|
||||
}
|
||||
|
||||
TextureRef Graphics::create_texture(const Image& image)
|
||||
@ -125,9 +61,8 @@ TextureRef Graphics::create_texture(int width, int height, TextureFormat format)
|
||||
{
|
||||
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");
|
||||
BLAH_ASSERT(device != nullptr && device->valid, "The graphics device has not been created");
|
||||
|
||||
return device->create_texture(width, height, TextureFilter::Linear, TextureWrap::Repeat, TextureWrap::Repeat, format);
|
||||
return GraphicsBackend::create_texture(width, height, TextureFilter::Linear, TextureWrap::Repeat, TextureWrap::Repeat, format);
|
||||
}
|
||||
|
||||
TextureRef Graphics::create_texture(Stream& stream)
|
||||
@ -171,31 +106,27 @@ FrameBufferRef Graphics::create_framebuffer(int width, int height, const Texture
|
||||
BLAH_ASSERT(attachment_count > 0, "At least one attachment must be provided");
|
||||
for (int i = 0; i < attachment_count; i++)
|
||||
BLAH_ASSERT((int)attachments[i] > (int)TextureFormat::None && (int)attachments[i] < (int)TextureFormat::Count, "Invalid texture format");
|
||||
BLAH_ASSERT(device != nullptr && device->valid, "The graphics device has not been created");
|
||||
|
||||
return device->create_framebuffer(width, height, attachments, attachment_count);
|
||||
return GraphicsBackend::create_framebuffer(width, height, attachments, attachment_count);
|
||||
}
|
||||
|
||||
ShaderRef Graphics::create_shader(const ShaderData* data)
|
||||
{
|
||||
BLAH_ASSERT(device != nullptr && device->valid, "The graphics device has not been created");
|
||||
return device->create_shader(data);
|
||||
return GraphicsBackend::create_shader(data);
|
||||
}
|
||||
|
||||
MaterialRef Graphics::create_material(const ShaderRef& shader)
|
||||
{
|
||||
BLAH_ASSERT(device != nullptr && device->valid, "The graphics device has not been created");
|
||||
BLAH_ASSERT(shader && shader->is_valid(), "The provided shader is invalid");
|
||||
BLAH_ASSERT(shader, "The provided shader is invalid");
|
||||
|
||||
// TODO:
|
||||
// use a pool for Materials
|
||||
// use a pool for Materials?
|
||||
return MaterialRef(new Material(shader));
|
||||
}
|
||||
|
||||
MeshRef Graphics::create_mesh()
|
||||
{
|
||||
BLAH_ASSERT(device != nullptr && device->valid, "Graphics device has not been created");
|
||||
return device->create_mesh();
|
||||
return GraphicsBackend::create_mesh();
|
||||
}
|
||||
|
||||
RenderCall::RenderCall()
|
||||
@ -217,36 +148,27 @@ RenderCall::RenderCall()
|
||||
|
||||
void Graphics::render(const RenderCall& render_call)
|
||||
{
|
||||
BLAH_ASSERT(device != nullptr && device->valid, "Graphics device has not been created");
|
||||
|
||||
// Validate Material
|
||||
if (!render_call.material || !render_call.material->is_valid())
|
||||
if (!render_call.material)
|
||||
{
|
||||
Log::warn("Trying to draw with an invalid Material");
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate Shader
|
||||
if (!render_call.material->shader() || !render_call.material->shader()->is_valid())
|
||||
if (!render_call.material->shader())
|
||||
{
|
||||
Log::warn("Trying to draw with an invalid Shader");
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate Mesh
|
||||
if (!render_call.mesh || !render_call.mesh->is_valid())
|
||||
if (!render_call.mesh)
|
||||
{
|
||||
Log::warn("Trying to draw with an invalid Mesh");
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate FrameBuffer
|
||||
if (render_call.target && !render_call.target->is_valid())
|
||||
{
|
||||
Log::warn("Trying to draw with an invalid FrameBuffer");
|
||||
return;
|
||||
}
|
||||
|
||||
// copy call
|
||||
RenderCall call = render_call;
|
||||
|
||||
@ -330,11 +252,10 @@ void Graphics::render(const RenderCall& render_call)
|
||||
call.scissor.h = 0;
|
||||
}
|
||||
|
||||
device->render(&call);
|
||||
GraphicsBackend::render(&call);
|
||||
}
|
||||
|
||||
void Graphics::clear(const FrameBufferRef& target, uint32_t rgba)
|
||||
{
|
||||
BLAH_ASSERT(device != nullptr && device->valid, "Graphics device has not been created");
|
||||
device->clear(target, rgba);
|
||||
GraphicsBackend::clear(target, rgba);
|
||||
}
|
@ -32,9 +32,17 @@ namespace Blah
|
||||
class Mesh;
|
||||
typedef std::shared_ptr<Mesh> MeshRef;
|
||||
|
||||
enum class GraphicsRenderer
|
||||
{
|
||||
None = -1,
|
||||
OpenGL,
|
||||
D3D11,
|
||||
Metal,
|
||||
Count
|
||||
};
|
||||
|
||||
struct GraphicsInfo
|
||||
{
|
||||
GfxAPI api = GfxAPI::Any;
|
||||
bool instancing = false;
|
||||
bool origin_bottom_left = false;
|
||||
int max_texture_size = 0;
|
||||
@ -174,6 +182,7 @@ namespace Blah
|
||||
inline bool operator!=(const BlendMode& rhs) const { return !(*this == rhs); }
|
||||
|
||||
static const BlendMode Normal;
|
||||
static const BlendMode Subtract;
|
||||
};
|
||||
|
||||
enum class UniformType
|
||||
@ -300,6 +309,9 @@ namespace Blah
|
||||
// Gets graphics information from the graphics device
|
||||
const GraphicsInfo* info();
|
||||
|
||||
// Gets the Renderer implementation type
|
||||
GraphicsRenderer renderer();
|
||||
|
||||
// Creates a new Texture.
|
||||
// If the Texture creation fails, it will return an invalid TextureRef.
|
||||
TextureRef create_texture(const Image& image);
|
||||
|
@ -28,16 +28,8 @@ namespace
|
||||
|
||||
Material::Material(const ShaderRef& shader)
|
||||
{
|
||||
BLAH_ASSERT(shader, "Material is being created with an invalid shader");
|
||||
m_shader = shader;
|
||||
m_data = nullptr;
|
||||
m_disposed = false;
|
||||
|
||||
// invalid shader
|
||||
if (!m_shader || !m_shader->is_valid())
|
||||
{
|
||||
m_disposed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
Uniforms uniforms = shader->uniforms();
|
||||
StackVector<size_t, BLAH_UNIFORMS> float_offsets;
|
||||
@ -59,16 +51,9 @@ Material::Material(const ShaderRef& shader)
|
||||
float_size += calc_uniform_size(uniform);
|
||||
}
|
||||
|
||||
m_data = new float[float_size];
|
||||
memset(m_data, 0, sizeof(float) * float_size);
|
||||
|
||||
m_data.expand(float_size);
|
||||
for (auto& it : float_offsets)
|
||||
m_floats.push_back(m_data + it);
|
||||
}
|
||||
|
||||
Material::~Material()
|
||||
{
|
||||
dispose();
|
||||
m_floats.push_back(m_data.begin() + it);
|
||||
}
|
||||
|
||||
const ShaderRef Material::shader() const
|
||||
@ -78,8 +63,7 @@ const ShaderRef Material::shader() const
|
||||
|
||||
void Material::set_texture(const char* name, const TextureRef& texture, int index)
|
||||
{
|
||||
BLAH_ASSERT(!m_disposed, "Material has been disposed");
|
||||
BLAH_ASSERT(m_shader && m_shader->is_valid(), "Material Shader is invalid");
|
||||
BLAH_ASSERT(m_shader, "Material Shader is invalid");
|
||||
|
||||
if (m_textures.size() > 0)
|
||||
{
|
||||
@ -106,8 +90,7 @@ void Material::set_texture(const char* name, const TextureRef& texture, int inde
|
||||
|
||||
TextureRef Material::get_texture(const char* name, int index) const
|
||||
{
|
||||
BLAH_ASSERT(!m_disposed, "Material has been disposed");
|
||||
BLAH_ASSERT(m_shader && m_shader->is_valid(), "Material Shader is invalid");
|
||||
BLAH_ASSERT(m_shader, "Material Shader is invalid");
|
||||
|
||||
int offset = 0;
|
||||
for (auto& uniform : m_shader->uniforms())
|
||||
@ -129,8 +112,7 @@ TextureRef Material::get_texture(const char* name, int index) const
|
||||
|
||||
TextureRef Material::get_texture(int slot, int index) const
|
||||
{
|
||||
BLAH_ASSERT(!m_disposed, "Material has been disposed");
|
||||
BLAH_ASSERT(m_shader && m_shader->is_valid(), "Material Shader is invalid");
|
||||
BLAH_ASSERT(m_shader, "Material Shader is invalid");
|
||||
|
||||
int offset = 0;
|
||||
int s = 0;
|
||||
@ -155,8 +137,7 @@ TextureRef Material::get_texture(int slot, int index) const
|
||||
|
||||
void Material::set_value(const char* name, const float* value, int64_t length)
|
||||
{
|
||||
BLAH_ASSERT(!m_disposed, "Material has been disposed");
|
||||
BLAH_ASSERT(m_shader && m_shader->is_valid(), "Material Shader is invalid");
|
||||
BLAH_ASSERT(m_shader, "Material Shader is invalid");
|
||||
BLAH_ASSERT(length >= 0, "Length must be >= 0");
|
||||
|
||||
int index = 0;
|
||||
@ -186,8 +167,7 @@ void Material::set_value(const char* name, const float* value, int64_t length)
|
||||
|
||||
const float* Material::get_value(const char* name, int64_t* length) const
|
||||
{
|
||||
BLAH_ASSERT(!m_disposed, "Material has been disposed");
|
||||
BLAH_ASSERT(m_shader && m_shader->is_valid(), "Material Shader is invalid");
|
||||
BLAH_ASSERT(m_shader, "Material Shader is invalid");
|
||||
|
||||
int index = 0;
|
||||
for (auto& uniform : m_shader->uniforms())
|
||||
@ -212,8 +192,7 @@ const float* Material::get_value(const char* name, int64_t* length) const
|
||||
|
||||
const float* Material::get_value(int slot, int64_t* length) const
|
||||
{
|
||||
BLAH_ASSERT(!m_disposed, "Material has been disposed");
|
||||
BLAH_ASSERT(m_shader && m_shader->is_valid(), "Material Shader is invalid");
|
||||
BLAH_ASSERT(m_shader, "Material Shader is invalid");
|
||||
|
||||
int index = 0;
|
||||
int s = 0;
|
||||
@ -233,21 +212,7 @@ const float* Material::get_value(int slot, int64_t* length) const
|
||||
s++;
|
||||
}
|
||||
|
||||
Log::warn("No Uniform [%i] exists", slot);
|
||||
*length = 0;
|
||||
return nullptr;
|
||||
Log::warn("No Uniform [%i] exists", slot);
|
||||
}
|
||||
|
||||
bool Material::is_valid() const
|
||||
{
|
||||
return !m_disposed && m_shader && m_shader->is_valid();
|
||||
}
|
||||
|
||||
void Material::dispose()
|
||||
{
|
||||
delete[] m_data;
|
||||
m_data = nullptr;
|
||||
m_shader.reset();
|
||||
m_textures.clear();
|
||||
m_floats.clear();
|
||||
}
|
@ -10,8 +10,6 @@ namespace Blah
|
||||
{
|
||||
public:
|
||||
Material(const ShaderRef& shader);
|
||||
~Material();
|
||||
|
||||
Material(const Material& src) = delete;
|
||||
Material(Material&& src) = delete;
|
||||
Material& operator=(const Material& src) = delete;
|
||||
@ -48,18 +46,11 @@ namespace Blah
|
||||
// is a float2, and there are 4 elements, the length should be 8.
|
||||
const float* get_value(int slot, int64_t* length = nullptr) const;
|
||||
|
||||
// Returns true if the Material is valid
|
||||
bool is_valid() const;
|
||||
|
||||
// Destroys the Material
|
||||
void dispose();
|
||||
|
||||
private:
|
||||
ShaderRef m_shader;
|
||||
Vector<TextureRef> m_textures;
|
||||
Vector<float*> m_floats;
|
||||
float* m_data;
|
||||
bool m_disposed;
|
||||
Vector<float> m_data;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<Material> MaterialRef;
|
||||
|
@ -36,12 +36,6 @@ namespace Blah
|
||||
// Gets the instance count of the Mesh
|
||||
virtual int64_t instance_count() const = 0;
|
||||
|
||||
// Returns true if the Mesh is valid
|
||||
virtual bool is_valid() const = 0;
|
||||
|
||||
// Destroys the given Mesh
|
||||
virtual void dispose() = 0;
|
||||
|
||||
protected:
|
||||
virtual void vertex_format_internal(const VertexAttribute* attributes, int count, int stride) = 0;
|
||||
virtual void instance_format_internal(const VertexAttribute* attributes, int count, int stride) = 0;
|
||||
|
@ -24,12 +24,6 @@ namespace Blah
|
||||
|
||||
// Gets a list of Shader Attributes from Shader
|
||||
virtual const Attributes& attributes() const = 0;
|
||||
|
||||
// Returns true if the Shader is valid
|
||||
virtual bool is_valid() const = 0;
|
||||
|
||||
// Destroys the given Shader
|
||||
virtual void dispose() = 0;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<Shader> ShaderRef;
|
||||
|
@ -45,12 +45,6 @@ namespace Blah
|
||||
|
||||
// Returns true if the Texture is part of a FrameBuffer
|
||||
virtual bool is_framebuffer() const = 0;
|
||||
|
||||
// Returns true if the Texture
|
||||
virtual bool is_valid() const = 0;
|
||||
|
||||
// Destroys the given Texture
|
||||
virtual void dispose() = 0;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<Texture> TextureRef;
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include <blah/time.h>
|
||||
#include <blah/log.h>
|
||||
#include <blah/math/point.h>
|
||||
#include <blah/internal/input.h>
|
||||
#include <blah/internal/input_backend.h>
|
||||
#include <string.h>
|
||||
|
||||
using namespace Blah;
|
||||
@ -17,7 +17,7 @@ namespace
|
||||
ControllerState g_empty_controller;
|
||||
}
|
||||
|
||||
void Internal::Input::init()
|
||||
void InputBackend::init()
|
||||
{
|
||||
g_empty_controller.name = "Disconnected";
|
||||
for (int i = 0; i < Blah::Input::max_controllers; i++)
|
||||
@ -28,7 +28,7 @@ void Internal::Input::init()
|
||||
g_next_state = g_empty_state;
|
||||
}
|
||||
|
||||
void Internal::Input::frame()
|
||||
void InputBackend::frame()
|
||||
{
|
||||
// cycle states
|
||||
g_last_state = g_curr_state;
|
||||
@ -68,7 +68,7 @@ void Internal::Input::frame()
|
||||
}
|
||||
}
|
||||
|
||||
void Internal::Input::on_mouse_move(float x, float y)
|
||||
void InputBackend::on_mouse_move(float x, float y)
|
||||
{
|
||||
g_next_state.mouse.position.x = x;
|
||||
g_next_state.mouse.position.y = y;
|
||||
@ -80,13 +80,13 @@ void Internal::Input::on_mouse_move(float x, float y)
|
||||
g_next_state.mouse.draw_position.y = (y / (float)size.y) * draw.y;
|
||||
}
|
||||
|
||||
void Internal::Input::on_mouse_screen_move(float x, float y)
|
||||
void InputBackend::on_mouse_screen_move(float x, float y)
|
||||
{
|
||||
g_next_state.mouse.screen_position.x = x;
|
||||
g_next_state.mouse.screen_position.y = y;
|
||||
}
|
||||
|
||||
void Internal::Input::on_mouse_down(MouseButton button)
|
||||
void InputBackend::on_mouse_down(MouseButton button)
|
||||
{
|
||||
int i = (int)button;
|
||||
if (i >= 0 && i < Blah::Input::max_mouse_buttons)
|
||||
@ -97,7 +97,7 @@ void Internal::Input::on_mouse_down(MouseButton button)
|
||||
}
|
||||
}
|
||||
|
||||
void Internal::Input::on_mouse_up(MouseButton button)
|
||||
void InputBackend::on_mouse_up(MouseButton button)
|
||||
{
|
||||
int i = (int)button;
|
||||
if (i >= 0 && i < Blah::Input::max_mouse_buttons)
|
||||
@ -107,7 +107,7 @@ void Internal::Input::on_mouse_up(MouseButton button)
|
||||
}
|
||||
}
|
||||
|
||||
void Internal::Input::on_key_down(Key key)
|
||||
void InputBackend::on_key_down(Key key)
|
||||
{
|
||||
int i = (int)key;
|
||||
if (i >= 0 && i < Blah::Input::max_keyboard_keys)
|
||||
@ -118,12 +118,12 @@ void Internal::Input::on_key_down(Key key)
|
||||
}
|
||||
}
|
||||
|
||||
void Internal::Input::on_mouse_wheel(Point wheel)
|
||||
void InputBackend::on_mouse_wheel(Point wheel)
|
||||
{
|
||||
g_next_state.mouse.wheel = wheel;
|
||||
}
|
||||
|
||||
void Internal::Input::on_key_up(Key key)
|
||||
void InputBackend::on_key_up(Key key)
|
||||
{
|
||||
int i = (int)key;
|
||||
if (i >= 0 && i < Blah::Input::max_keyboard_keys)
|
||||
@ -133,12 +133,12 @@ void Internal::Input::on_key_up(Key key)
|
||||
}
|
||||
}
|
||||
|
||||
void Internal::Input::on_text_utf8(const char* text)
|
||||
void InputBackend::on_text_utf8(const char* text)
|
||||
{
|
||||
strncat(g_next_state.keyboard.text, text, Blah::Input::max_text_input);
|
||||
}
|
||||
|
||||
void Internal::Input::on_controller_connect(int index, const char* name, int is_gamepad, int button_count, int axis_count)
|
||||
void InputBackend::on_controller_connect(int index, const char* name, int is_gamepad, int button_count, int axis_count)
|
||||
{
|
||||
if (index < Blah::Input::max_controllers)
|
||||
{
|
||||
@ -152,13 +152,13 @@ void Internal::Input::on_controller_connect(int index, const char* name, int is_
|
||||
}
|
||||
}
|
||||
|
||||
void Internal::Input::on_controller_disconnect(int index)
|
||||
void InputBackend::on_controller_disconnect(int index)
|
||||
{
|
||||
if (index < Blah::Input::max_controllers)
|
||||
g_next_state.controllers[index] = g_empty_controller;
|
||||
}
|
||||
|
||||
void Internal::Input::on_button_down(int index, int button)
|
||||
void InputBackend::on_button_down(int index, int button)
|
||||
{
|
||||
if (index < Blah::Input::max_controllers &&
|
||||
button < Blah::Input::max_controller_buttons &&
|
||||
@ -171,7 +171,7 @@ void Internal::Input::on_button_down(int index, int button)
|
||||
}
|
||||
}
|
||||
|
||||
void Internal::Input::on_button_up(int index, int button)
|
||||
void InputBackend::on_button_up(int index, int button)
|
||||
{
|
||||
if (index < Blah::Input::max_controllers &&
|
||||
button < Blah::Input::max_controller_buttons &&
|
||||
@ -183,7 +183,7 @@ void Internal::Input::on_button_up(int index, int button)
|
||||
}
|
||||
}
|
||||
|
||||
void Internal::Input::on_axis_move(int index, int axis, float value)
|
||||
void InputBackend::on_axis_move(int index, int axis, float value)
|
||||
{
|
||||
if (index < Blah::Input::max_controllers &&
|
||||
axis < Blah::Input::max_controller_axis &&
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include <blah/streams/filestream.h>
|
||||
#include <blah/internal/platform.h>
|
||||
#include <blah/internal/platform_backend.h>
|
||||
#include <blah/log.h>
|
||||
#include <string.h>
|
||||
|
||||
@ -14,7 +14,7 @@ FileStream::FileStream()
|
||||
FileStream::FileStream(const char* path, FileMode mode)
|
||||
: m_mode(mode)
|
||||
{
|
||||
if (!Internal::Platform::file_open(path, &m_handle, mode))
|
||||
if (!PlatformBackend::file_open(path, &m_handle, mode))
|
||||
m_handle = nullptr;
|
||||
}
|
||||
|
||||
@ -36,7 +36,7 @@ FileStream& FileStream::operator=(FileStream&& src) noexcept
|
||||
FileStream::~FileStream()
|
||||
{
|
||||
if (m_handle != nullptr)
|
||||
Internal::Platform::file_close(m_handle);
|
||||
PlatformBackend::file_close(m_handle);
|
||||
}
|
||||
|
||||
int64_t FileStream::length() const
|
||||
@ -44,7 +44,7 @@ int64_t FileStream::length() const
|
||||
if (m_handle == nullptr)
|
||||
return 0;
|
||||
|
||||
return Internal::Platform::file_length(m_handle);
|
||||
return PlatformBackend::file_length(m_handle);
|
||||
}
|
||||
|
||||
int64_t FileStream::position() const
|
||||
@ -52,7 +52,7 @@ int64_t FileStream::position() const
|
||||
if (m_handle == nullptr)
|
||||
return 0;
|
||||
|
||||
return Internal::Platform::file_position(m_handle);
|
||||
return PlatformBackend::file_position(m_handle);
|
||||
}
|
||||
|
||||
int64_t FileStream::seek(int64_t seek_to)
|
||||
@ -60,7 +60,7 @@ int64_t FileStream::seek(int64_t seek_to)
|
||||
if (m_handle == nullptr)
|
||||
return 0;
|
||||
|
||||
return Internal::Platform::file_seek(m_handle, seek_to);
|
||||
return PlatformBackend::file_seek(m_handle, seek_to);
|
||||
}
|
||||
|
||||
int64_t FileStream::read_into(void* ptr, int64_t length)
|
||||
@ -71,7 +71,7 @@ int64_t FileStream::read_into(void* ptr, int64_t length)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Internal::Platform::file_read(m_handle, ptr, length);
|
||||
return PlatformBackend::file_read(m_handle, ptr, length);
|
||||
}
|
||||
|
||||
int64_t FileStream::write_from(const void* ptr, int64_t length)
|
||||
@ -85,13 +85,13 @@ int64_t FileStream::write_from(const void* ptr, int64_t length)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Internal::Platform::file_write(m_handle, ptr, length);
|
||||
return PlatformBackend::file_write(m_handle, ptr, length);
|
||||
}
|
||||
|
||||
void FileStream::close()
|
||||
{
|
||||
if (m_handle != nullptr)
|
||||
Internal::Platform::file_close(m_handle);
|
||||
PlatformBackend::file_close(m_handle);
|
||||
m_handle = nullptr;
|
||||
m_mode = FileMode::None;
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
#include <blah/streams/stream.h>
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/internal/platform.h>
|
||||
#include <string.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
Reference in New Issue
Block a user