restructuring internal namespaces & function calls

This commit is contained in:
Noel Berry 2022-11-19 16:15:31 -08:00
parent 2437d5841f
commit c89535e328
11 changed files with 249 additions and 269 deletions

View File

@ -18,6 +18,7 @@ add_library(blah
src/blah_spritefont.cpp src/blah_spritefont.cpp
src/blah_subtexture.cpp src/blah_subtexture.cpp
src/blah_aseprite.cpp src/blah_aseprite.cpp
src/blah_audio.cpp
src/blah_font.cpp src/blah_font.cpp
src/blah_image.cpp src/blah_image.cpp
src/blah_packer.cpp src/blah_packer.cpp

View File

@ -51,5 +51,4 @@ int main()
#### notes #### notes
- There's no Shader abstraction, so you need to swap between GLSL/HLSL depending on the Renderer. - There's no Shader abstraction, so you need to swap between GLSL/HLSL depending on the Renderer.
- Only floatN/mat3x2/mat4x4 uniforms are supported. - Only floatN/mat3x2/mat4x4 uniforms are supported.
- There's no Audio API or backend implementation yet.
- No threaded rendering so it will explode if you try that. - No threaded rendering so it will explode if you try that.

View File

@ -15,10 +15,10 @@ using namespace Blah;
#define BLAH_ASSERT_RUNNING() BLAH_ASSERT(app_is_running, "The App is not running (call App::run)") #define BLAH_ASSERT_RUNNING() BLAH_ASSERT(app_is_running, "The App is not running (call App::run)")
// Internal Platform Pointer // Internal Platform Pointer
Platform* App::Internal::platform = nullptr; Platform* Internal::platform = nullptr;
// Internal Renderer Pointer // Internal Renderer Pointer
Renderer* App::Internal::renderer = nullptr; Renderer* Internal::renderer = nullptr;
// Internal Audio bool // Internal Audio bool
bool Internal::audio_is_init = false; bool Internal::audio_is_init = false;
@ -37,11 +37,11 @@ namespace
void get_drawable_size(int* w, int* h) void get_drawable_size(int* w, int* h)
{ {
// Some renderer implementations might return their own size // Some renderer implementations might return their own size
if (App::Internal::renderer->get_draw_size(w, h)) if (Internal::renderer->get_draw_size(w, h))
return; return;
// otherwise fallback to the platform size // otherwise fallback to the platform size
App::Internal::platform->get_draw_size(w, h); Internal::platform->get_draw_size(w, h);
} }
// A dummy Target that represents the Back Buffer. // A dummy Target that represents the Back Buffer.
@ -57,8 +57,8 @@ namespace
void clear(Color color, float depth, u8 stencil, ClearMask mask) override void clear(Color color, float depth, u8 stencil, ClearMask mask) override
{ {
BLAH_ASSERT_RENDERER(); BLAH_ASSERT_RENDERER();
if (App::Internal::renderer) if (Internal::renderer)
App::Internal::renderer->clear_backbuffer(color, depth, stencil, mask); Internal::renderer->clear_backbuffer(color, depth, stencil, mask);
} }
}; };
} }
@ -87,7 +87,7 @@ bool App::run(const Config* c)
if (app_is_running || c == nullptr || c->width <= 0 || c->height <= 0 || c->max_updates <= 0 || c->target_framerate <= 0) if (app_is_running || c == nullptr || c->width <= 0 || c->height <= 0 || c->max_updates <= 0 || c->target_framerate <= 0)
{ {
App::Internal::shutdown(); Internal::app_shutdown();
return false; return false;
} }
@ -103,14 +103,14 @@ bool App::run(const Config* c)
if (!Internal::platform) if (!Internal::platform)
{ {
Log::error("Failed to create Platform module"); Log::error("Failed to create Platform module");
App::Internal::shutdown(); Internal::app_shutdown();
return false; return false;
} }
if (!Internal::platform->init(app_config)) if (!Internal::platform->init(app_config))
{ {
Log::error("Failed to initialize Platform module"); Log::error("Failed to initialize Platform module");
App::Internal::shutdown(); Internal::app_shutdown();
return false; return false;
} }
} }
@ -132,14 +132,14 @@ bool App::run(const Config* c)
if (Internal::renderer == nullptr) if (Internal::renderer == nullptr)
{ {
Log::error("Renderer module was not found"); Log::error("Renderer module was not found");
App::Internal::shutdown(); Internal::app_shutdown();
return false; return false;
} }
if (!Internal::renderer->init()) if (!Internal::renderer->init())
{ {
Log::error("Failed to initialize Renderer module"); Log::error("Failed to initialize Renderer module");
App::Internal::shutdown(); Internal::app_shutdown();
return false; return false;
} }
} }
@ -149,8 +149,8 @@ bool App::run(const Config* c)
Internal::renderer->set_app_flags(app_flags); Internal::renderer->set_app_flags(app_flags);
// input + poll the platform once // input + poll the platform once
Input::Internal::init(); Internal::input_init();
Input::Internal::step_state(); Internal::input_step_state();
Internal::platform->update(Input::state); Internal::platform->update(Input::state);
// startup // startup
@ -164,16 +164,16 @@ bool App::run(const Config* c)
// Begin main loop // Begin main loop
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
emscripten_set_main_loop(App::Internal::iterate, 0, 1); emscripten_set_main_loop(Internal::iterate, 0, 1);
#else #else
while (!app_is_exiting) while (!app_is_exiting)
App::Internal::iterate(); Internal::app_step();
#endif #endif
// shutdown // shutdown
if (app_config.on_shutdown != nullptr) if (app_config.on_shutdown != nullptr)
app_config.on_shutdown(); app_config.on_shutdown();
App::Internal::shutdown(); Internal::app_shutdown();
return true; return true;
} }
@ -182,25 +182,25 @@ bool App::is_running()
return app_is_running; return app_is_running;
} }
void App::Internal::iterate() void Internal::app_step()
{ {
static const auto step = []() static const auto step = []()
{ {
Input::Internal::step_state(); Internal::input_step_state();
platform->update(Input::state); platform->update(Input::state);
Input::Internal::update_bindings(); Internal::input_step_bindings();
renderer->update(); renderer->update();
if (app_config.on_update != nullptr) if (app_config.on_update != nullptr)
app_config.on_update(); app_config.on_update();
}; };
bool is_fixed_timestep = get_flag(Flags::FixedTimestep); bool is_fixed_timestep = App::get_flag(Flags::FixedTimestep);
// Update in Fixed Timestep // Update in Fixed Timestep
if (is_fixed_timestep) if (is_fixed_timestep)
{ {
u64 time_target = (u64)((1.0 / app_config.target_framerate) * Time::ticks_per_second); u64 time_target = (u64)((1.0 / app_config.target_framerate) * Time::ticks_per_second);
u64 ticks_curr = App::Internal::platform->ticks(); u64 ticks_curr = Internal::platform->ticks();
u64 ticks_diff = ticks_curr - app_time_last; u64 ticks_diff = ticks_curr - app_time_last;
app_time_last = ticks_curr; app_time_last = ticks_curr;
app_time_accumulator += ticks_diff; app_time_accumulator += ticks_diff;
@ -209,9 +209,9 @@ void App::Internal::iterate()
while (app_time_accumulator < time_target) while (app_time_accumulator < time_target)
{ {
int milliseconds = (int)(time_target - app_time_accumulator) / (Time::ticks_per_second / 1000); int milliseconds = (int)(time_target - app_time_accumulator) / (Time::ticks_per_second / 1000);
App::Internal::platform->sleep(milliseconds); Internal::platform->sleep(milliseconds);
ticks_curr = App::Internal::platform->ticks(); ticks_curr = Internal::platform->ticks();
ticks_diff = ticks_curr - app_time_last; ticks_diff = ticks_curr - app_time_last;
app_time_last = ticks_curr; app_time_last = ticks_curr;
app_time_accumulator += ticks_diff; app_time_accumulator += ticks_diff;
@ -250,7 +250,7 @@ void App::Internal::iterate()
// Update with Variable Timestep // Update with Variable Timestep
else else
{ {
u64 ticks_curr = App::Internal::platform->ticks(); u64 ticks_curr = Internal::platform->ticks();
u64 ticks_diff = ticks_curr - app_time_last; u64 ticks_diff = ticks_curr - app_time_last;
app_time_last = ticks_curr; app_time_last = ticks_curr;
app_time_accumulator += ticks_diff; app_time_accumulator += ticks_diff;
@ -285,9 +285,9 @@ void App::Internal::iterate()
Blah::Internal::audio_update(); Blah::Internal::audio_update();
} }
void App::Internal::shutdown() void Internal::app_shutdown()
{ {
Input::Internal::shutdown(); Internal::input_shutdown();
if (renderer) if (renderer)
renderer->shutdown(); renderer->shutdown();
@ -449,5 +449,5 @@ const TargetRef& App::backbuffer()
void System::open_url(const char* url) void System::open_url(const char* url)
{ {
BLAH_ASSERT_RUNNING(); BLAH_ASSERT_RUNNING();
App::Internal::platform->open_url(url); Internal::platform->open_url(url);
} }

View File

@ -282,7 +282,7 @@ void Batch::render(const TargetRef& target, const Mat4x4f& matrix)
if (!m_default_material) if (!m_default_material)
{ {
BLAH_ASSERT_RENDERER(); BLAH_ASSERT_RENDERER();
m_default_material = Material::create(App::Internal::renderer->default_batcher_shader); m_default_material = Material::create(Internal::renderer->default_batcher_shader);
} }
} }

View File

@ -8,8 +8,8 @@ FileRef File::open(const FilePath& path, FileMode mode)
BLAH_ASSERT_PLATFORM(); BLAH_ASSERT_PLATFORM();
FileRef ref; FileRef ref;
if (App::Internal::platform) if (Internal::platform)
ref = App::Internal::platform->file_open(path.cstr(), mode); ref = Internal::platform->file_open(path.cstr(), mode);
if (ref) if (ref)
ref->m_mode = mode; ref->m_mode = mode;
return ref; return ref;
@ -18,16 +18,16 @@ FileRef File::open(const FilePath& path, FileMode mode)
bool File::exists(const FilePath& path) bool File::exists(const FilePath& path)
{ {
BLAH_ASSERT_PLATFORM(); BLAH_ASSERT_PLATFORM();
if (App::Internal::platform) if (Internal::platform)
return App::Internal::platform->file_exists(path.cstr()); return Internal::platform->file_exists(path.cstr());
return false; return false;
} }
bool File::destroy(const FilePath& path) bool File::destroy(const FilePath& path)
{ {
BLAH_ASSERT_PLATFORM(); BLAH_ASSERT_PLATFORM();
if (App::Internal::platform) if (Internal::platform)
return App::Internal::platform->file_delete(path.cstr()); return Internal::platform->file_delete(path.cstr());
return false; return false;
} }
@ -39,24 +39,24 @@ FileMode File::mode() const
bool Directory::create(const FilePath& path) bool Directory::create(const FilePath& path)
{ {
BLAH_ASSERT_PLATFORM(); BLAH_ASSERT_PLATFORM();
if (App::Internal::platform) if (Internal::platform)
return App::Internal::platform->dir_create(path.cstr()); return Internal::platform->dir_create(path.cstr());
return false; return false;
} }
bool Directory::exists(const FilePath& path) bool Directory::exists(const FilePath& path)
{ {
BLAH_ASSERT_PLATFORM(); BLAH_ASSERT_PLATFORM();
if (App::Internal::platform) if (Internal::platform)
return App::Internal::platform->dir_exists(path.cstr()); return Internal::platform->dir_exists(path.cstr());
return false; return false;
} }
bool Directory::destroy(const FilePath& path) bool Directory::destroy(const FilePath& path)
{ {
BLAH_ASSERT_PLATFORM(); BLAH_ASSERT_PLATFORM();
if (App::Internal::platform) if (Internal::platform)
return App::Internal::platform->dir_delete(path.cstr()); return Internal::platform->dir_delete(path.cstr());
return false; return false;
} }
@ -66,9 +66,9 @@ Vector<FilePath> Directory::enumerate(const FilePath& path, bool recursive)
Vector<FilePath> list; Vector<FilePath> list;
if (App::Internal::platform) if (Internal::platform)
{ {
App::Internal::platform->dir_enumerate(list, path.cstr(), recursive); Internal::platform->dir_enumerate(list, path.cstr(), recursive);
for (auto& it : list) for (auto& it : list)
{ {
for (int n = 0; n < it.length(); n ++) for (int n = 0; n < it.length(); n ++)
@ -82,8 +82,8 @@ Vector<FilePath> Directory::enumerate(const FilePath& path, bool recursive)
void Directory::explore(const FilePath& path) void Directory::explore(const FilePath& path)
{ {
BLAH_ASSERT_PLATFORM(); BLAH_ASSERT_PLATFORM();
if (App::Internal::platform) if (Internal::platform)
App::Internal::platform->dir_explore(path); Internal::platform->dir_explore(path);
} }
FilePath Path::get_file_name(const FilePath& path) FilePath Path::get_file_name(const FilePath& path)

View File

@ -88,8 +88,8 @@ ShaderRef Shader::create(const ShaderData& data)
ShaderRef shader; ShaderRef shader;
if (App::Internal::renderer) if (Internal::renderer)
shader = App::Internal::renderer->create_shader(&data); shader = Internal::renderer->create_shader(&data);
// validate the shader // validate the shader
if (shader) if (shader)
@ -130,9 +130,9 @@ TextureRef Texture::create(int width, int height, TextureFormat format, unsigned
BLAH_ASSERT(width > 0 && height > 0, "Texture width and height must be larger than 0"); 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((int)format > (int)TextureFormat::None && (int)format < (int)TextureFormat::Count, "Invalid texture format");
if (App::Internal::renderer) if (Internal::renderer)
{ {
auto tex = App::Internal::renderer->create_texture(width, height, format); auto tex = Internal::renderer->create_texture(width, height, format);
if (tex && data != nullptr) if (tex && data != nullptr)
tex->set_data(data); tex->set_data(data);
@ -194,8 +194,8 @@ 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(depth_count <= 1, "Target can only have 1 Depth/Stencil Texture");
BLAH_ASSERT(color_count <= Attachments::capacity - 1, "Exceeded maximum Color texture count"); BLAH_ASSERT(color_count <= Attachments::capacity - 1, "Exceeded maximum Color texture count");
if (App::Internal::renderer) if (Internal::renderer)
return App::Internal::renderer->create_target(width, height, textures.data(), textures.size()); return Internal::renderer->create_target(width, height, textures.data(), textures.size());
return TargetRef(); return TargetRef();
} }
@ -224,8 +224,8 @@ MeshRef Mesh::create()
{ {
BLAH_ASSERT_RENDERER(); BLAH_ASSERT_RENDERER();
if (App::Internal::renderer) if (Internal::renderer)
return App::Internal::renderer->create_mesh(); return Internal::renderer->create_mesh();
return MeshRef(); return MeshRef();
} }
@ -623,7 +623,7 @@ void DrawCall::perform()
BLAH_ASSERT(material->shader(), "Trying to draw with an invalid Shader"); BLAH_ASSERT(material->shader(), "Trying to draw with an invalid Shader");
BLAH_ASSERT(mesh, "Trying to draw with an invalid Mesh"); BLAH_ASSERT(mesh, "Trying to draw with an invalid Mesh");
if (!App::Internal::renderer) if (!Internal::renderer)
return; return;
// copy call // copy call
@ -685,5 +685,5 @@ void DrawCall::perform()
pass.scissor = pass.scissor.overlap_rect(Rectf(0, 0, draw_size.x, draw_size.y)); pass.scissor = pass.scissor.overlap_rect(Rectf(0, 0, draw_size.x, draw_size.y));
// perform render // perform render
App::Internal::renderer->render(pass); Internal::renderer->render(pass);
} }

View File

@ -24,7 +24,7 @@ InputState Blah::Input::last_state;
float Blah::Input::repeat_delay = 0.35f; float Blah::Input::repeat_delay = 0.35f;
float Blah::Input::repeat_interval = 0.025f; float Blah::Input::repeat_interval = 0.025f;
void Input::Internal::init() void Internal::input_init()
{ {
g_empty_controller.name = "Disconnected"; g_empty_controller.name = "Disconnected";
for (int i = 0; i < Input::max_controllers; i++) for (int i = 0; i < Input::max_controllers; i++)
@ -37,12 +37,12 @@ void Input::Internal::init()
g_sticks = Vector<Ref<StickBinding>>(); g_sticks = Vector<Ref<StickBinding>>();
} }
void Input::Internal::shutdown() void Internal::input_shutdown()
{ {
init(); input_init();
} }
void Input::Internal::step_state() void Internal::input_step_state()
{ {
// cycle states // cycle states
Input::last_state = Input::state; Input::last_state = Input::state;
@ -78,11 +78,11 @@ void Input::Internal::step_state()
} }
// get clipboard // get clipboard
if (App::Internal::platform) if (Internal::platform)
g_clipboard = App::Internal::platform->get_clipboard(); g_clipboard = Internal::platform->get_clipboard();
} }
void Input::Internal::update_bindings() void Internal::input_step_bindings()
{ {
for (int i = 0; i < g_buttons.size(); i++) for (int i = 0; i < g_buttons.size(); i++)
{ {
@ -398,8 +398,8 @@ const String& Input::get_clipboard()
void Input::set_clipboard(const String& text) void Input::set_clipboard(const String& text)
{ {
g_clipboard = text; g_clipboard = text;
if (App::Internal::platform) if (Internal::platform)
App::Internal::platform->set_clipboard(text); Internal::platform->set_clipboard(text);
} }
ButtonBindingRef Input::register_binding(const ButtonBinding& binding_data) ButtonBindingRef Input::register_binding(const ButtonBinding& binding_data)

View File

@ -12,8 +12,8 @@ float Time::pause_timer = 0;
u64 Time::get_ticks() u64 Time::get_ticks()
{ {
if (App::Internal::platform) if (Internal::platform)
return App::Internal::platform->ticks(); return Internal::platform->ticks();
return 0; return 0;
} }

View File

@ -2,45 +2,25 @@
#include "blah_renderer.h" #include "blah_renderer.h"
#include "blah_platform.h" #include "blah_platform.h"
#define BLAH_ASSERT_RENDERER() BLAH_ASSERT(App::Internal::renderer, "Renderer has not been created") #define BLAH_ASSERT_RENDERER() BLAH_ASSERT(Blah::Internal::renderer, "Renderer has not been created")
#define BLAH_ASSERT_PLATFORM() BLAH_ASSERT(App::Internal::platform, "Platform has not been created") #define BLAH_ASSERT_PLATFORM() BLAH_ASSERT(Blah::Internal::platform, "Platform has not been created")
namespace Blah namespace Blah
{ {
namespace App
{
namespace Internal
{
extern Platform* platform;
extern Renderer* renderer;
void iterate();
void shutdown();
}
}
namespace Input
{
namespace Internal
{
// Initializes the Input State
void init();
// Steps the input state
void step_state();
// Updates bindings
void update_bindings();
// Clears Input State
void shutdown();
}
}
namespace Internal namespace Internal
{ {
extern Platform* platform;
extern Renderer* renderer;
extern bool audio_is_init; extern bool audio_is_init;
void app_step();
void app_shutdown();
void input_init();
void input_step_state();
void input_step_bindings();
void input_shutdown();
// Pass in NULL for `os_handle`, except for the DirectSound backend this should be hwnd. // Pass in NULL for `os_handle`, except for the DirectSound backend this should be hwnd.
// play_frequency_in_Hz depends on your audio file, 44100 seems to be fine. // play_frequency_in_Hz depends on your audio file, 44100 seems to be fine.
// buffered_samples is clamped to be at least 1024. // buffered_samples is clamped to be at least 1024.

View File

@ -17,7 +17,7 @@
#include <d3dcompiler.h> #include <d3dcompiler.h>
// shorthand to our internal state // shorthand to our internal state
#define renderer ((Renderer_D3D11*)App::Internal::renderer) #define RENDERER ((Renderer_D3D11*)Internal::renderer)
namespace Blah namespace Blah
{ {
@ -228,7 +228,7 @@ namespace Blah
m_dxgi_format = desc.Format; m_dxgi_format = desc.Format;
auto hr = renderer->device->CreateTexture2D(&desc, NULL, &texture); auto hr = RENDERER->device->CreateTexture2D(&desc, NULL, &texture);
if (!SUCCEEDED(hr)) if (!SUCCEEDED(hr))
{ {
if (texture) if (texture)
@ -239,7 +239,7 @@ namespace Blah
if (!is_depth_stencil) if (!is_depth_stencil)
{ {
hr = renderer->device->CreateShaderResourceView(texture, NULL, &view); hr = RENDERER->device->CreateShaderResourceView(texture, NULL, &view);
if (!SUCCEEDED(hr)) if (!SUCCEEDED(hr))
{ {
texture->Release(); texture->Release();
@ -288,7 +288,7 @@ namespace Blah
box.back = 1; box.back = 1;
// set data // set data
renderer->context->UpdateSubresource( RENDERER->context->UpdateSubresource(
texture, texture,
0, 0,
&box, &box,
@ -317,7 +317,7 @@ namespace Blah
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.MiscFlags = 0; desc.MiscFlags = 0;
hr = renderer->device->CreateTexture2D(&desc, NULL, &staging); hr = RENDERER->device->CreateTexture2D(&desc, NULL, &staging);
if (!SUCCEEDED(hr)) if (!SUCCEEDED(hr))
{ {
BLAH_ASSERT(false, "Failed to create staging texture to get data"); BLAH_ASSERT(false, "Failed to create staging texture to get data");
@ -334,7 +334,7 @@ namespace Blah
box.back = 1; box.back = 1;
// copy data to staging texture // copy data to staging texture
renderer->context->CopySubresourceRegion( RENDERER->context->CopySubresourceRegion(
staging, 0, staging, 0,
0, 0, 0, 0, 0, 0,
texture, 0, texture, 0,
@ -342,7 +342,7 @@ namespace Blah
// get data // get data
D3D11_MAPPED_SUBRESOURCE map; D3D11_MAPPED_SUBRESOURCE map;
hr = renderer->context->Map(staging, 0, D3D11_MAP_READ, 0, &map); hr = RENDERER->context->Map(staging, 0, D3D11_MAP_READ, 0, &map);
if (!SUCCEEDED(hr)) if (!SUCCEEDED(hr))
{ {
@ -355,7 +355,7 @@ namespace Blah
for (int y = 0; y < m_height; y++) for (int y = 0; y < m_height; y++)
memcpy(data + y * bytes_per_row, (unsigned char*)map.pData + map.RowPitch * y, bytes_per_row); memcpy(data + y * bytes_per_row, (unsigned char*)map.pData + map.RowPitch * y, bytes_per_row);
renderer->context->Unmap(staging, 0); RENDERER->context->Unmap(staging, 0);
} }
bool is_framebuffer() const override bool is_framebuffer() const override
@ -384,12 +384,12 @@ namespace Blah
if (attachments[i] == TextureFormat::DepthStencil) if (attachments[i] == TextureFormat::DepthStencil)
{ {
renderer->device->CreateDepthStencilView(tex->texture, nullptr, &depth_view); RENDERER->device->CreateDepthStencilView(tex->texture, nullptr, &depth_view);
} }
else else
{ {
ID3D11RenderTargetView* view = nullptr; ID3D11RenderTargetView* view = nullptr;
renderer->device->CreateRenderTargetView(tex->texture, nullptr, &view); RENDERER->device->CreateRenderTargetView(tex->texture, nullptr, &view);
color_views.push_back(view); color_views.push_back(view);
} }
} }
@ -423,7 +423,7 @@ namespace Blah
if (((int)mask & (int)ClearMask::Color) == (int)ClearMask::Color) if (((int)mask & (int)ClearMask::Color) == (int)ClearMask::Color)
{ {
for (int i = 0; i < color_views.size(); i++) for (int i = 0; i < color_views.size(); i++)
renderer->context->ClearRenderTargetView(color_views[i], col); RENDERER->context->ClearRenderTargetView(color_views[i], col);
} }
if (depth_view) if (depth_view)
@ -435,7 +435,7 @@ namespace Blah
flags |= D3D11_CLEAR_STENCIL; flags |= D3D11_CLEAR_STENCIL;
if (flags != 0) if (flags != 0)
renderer->context->ClearDepthStencilView(depth_view, flags, depth, stencil); RENDERER->context->ClearDepthStencilView(depth_view, flags, depth, stencil);
} }
} }
}; };
@ -510,7 +510,7 @@ namespace Blah
// create vertex shader // create vertex shader
{ {
hr = renderer->device->CreateVertexShader( hr = RENDERER->device->CreateVertexShader(
vertex_blob->GetBufferPointer(), vertex_blob->GetBufferPointer(),
vertex_blob->GetBufferSize(), vertex_blob->GetBufferSize(),
NULL, NULL,
@ -522,7 +522,7 @@ namespace Blah
// create fragment shader // create fragment shader
{ {
hr = renderer->device->CreatePixelShader( hr = RENDERER->device->CreatePixelShader(
fragment_blob->GetBufferPointer(), fragment_blob->GetBufferPointer(),
fragment_blob->GetBufferSize(), fragment_blob->GetBufferSize(),
NULL, NULL,
@ -675,7 +675,7 @@ namespace Blah
data.pSysMem = indices; data.pSysMem = indices;
// create // create
auto hr = renderer->device->CreateBuffer(&desc, &data, &index_buffer); auto hr = RENDERER->device->CreateBuffer(&desc, &data, &index_buffer);
BLAH_ASSERT(SUCCEEDED(hr), "Failed to update Index Data"); BLAH_ASSERT(SUCCEEDED(hr), "Failed to update Index Data");
} }
} }
@ -683,13 +683,13 @@ namespace Blah
{ {
D3D11_MAPPED_SUBRESOURCE map; D3D11_MAPPED_SUBRESOURCE map;
auto hr = renderer->context->Map(index_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map); auto hr = RENDERER->context->Map(index_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
BLAH_ASSERT(SUCCEEDED(hr), "Failed to update Index Data"); BLAH_ASSERT(SUCCEEDED(hr), "Failed to update Index Data");
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
memcpy(map.pData, indices, index_stride * count); memcpy(map.pData, indices, index_stride * count);
renderer->context->Unmap(index_buffer, 0); RENDERER->context->Unmap(index_buffer, 0);
} }
} }
} }
@ -723,7 +723,7 @@ namespace Blah
data.pSysMem = vertices; data.pSysMem = vertices;
// create // create
auto hr = renderer->device->CreateBuffer(&desc, &data, &vertex_buffer); auto hr = RENDERER->device->CreateBuffer(&desc, &data, &vertex_buffer);
BLAH_ASSERT(SUCCEEDED(hr), "Failed to update Vertex Data"); BLAH_ASSERT(SUCCEEDED(hr), "Failed to update Vertex Data");
} }
} }
@ -731,13 +731,13 @@ namespace Blah
else if (vertices) else if (vertices)
{ {
D3D11_MAPPED_SUBRESOURCE map; D3D11_MAPPED_SUBRESOURCE map;
auto hr = renderer->context->Map(vertex_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map); auto hr = RENDERER->context->Map(vertex_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
BLAH_ASSERT(SUCCEEDED(hr), "Failed to update Vertex Data"); BLAH_ASSERT(SUCCEEDED(hr), "Failed to update Vertex Data");
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
memcpy(map.pData, vertices, vertex_format.stride * count); memcpy(map.pData, vertices, vertex_format.stride * count);
renderer->context->Unmap(vertex_buffer, 0); RENDERER->context->Unmap(vertex_buffer, 0);
} }
} }
} }
@ -776,7 +776,7 @@ namespace Blah
desc.SampleDesc.Quality = 0; desc.SampleDesc.Quality = 0;
desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
desc.BufferCount = 1; desc.BufferCount = 1;
desc.OutputWindow = (HWND)App::Internal::platform->d3d11_get_hwnd(); desc.OutputWindow = (HWND)Internal::platform->d3d11_get_hwnd();
desc.Windowed = true; desc.Windowed = true;
// Creation Flags // Creation Flags
@ -1258,7 +1258,7 @@ namespace Blah
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
ID3D11Buffer* buffer; ID3D11Buffer* buffer;
renderer->device->CreateBuffer(&buffer_desc, nullptr, &buffer); RENDERER->device->CreateBuffer(&buffer_desc, nullptr, &buffer);
append_buffers_to.push_back(buffer); append_buffers_to.push_back(buffer);
} }
@ -1366,9 +1366,9 @@ namespace Blah
if (buffers[i]) if (buffers[i])
{ {
D3D11_MAPPED_SUBRESOURCE map; D3D11_MAPPED_SUBRESOURCE map;
renderer->context->Map(buffers[i], 0, D3D11_MAP_WRITE_DISCARD, 0, &map); RENDERER->context->Map(buffers[i], 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
memcpy(map.pData, values[i].begin(), values[i].size() * sizeof(float)); memcpy(map.pData, values[i].begin(), values[i].size() * sizeof(float));
renderer->context->Unmap(buffers[i], 0); RENDERER->context->Unmap(buffers[i], 0);
} }
} }
} }
@ -1502,7 +1502,7 @@ namespace Blah
desc.RenderTarget[i] = desc.RenderTarget[0]; desc.RenderTarget[i] = desc.RenderTarget[0];
ID3D11BlendState* blend_state = nullptr; ID3D11BlendState* blend_state = nullptr;
auto hr = renderer->device->CreateBlendState(&desc, &blend_state); auto hr = RENDERER->device->CreateBlendState(&desc, &blend_state);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
@ -1550,7 +1550,7 @@ namespace Blah
} }
ID3D11SamplerState* result; ID3D11SamplerState* result;
auto hr = renderer->device->CreateSamplerState(&desc, &result); auto hr = RENDERER->device->CreateSamplerState(&desc, &result);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
@ -1590,7 +1590,7 @@ namespace Blah
desc.AntialiasedLineEnable = false; desc.AntialiasedLineEnable = false;
ID3D11RasterizerState* result; ID3D11RasterizerState* result;
auto hr = renderer->device->CreateRasterizerState(&desc, &result); auto hr = RENDERER->device->CreateRasterizerState(&desc, &result);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
@ -1629,7 +1629,7 @@ namespace Blah
} }
ID3D11DepthStencilState* result; ID3D11DepthStencilState* result;
auto hr = renderer->device->CreateDepthStencilState(&desc, &result); auto hr = RENDERER->device->CreateDepthStencilState(&desc, &result);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
@ -1650,7 +1650,7 @@ Blah::Renderer* Blah::Renderer::try_make_d3d11()
#else // BLAH_RENDERER_D3D11 #else // BLAH_RENDERER_D3D11
#include "blah_renderer.h" #include "blah_RENDERER.h"
Blah::Renderer* Blah::Renderer::try_make_d3d11() Blah::Renderer* Blah::Renderer::try_make_d3d11()
{ {
return nullptr; return nullptr;

View File

@ -340,7 +340,7 @@ typedef void (APIENTRY* DEBUGPROC)(GLenum source,
const void* userParam); const void* userParam);
// shorthand to our internal state // shorthand to our internal state
#define renderer ((Renderer_OpenGL*)App::Internal::renderer) #define RENDERER ((Renderer_OpenGL*)Internal::renderer)
namespace Blah namespace Blah
{ {
@ -470,7 +470,7 @@ namespace Blah
GLuint gl_mesh_assign_attributes(GLuint buffer, GLenum buffer_type, const VertexFormat& format, GLint divisor) GLuint gl_mesh_assign_attributes(GLuint buffer, GLenum buffer_type, const VertexFormat& format, GLint divisor)
{ {
// bind // bind
renderer->gl.BindBuffer(buffer_type, buffer); RENDERER->gl.BindBuffer(buffer_type, buffer);
// TODO: disable existing enabled attributes .. // TODO: disable existing enabled attributes ..
// ... // ...
@ -547,9 +547,9 @@ namespace Blah
} }
u32 location = (u32)(attribute.index); u32 location = (u32)(attribute.index);
renderer->gl.EnableVertexAttribArray(location); RENDERER->gl.EnableVertexAttribArray(location);
renderer->gl.VertexAttribPointer(location, components, type, attribute.normalized, format.stride, (void*)ptr); RENDERER->gl.VertexAttribPointer(location, components, type, attribute.normalized, format.stride, (void*)ptr);
renderer->gl.VertexAttribDivisor(location, divisor); RENDERER->gl.VertexAttribDivisor(location, divisor);
ptr += components * component_size; ptr += components * component_size;
} }
@ -627,9 +627,9 @@ namespace Blah
m_gl_format = GL_RED; m_gl_format = GL_RED;
m_gl_type = GL_UNSIGNED_BYTE; m_gl_type = GL_UNSIGNED_BYTE;
if (width > renderer->max_texture_size || height > renderer->max_texture_size) if (width > RENDERER->max_texture_size || height > RENDERER->max_texture_size)
{ {
Log::error("Exceeded Max Texture Size of %i", renderer->max_texture_size); Log::error("Exceeded Max Texture Size of %i", RENDERER->max_texture_size);
return; return;
} }
@ -663,16 +663,16 @@ namespace Blah
return; return;
} }
renderer->gl.GenTextures(1, &m_id); RENDERER->gl.GenTextures(1, &m_id);
renderer->gl.ActiveTexture(GL_TEXTURE0); RENDERER->gl.ActiveTexture(GL_TEXTURE0);
renderer->gl.BindTexture(GL_TEXTURE_2D, m_id); RENDERER->gl.BindTexture(GL_TEXTURE_2D, m_id);
renderer->gl.TexImage2D(GL_TEXTURE_2D, 0, m_gl_internal_format, width, height, 0, m_gl_format, m_gl_type, nullptr); RENDERER->gl.TexImage2D(GL_TEXTURE_2D, 0, m_gl_internal_format, width, height, 0, m_gl_format, m_gl_type, nullptr);
} }
~OpenGL_Texture() ~OpenGL_Texture()
{ {
if (m_id > 0 && renderer) if (m_id > 0 && RENDERER)
renderer->gl.DeleteTextures(1, &m_id); RENDERER->gl.DeleteTextures(1, &m_id);
} }
GLuint gl_id() const GLuint gl_id() const
@ -701,26 +701,26 @@ namespace Blah
{ {
m_sampler = sampler; m_sampler = sampler;
renderer->gl.BindTexture(GL_TEXTURE_2D, m_id); RENDERER->gl.BindTexture(GL_TEXTURE_2D, m_id);
renderer->gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (m_sampler.filter == TextureFilter::Nearest ? GL_NEAREST : GL_LINEAR)); RENDERER->gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (m_sampler.filter == TextureFilter::Nearest ? GL_NEAREST : GL_LINEAR));
renderer->gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (m_sampler.filter == TextureFilter::Nearest ? GL_NEAREST : GL_LINEAR)); RENDERER->gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (m_sampler.filter == TextureFilter::Nearest ? GL_NEAREST : GL_LINEAR));
renderer->gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (m_sampler.wrap_x == TextureWrap::Clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT)); RENDERER->gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (m_sampler.wrap_x == TextureWrap::Clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT));
renderer->gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (m_sampler.wrap_y == TextureWrap::Clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT)); RENDERER->gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (m_sampler.wrap_y == TextureWrap::Clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT));
} }
} }
virtual void set_data(const u8* data) override virtual void set_data(const u8* data) override
{ {
renderer->gl.ActiveTexture(GL_TEXTURE0); RENDERER->gl.ActiveTexture(GL_TEXTURE0);
renderer->gl.BindTexture(GL_TEXTURE_2D, m_id); RENDERER->gl.BindTexture(GL_TEXTURE_2D, m_id);
renderer->gl.TexImage2D(GL_TEXTURE_2D, 0, m_gl_internal_format, m_width, m_height, 0, m_gl_format, m_gl_type, data); RENDERER->gl.TexImage2D(GL_TEXTURE_2D, 0, m_gl_internal_format, m_width, m_height, 0, m_gl_format, m_gl_type, data);
} }
virtual void get_data(u8* data) override virtual void get_data(u8* data) override
{ {
renderer->gl.ActiveTexture(GL_TEXTURE0); RENDERER->gl.ActiveTexture(GL_TEXTURE0);
renderer->gl.BindTexture(GL_TEXTURE_2D, m_id); RENDERER->gl.BindTexture(GL_TEXTURE_2D, m_id);
renderer->gl.GetTexImage(GL_TEXTURE_2D, 0, m_gl_internal_format, m_gl_type, data); RENDERER->gl.GetTexImage(GL_TEXTURE_2D, 0, m_gl_internal_format, m_gl_type, data);
} }
virtual bool is_framebuffer() const override virtual bool is_framebuffer() const override
@ -742,11 +742,11 @@ namespace Blah
OpenGL_Target(int width, int height, const TextureFormat* attachments, int attachmentCount) OpenGL_Target(int width, int height, const TextureFormat* attachments, int attachmentCount)
{ {
renderer->gl.GenFramebuffers(1, &m_id); RENDERER->gl.GenFramebuffers(1, &m_id);
m_width = width; m_width = width;
m_height = height; m_height = height;
renderer->gl.BindFramebuffer(GL_FRAMEBUFFER, m_id); RENDERER->gl.BindFramebuffer(GL_FRAMEBUFFER, m_id);
for (int i = 0; i < attachmentCount; i++) for (int i = 0; i < attachmentCount; i++)
{ {
@ -758,20 +758,20 @@ namespace Blah
if (attachments[i] != TextureFormat::DepthStencil) if (attachments[i] != TextureFormat::DepthStencil)
{ {
renderer->gl.FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, gltex->gl_id(), 0); RENDERER->gl.FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, gltex->gl_id(), 0);
} }
else else
{ {
renderer->gl.FramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, gltex->gl_id(), 0); RENDERER->gl.FramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, gltex->gl_id(), 0);
} }
} }
} }
~OpenGL_Target() ~OpenGL_Target()
{ {
if (m_id > 0 && renderer) if (m_id > 0 && RENDERER)
{ {
renderer->gl.DeleteFramebuffers(1, &m_id); RENDERER->gl.DeleteFramebuffers(1, &m_id);
m_id = 0; m_id = 0;
} }
} }
@ -793,33 +793,33 @@ namespace Blah
virtual void clear(Color color, float depth, u8 stencil, ClearMask mask) override virtual void clear(Color color, float depth, u8 stencil, ClearMask mask) override
{ {
renderer->gl.BindFramebuffer(GL_FRAMEBUFFER, m_id); RENDERER->gl.BindFramebuffer(GL_FRAMEBUFFER, m_id);
renderer->gl.Disable(GL_SCISSOR_TEST); RENDERER->gl.Disable(GL_SCISSOR_TEST);
int clear = 0; int clear = 0;
if (((int)mask & (int)ClearMask::Color) == (int)ClearMask::Color) if (((int)mask & (int)ClearMask::Color) == (int)ClearMask::Color)
{ {
clear |= GL_COLOR_BUFFER_BIT; clear |= GL_COLOR_BUFFER_BIT;
renderer->gl.ColorMask(true, true, true, true); RENDERER->gl.ColorMask(true, true, true, true);
renderer->gl.ClearColor(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f); RENDERER->gl.ClearColor(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f);
} }
if (((int)mask & (int)ClearMask::Depth) == (int)ClearMask::Depth) if (((int)mask & (int)ClearMask::Depth) == (int)ClearMask::Depth)
{ {
clear |= GL_DEPTH_BUFFER_BIT; clear |= GL_DEPTH_BUFFER_BIT;
if (renderer->gl.ClearDepth) if (RENDERER->gl.ClearDepth)
renderer->gl.ClearDepth(depth); RENDERER->gl.ClearDepth(depth);
} }
if (((int)mask & (int)ClearMask::Stencil) == (int)ClearMask::Stencil) if (((int)mask & (int)ClearMask::Stencil) == (int)ClearMask::Stencil)
{ {
clear |= GL_STENCIL_BUFFER_BIT; clear |= GL_STENCIL_BUFFER_BIT;
if (renderer->gl.ClearStencil) if (RENDERER->gl.ClearStencil)
renderer->gl.ClearStencil(stencil); RENDERER->gl.ClearStencil(stencil);
} }
renderer->gl.Clear(clear); RENDERER->gl.Clear(clear);
} }
}; };
@ -851,47 +851,47 @@ namespace Blah
GLchar log[1024] = { 0 }; GLchar log[1024] = { 0 };
GLsizei log_length = 0; GLsizei log_length = 0;
GLuint vertex_shader = renderer->gl.CreateShader(GL_VERTEX_SHADER); GLuint vertex_shader = RENDERER->gl.CreateShader(GL_VERTEX_SHADER);
{ {
const GLchar* source = (const GLchar*)data->vertex.cstr(); const GLchar* source = (const GLchar*)data->vertex.cstr();
renderer->gl.ShaderSource(vertex_shader, 1, &source, nullptr); RENDERER->gl.ShaderSource(vertex_shader, 1, &source, nullptr);
renderer->gl.CompileShader(vertex_shader); RENDERER->gl.CompileShader(vertex_shader);
renderer->gl.GetShaderInfoLog(vertex_shader, 1024, &log_length, log); RENDERER->gl.GetShaderInfoLog(vertex_shader, 1024, &log_length, log);
if (log_length > 0) if (log_length > 0)
{ {
renderer->gl.DeleteShader(vertex_shader); RENDERER->gl.DeleteShader(vertex_shader);
Log::error(log); Log::error(log);
return; return;
} }
} }
GLuint fragment_shader = renderer->gl.CreateShader(GL_FRAGMENT_SHADER); GLuint fragment_shader = RENDERER->gl.CreateShader(GL_FRAGMENT_SHADER);
{ {
const GLchar* source = (const GLchar*)data->fragment.cstr(); const GLchar* source = (const GLchar*)data->fragment.cstr();
renderer->gl.ShaderSource(fragment_shader, 1, &source, nullptr); RENDERER->gl.ShaderSource(fragment_shader, 1, &source, nullptr);
renderer->gl.CompileShader(fragment_shader); RENDERER->gl.CompileShader(fragment_shader);
renderer->gl.GetShaderInfoLog(fragment_shader, 1024, &log_length, log); RENDERER->gl.GetShaderInfoLog(fragment_shader, 1024, &log_length, log);
if (log_length > 0) if (log_length > 0)
{ {
renderer->gl.DeleteShader(vertex_shader); RENDERER->gl.DeleteShader(vertex_shader);
renderer->gl.DeleteShader(fragment_shader); RENDERER->gl.DeleteShader(fragment_shader);
Log::error(log); Log::error(log);
return; return;
} }
} }
// create actual shader program // create actual shader program
GLuint id = renderer->gl.CreateProgram(); GLuint id = RENDERER->gl.CreateProgram();
renderer->gl.AttachShader(id, vertex_shader); RENDERER->gl.AttachShader(id, vertex_shader);
renderer->gl.AttachShader(id, fragment_shader); RENDERER->gl.AttachShader(id, fragment_shader);
renderer->gl.LinkProgram(id); RENDERER->gl.LinkProgram(id);
renderer->gl.GetProgramInfoLog(id, 1024, &log_length, log); RENDERER->gl.GetProgramInfoLog(id, 1024, &log_length, log);
renderer->gl.DetachShader(id, vertex_shader); RENDERER->gl.DetachShader(id, vertex_shader);
renderer->gl.DetachShader(id, fragment_shader); RENDERER->gl.DetachShader(id, fragment_shader);
renderer->gl.DeleteShader(vertex_shader); RENDERER->gl.DeleteShader(vertex_shader);
renderer->gl.DeleteShader(fragment_shader); RENDERER->gl.DeleteShader(fragment_shader);
if (log_length > 0) if (log_length > 0)
{ {
@ -906,7 +906,7 @@ namespace Blah
GLint active_uniforms = 0; GLint active_uniforms = 0;
GLint sampler_uniforms = 0; GLint sampler_uniforms = 0;
renderer->gl.GetProgramiv(id, GL_ACTIVE_UNIFORMS, &active_uniforms); RENDERER->gl.GetProgramiv(id, GL_ACTIVE_UNIFORMS, &active_uniforms);
for (int i = 0; i < active_uniforms; i++) for (int i = 0; i < active_uniforms; i++)
{ {
@ -915,7 +915,7 @@ namespace Blah
GLenum type; GLenum type;
GLchar name[max_name_length + 1] = { 0 }; GLchar name[max_name_length + 1] = { 0 };
renderer->gl.GetActiveUniform(id, i, max_name_length, &length, &size, &type, name); RENDERER->gl.GetActiveUniform(id, i, max_name_length, &length, &size, &type, name);
name[length] = '\0'; name[length] = '\0';
// array names end with "[0]", and we don't want that // array names end with "[0]", and we don't want that
@ -938,7 +938,7 @@ namespace Blah
tex_uniform.array_length = size; tex_uniform.array_length = size;
tex_uniform.type = UniformType::Texture2D; tex_uniform.type = UniformType::Texture2D;
tex_uniform.shader = ShaderType::Fragment; tex_uniform.shader = ShaderType::Fragment;
uniform_locations.push_back(renderer->gl.GetUniformLocation(id, name)); uniform_locations.push_back(RENDERER->gl.GetUniformLocation(id, name));
m_uniforms.push_back(tex_uniform); m_uniforms.push_back(tex_uniform);
UniformInfo sampler_uniform; UniformInfo sampler_uniform;
@ -948,7 +948,7 @@ namespace Blah
sampler_uniform.array_length = size; sampler_uniform.array_length = size;
sampler_uniform.type = UniformType::Sampler2D; sampler_uniform.type = UniformType::Sampler2D;
sampler_uniform.shader = ShaderType::Fragment; sampler_uniform.shader = ShaderType::Fragment;
uniform_locations.push_back(renderer->gl.GetUniformLocation(id, name)); uniform_locations.push_back(RENDERER->gl.GetUniformLocation(id, name));
m_uniforms.push_back(sampler_uniform); m_uniforms.push_back(sampler_uniform);
sampler_uniforms += size; sampler_uniforms += size;
@ -961,7 +961,7 @@ namespace Blah
uniform.register_index = 0; uniform.register_index = 0;
uniform.buffer_index = 0; uniform.buffer_index = 0;
uniform.array_length = size; uniform.array_length = size;
uniform_locations.push_back(renderer->gl.GetUniformLocation(id, name)); uniform_locations.push_back(RENDERER->gl.GetUniformLocation(id, name));
uniform.shader = (ShaderType)((int)ShaderType::Vertex | (int)ShaderType::Fragment); uniform.shader = (ShaderType)((int)ShaderType::Vertex | (int)ShaderType::Fragment);
if (type == GL_FLOAT) if (type == GL_FLOAT)
@ -991,15 +991,15 @@ namespace Blah
// assign ID if the uniforms were valid // assign ID if the uniforms were valid
if (!valid_uniforms) if (!valid_uniforms)
renderer->gl.DeleteProgram(id); RENDERER->gl.DeleteProgram(id);
else else
m_id = id; m_id = id;
} }
~OpenGL_Shader() ~OpenGL_Shader()
{ {
if (m_id > 0 && renderer) if (m_id > 0 && RENDERER)
renderer->gl.DeleteProgram(m_id); RENDERER->gl.DeleteProgram(m_id);
m_id = 0; m_id = 0;
} }
@ -1054,21 +1054,21 @@ namespace Blah
m_vertex_attribs_enabled = 0; m_vertex_attribs_enabled = 0;
m_instance_attribs_enabled = 0; m_instance_attribs_enabled = 0;
renderer->gl.GenVertexArrays(1, &m_id); RENDERER->gl.GenVertexArrays(1, &m_id);
} }
~OpenGL_Mesh() ~OpenGL_Mesh()
{ {
if (renderer) if (RENDERER)
{ {
if (m_vertex_buffer != 0) if (m_vertex_buffer != 0)
renderer->gl.DeleteBuffers(1, &m_vertex_buffer); RENDERER->gl.DeleteBuffers(1, &m_vertex_buffer);
if (m_index_buffer != 0) if (m_index_buffer != 0)
renderer->gl.DeleteBuffers(1, &m_index_buffer); RENDERER->gl.DeleteBuffers(1, &m_index_buffer);
if (m_instance_buffer != 0) if (m_instance_buffer != 0)
renderer->gl.DeleteBuffers(1, &m_instance_buffer); RENDERER->gl.DeleteBuffers(1, &m_instance_buffer);
if (m_id != 0) if (m_id != 0)
renderer->gl.DeleteVertexArrays(1, &m_id); RENDERER->gl.DeleteVertexArrays(1, &m_id);
} }
m_id = 0; m_id = 0;
} }
@ -1092,10 +1092,10 @@ namespace Blah
{ {
m_index_count = count; m_index_count = count;
renderer->gl.BindVertexArray(m_id); RENDERER->gl.BindVertexArray(m_id);
{ {
if (m_index_buffer == 0) if (m_index_buffer == 0)
renderer->gl.GenBuffers(1, &(m_index_buffer)); RENDERER->gl.GenBuffers(1, &(m_index_buffer));
switch (format) switch (format)
{ {
@ -1109,52 +1109,52 @@ namespace Blah
break; break;
} }
renderer->gl.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_buffer); RENDERER->gl.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_buffer);
renderer->gl.BufferData(GL_ELEMENT_ARRAY_BUFFER, m_index_size * count, indices, GL_DYNAMIC_DRAW); RENDERER->gl.BufferData(GL_ELEMENT_ARRAY_BUFFER, m_index_size * count, indices, GL_DYNAMIC_DRAW);
} }
renderer->gl.BindVertexArray(0); RENDERER->gl.BindVertexArray(0);
} }
virtual void vertex_data(const VertexFormat& format, const void* vertices, i64 count) override virtual void vertex_data(const VertexFormat& format, const void* vertices, i64 count) override
{ {
m_vertex_count = count; m_vertex_count = count;
renderer->gl.BindVertexArray(m_id); RENDERER->gl.BindVertexArray(m_id);
{ {
// Create Buffer if it doesn't exist yet // Create Buffer if it doesn't exist yet
if (m_vertex_buffer == 0) if (m_vertex_buffer == 0)
renderer->gl.GenBuffers(1, &(m_vertex_buffer)); RENDERER->gl.GenBuffers(1, &(m_vertex_buffer));
// TODO: // TODO:
// Cache this // Cache this
m_vertex_size = gl_mesh_assign_attributes(m_vertex_buffer, GL_ARRAY_BUFFER, format, 0); m_vertex_size = gl_mesh_assign_attributes(m_vertex_buffer, GL_ARRAY_BUFFER, format, 0);
// Upload Buffer // Upload Buffer
renderer->gl.BindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer); RENDERER->gl.BindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer);
renderer->gl.BufferData(GL_ARRAY_BUFFER, m_vertex_size * count, vertices, GL_DYNAMIC_DRAW); RENDERER->gl.BufferData(GL_ARRAY_BUFFER, m_vertex_size * count, vertices, GL_DYNAMIC_DRAW);
} }
renderer->gl.BindVertexArray(0); RENDERER->gl.BindVertexArray(0);
} }
virtual void instance_data(const VertexFormat& format, const void* instances, i64 count) override virtual void instance_data(const VertexFormat& format, const void* instances, i64 count) override
{ {
m_instance_count = count; m_instance_count = count;
renderer->gl.BindVertexArray(m_id); RENDERER->gl.BindVertexArray(m_id);
{ {
// Create Buffer if it doesn't exist yet // Create Buffer if it doesn't exist yet
if (m_instance_buffer == 0) if (m_instance_buffer == 0)
renderer->gl.GenBuffers(1, &(m_instance_buffer)); RENDERER->gl.GenBuffers(1, &(m_instance_buffer));
// TODO: // TODO:
// Cache this // Cache this
m_instance_size = gl_mesh_assign_attributes(m_instance_buffer, GL_ARRAY_BUFFER, format, 1); m_instance_size = gl_mesh_assign_attributes(m_instance_buffer, GL_ARRAY_BUFFER, format, 1);
// Upload Buffer // Upload Buffer
renderer->gl.BindBuffer(GL_ARRAY_BUFFER, m_instance_buffer); RENDERER->gl.BindBuffer(GL_ARRAY_BUFFER, m_instance_buffer);
renderer->gl.BufferData(GL_ARRAY_BUFFER, m_instance_size * count, instances, GL_DYNAMIC_DRAW); RENDERER->gl.BufferData(GL_ARRAY_BUFFER, m_instance_size * count, instances, GL_DYNAMIC_DRAW);
} }
renderer->gl.BindVertexArray(0); RENDERER->gl.BindVertexArray(0);
} }
virtual i64 index_count() const override virtual i64 index_count() const override
@ -1176,16 +1176,16 @@ namespace Blah
bool Renderer_OpenGL::init() bool Renderer_OpenGL::init()
{ {
// create gl context // create gl context
context = App::Internal::platform->gl_context_create(); context = Internal::platform->gl_context_create();
if (context == nullptr) if (context == nullptr)
{ {
Log::error("Failed to create OpenGL Context"); Log::error("Failed to create OpenGL Context");
return false; return false;
} }
App::Internal::platform->gl_context_make_current(context); Internal::platform->gl_context_make_current(context);
// bind opengl functions // bind opengl functions
#define GL_FUNC(name, ...) gl.name = (Renderer_OpenGL::Bindings::name ## Func)(App::Internal::platform->gl_get_func("gl" #name)); #define GL_FUNC(name, ...) gl.name = (Renderer_OpenGL::Bindings::name ## Func)(Internal::platform->gl_get_func("gl" #name));
GL_FUNCTIONS GL_FUNCTIONS
#undef GL_FUNC #undef GL_FUNC
@ -1229,7 +1229,7 @@ namespace Blah
void Renderer_OpenGL::shutdown() void Renderer_OpenGL::shutdown()
{ {
App::Internal::platform->gl_context_destroy(context); Internal::platform->gl_context_destroy(context);
context = nullptr; context = nullptr;
} }
@ -1294,12 +1294,12 @@ namespace Blah
// Bind the Target // Bind the Target
if (pass.target == App::backbuffer()) if (pass.target == App::backbuffer())
{ {
renderer->gl.BindFramebuffer(GL_FRAMEBUFFER, 0); RENDERER->gl.BindFramebuffer(GL_FRAMEBUFFER, 0);
} }
else if (pass.target) else if (pass.target)
{ {
auto framebuffer = (OpenGL_Target*)pass.target.get(); auto framebuffer = (OpenGL_Target*)pass.target.get();
renderer->gl.BindFramebuffer(GL_FRAMEBUFFER, framebuffer->gl_id()); RENDERER->gl.BindFramebuffer(GL_FRAMEBUFFER, framebuffer->gl_id());
} }
auto size = Point(pass.target->width(), pass.target->height()); auto size = Point(pass.target->width(), pass.target->height());
@ -1311,7 +1311,7 @@ namespace Blah
// 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
// TODO: this should be cached? // TODO: this should be cached?
{ {
renderer->gl.UseProgram(shader->gl_id()); RENDERER->gl.UseProgram(shader->gl_id());
int texture_slot = 0; int texture_slot = 0;
GLint texture_ids[64]; GLint texture_ids[64];
@ -1335,61 +1335,61 @@ namespace Blah
auto tex = pass.material->get_texture(texture_slot); auto tex = pass.material->get_texture(texture_slot);
auto sampler = pass.material->get_sampler(texture_slot); auto sampler = pass.material->get_sampler(texture_slot);
renderer->gl.ActiveTexture(GL_TEXTURE0 + texture_slot); RENDERER->gl.ActiveTexture(GL_TEXTURE0 + texture_slot);
if (!tex) if (!tex)
{ {
renderer->gl.BindTexture(GL_TEXTURE_2D, 0); RENDERER->gl.BindTexture(GL_TEXTURE_2D, 0);
} }
else else
{ {
auto gl_tex = ((OpenGL_Texture*)tex.get()); auto gl_tex = ((OpenGL_Texture*)tex.get());
gl_tex->update_sampler(sampler); gl_tex->update_sampler(sampler);
renderer->gl.BindTexture(GL_TEXTURE_2D, gl_tex->gl_id()); RENDERER->gl.BindTexture(GL_TEXTURE_2D, gl_tex->gl_id());
} }
texture_ids[n] = texture_slot; texture_ids[n] = texture_slot;
texture_slot++; texture_slot++;
} }
renderer->gl.Uniform1iv(location, (GLint)uniform.array_length, &texture_ids[0]); RENDERER->gl.Uniform1iv(location, (GLint)uniform.array_length, &texture_ids[0]);
continue; continue;
} }
// Float // Float
if (uniform.type == UniformType::Float) if (uniform.type == UniformType::Float)
{ {
renderer->gl.Uniform1fv(location, (GLint)uniform.array_length, data); RENDERER->gl.Uniform1fv(location, (GLint)uniform.array_length, data);
data += uniform.array_length; data += uniform.array_length;
} }
// Float2 // Float2
else if (uniform.type == UniformType::Float2) else if (uniform.type == UniformType::Float2)
{ {
renderer->gl.Uniform2fv(location, (GLint)uniform.array_length, data); RENDERER->gl.Uniform2fv(location, (GLint)uniform.array_length, data);
data += 2 * uniform.array_length; data += 2 * uniform.array_length;
} }
// Float3 // Float3
else if (uniform.type == UniformType::Float3) else if (uniform.type == UniformType::Float3)
{ {
renderer->gl.Uniform3fv(location, (GLint)uniform.array_length, data); RENDERER->gl.Uniform3fv(location, (GLint)uniform.array_length, data);
data += 3 * uniform.array_length; data += 3 * uniform.array_length;
} }
// Float4 // Float4
else if (uniform.type == UniformType::Float4) else if (uniform.type == UniformType::Float4)
{ {
renderer->gl.Uniform4fv(location, (GLint)uniform.array_length, data); RENDERER->gl.Uniform4fv(location, (GLint)uniform.array_length, data);
data += 4 * uniform.array_length; data += 4 * uniform.array_length;
} }
// Matrix3x2 // Matrix3x2
else if (uniform.type == UniformType::Mat3x2) else if (uniform.type == UniformType::Mat3x2)
{ {
renderer->gl.UniformMatrix3x2fv(location, (GLint)uniform.array_length, 0, data); RENDERER->gl.UniformMatrix3x2fv(location, (GLint)uniform.array_length, 0, data);
data += 6 * uniform.array_length; data += 6 * uniform.array_length;
} }
// Matrix4x4 // Matrix4x4
else if (uniform.type == UniformType::Mat4x4) else if (uniform.type == UniformType::Mat4x4)
{ {
renderer->gl.UniformMatrix4fv(location, (GLint)uniform.array_length, 0, data); RENDERER->gl.UniformMatrix4fv(location, (GLint)uniform.array_length, 0, data);
data += 16 * uniform.array_length; data += 16 * uniform.array_length;
} }
} }
@ -1404,11 +1404,11 @@ namespace Blah
GLenum alphaSrc = gl_get_blend_factor(pass.blend.alpha_src); GLenum alphaSrc = gl_get_blend_factor(pass.blend.alpha_src);
GLenum alphaDst = gl_get_blend_factor(pass.blend.alpha_dst); GLenum alphaDst = gl_get_blend_factor(pass.blend.alpha_dst);
renderer->gl.Enable(GL_BLEND); RENDERER->gl.Enable(GL_BLEND);
renderer->gl.BlendEquationSeparate(colorOp, alphaOp); RENDERER->gl.BlendEquationSeparate(colorOp, alphaOp);
renderer->gl.BlendFuncSeparate(colorSrc, colorDst, alphaSrc, alphaDst); RENDERER->gl.BlendFuncSeparate(colorSrc, colorDst, alphaSrc, alphaDst);
renderer->gl.ColorMask( RENDERER->gl.ColorMask(
((int)pass.blend.mask & (int)BlendMask::Red), ((int)pass.blend.mask & (int)BlendMask::Red),
((int)pass.blend.mask & (int)BlendMask::Green), ((int)pass.blend.mask & (int)BlendMask::Green),
((int)pass.blend.mask & (int)BlendMask::Blue), ((int)pass.blend.mask & (int)BlendMask::Blue),
@ -1419,7 +1419,7 @@ namespace Blah
unsigned char b = pass.blend.rgba >> 8; unsigned char b = pass.blend.rgba >> 8;
unsigned char a = pass.blend.rgba; unsigned char a = pass.blend.rgba;
renderer->gl.BlendColor( RENDERER->gl.BlendColor(
r / 255.0f, r / 255.0f,
g / 255.0f, g / 255.0f,
b / 255.0f, b / 255.0f,
@ -1430,38 +1430,38 @@ namespace Blah
{ {
if (pass.depth == Compare::None) if (pass.depth == Compare::None)
{ {
renderer->gl.Disable(GL_DEPTH_TEST); RENDERER->gl.Disable(GL_DEPTH_TEST);
} }
else else
{ {
renderer->gl.Enable(GL_DEPTH_TEST); RENDERER->gl.Enable(GL_DEPTH_TEST);
switch (pass.depth) switch (pass.depth)
{ {
case Compare::None: break; case Compare::None: break;
case Compare::Always: case Compare::Always:
renderer->gl.DepthFunc(GL_ALWAYS); RENDERER->gl.DepthFunc(GL_ALWAYS);
break; break;
case Compare::Equal: case Compare::Equal:
renderer->gl.DepthFunc(GL_EQUAL); RENDERER->gl.DepthFunc(GL_EQUAL);
break; break;
case Compare::Greater: case Compare::Greater:
renderer->gl.DepthFunc(GL_GREATER); RENDERER->gl.DepthFunc(GL_GREATER);
break; break;
case Compare::GreaterOrEqual: case Compare::GreaterOrEqual:
renderer->gl.DepthFunc(GL_GEQUAL); RENDERER->gl.DepthFunc(GL_GEQUAL);
break; break;
case Compare::Less: case Compare::Less:
renderer->gl.DepthFunc(GL_LESS); RENDERER->gl.DepthFunc(GL_LESS);
break; break;
case Compare::LessOrEqual: case Compare::LessOrEqual:
renderer->gl.DepthFunc(GL_LEQUAL); RENDERER->gl.DepthFunc(GL_LEQUAL);
break; break;
case Compare::Never: case Compare::Never:
renderer->gl.DepthFunc(GL_NEVER); RENDERER->gl.DepthFunc(GL_NEVER);
break; break;
case Compare::NotEqual: case Compare::NotEqual:
renderer->gl.DepthFunc(GL_NOTEQUAL); RENDERER->gl.DepthFunc(GL_NOTEQUAL);
break; break;
} }
} }
@ -1471,18 +1471,18 @@ namespace Blah
{ {
if (pass.cull == Cull::None) if (pass.cull == Cull::None)
{ {
renderer->gl.Disable(GL_CULL_FACE); RENDERER->gl.Disable(GL_CULL_FACE);
} }
else else
{ {
renderer->gl.Enable(GL_CULL_FACE); RENDERER->gl.Enable(GL_CULL_FACE);
if (pass.cull == Cull::Back) if (pass.cull == Cull::Back)
renderer->gl.CullFace(GL_BACK); RENDERER->gl.CullFace(GL_BACK);
else if (pass.cull == Cull::Front) else if (pass.cull == Cull::Front)
renderer->gl.CullFace(GL_FRONT); RENDERER->gl.CullFace(GL_FRONT);
else else
renderer->gl.CullFace(GL_FRONT_AND_BACK); RENDERER->gl.CullFace(GL_FRONT_AND_BACK);
} }
} }
@ -1491,14 +1491,14 @@ namespace Blah
Rectf viewport = pass.viewport; Rectf viewport = pass.viewport;
viewport.y = size.y - viewport.y - viewport.h; viewport.y = size.y - viewport.y - viewport.h;
renderer->gl.Viewport((GLint)viewport.x, (GLint)viewport.y, (GLint)viewport.w, (GLint)viewport.h); RENDERER->gl.Viewport((GLint)viewport.x, (GLint)viewport.y, (GLint)viewport.w, (GLint)viewport.h);
} }
// Scissor // Scissor
{ {
if (!pass.has_scissor) if (!pass.has_scissor)
{ {
renderer->gl.Disable(GL_SCISSOR_TEST); RENDERER->gl.Disable(GL_SCISSOR_TEST);
} }
else else
{ {
@ -1510,21 +1510,21 @@ namespace Blah
if (scissor.h < 0) if (scissor.h < 0)
scissor.h = 0; scissor.h = 0;
renderer->gl.Enable(GL_SCISSOR_TEST); RENDERER->gl.Enable(GL_SCISSOR_TEST);
renderer->gl.Scissor((GLint)scissor.x, (GLint)scissor.y, (GLint)scissor.w, (GLint)scissor.h); RENDERER->gl.Scissor((GLint)scissor.x, (GLint)scissor.y, (GLint)scissor.w, (GLint)scissor.h);
} }
} }
// Draw the Mesh // Draw the Mesh
{ {
renderer->gl.BindVertexArray(mesh->gl_id()); RENDERER->gl.BindVertexArray(mesh->gl_id());
GLenum index_format = mesh->gl_index_format(); GLenum index_format = mesh->gl_index_format();
int index_size = mesh->gl_index_size(); int index_size = mesh->gl_index_size();
if (pass.instance_count > 0) if (pass.instance_count > 0)
{ {
renderer->gl.DrawElementsInstanced( RENDERER->gl.DrawElementsInstanced(
GL_TRIANGLES, GL_TRIANGLES,
(GLint)(pass.index_count), (GLint)(pass.index_count),
index_format, index_format,
@ -1533,46 +1533,46 @@ namespace Blah
} }
else else
{ {
renderer->gl.DrawElements( RENDERER->gl.DrawElements(
GL_TRIANGLES, GL_TRIANGLES,
(GLint)(pass.index_count), (GLint)(pass.index_count),
index_format, index_format,
(void*)(index_size * pass.index_start)); (void*)(index_size * pass.index_start));
} }
renderer->gl.BindVertexArray(0); RENDERER->gl.BindVertexArray(0);
} }
} }
void Renderer_OpenGL::clear_backbuffer(Color color, float depth, u8 stencil, ClearMask mask) void Renderer_OpenGL::clear_backbuffer(Color color, float depth, u8 stencil, ClearMask mask)
{ {
renderer->gl.BindFramebuffer(GL_FRAMEBUFFER, 0); RENDERER->gl.BindFramebuffer(GL_FRAMEBUFFER, 0);
renderer->gl.Disable(GL_SCISSOR_TEST); RENDERER->gl.Disable(GL_SCISSOR_TEST);
int clear = 0; int clear = 0;
if (((int)mask & (int)ClearMask::Color) == (int)ClearMask::Color) if (((int)mask & (int)ClearMask::Color) == (int)ClearMask::Color)
{ {
clear |= GL_COLOR_BUFFER_BIT; clear |= GL_COLOR_BUFFER_BIT;
renderer->gl.ColorMask(true, true, true, true); RENDERER->gl.ColorMask(true, true, true, true);
renderer->gl.ClearColor(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f); RENDERER->gl.ClearColor(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f);
} }
if (((int)mask & (int)ClearMask::Depth) == (int)ClearMask::Depth) if (((int)mask & (int)ClearMask::Depth) == (int)ClearMask::Depth)
{ {
clear |= GL_DEPTH_BUFFER_BIT; clear |= GL_DEPTH_BUFFER_BIT;
if (renderer->gl.ClearDepth) if (RENDERER->gl.ClearDepth)
renderer->gl.ClearDepth(depth); RENDERER->gl.ClearDepth(depth);
} }
if (((int)mask & (int)ClearMask::Stencil) == (int)ClearMask::Stencil) if (((int)mask & (int)ClearMask::Stencil) == (int)ClearMask::Stencil)
{ {
clear |= GL_STENCIL_BUFFER_BIT; clear |= GL_STENCIL_BUFFER_BIT;
if (renderer->gl.ClearStencil) if (RENDERER->gl.ClearStencil)
renderer->gl.ClearStencil(stencil); RENDERER->gl.ClearStencil(stencil);
} }
renderer->gl.Clear(clear); RENDERER->gl.Clear(clear);
} }
} }