diff --git a/private/blah/internal/graphics_backend_gl.cpp b/private/blah/internal/graphics_backend_gl.cpp index f22a459..a20a954 100644 --- a/private/blah/internal/graphics_backend_gl.cpp +++ b/private/blah/internal/graphics_backend_gl.cpp @@ -1,14 +1,10 @@ #ifdef BLAH_USE_OPENGL + #include #include #include -#include -#include -#include -#include -#include -#include #include +#include #include #include #include @@ -346,25 +342,29 @@ typedef void (APIENTRY* DEBUGPROC)(GLenum source, namespace Blah { - // GL function pointers - struct GL + struct State { + // GL function pointers #define GL_FUNC(name, ret, ...) typedef ret (*name ## Func) (__VA_ARGS__); name ## Func name; GL_FUNCTIONS #undef GL_FUNC + + // state + void* context; + + // info + int max_color_attachments; + int max_element_indices; + int max_element_vertices; + int max_renderbuffer_size; + int max_samples; + int max_texture_image_units; + int max_texture_size; + GraphicsInfo info; }; // static state - GL gl; - void* gl_context; - int gl_max_color_attachments; - int gl_max_element_indices; - int gl_max_element_vertices; - int gl_max_renderbuffer_size; - int gl_max_samples; - int gl_max_texture_image_units; - int gl_max_texture_size; - GraphicsInfo gl_info; + State gl; // debug callback void APIENTRY gl_message_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) @@ -537,9 +537,9 @@ namespace Blah m_gl_format = GL_RED; m_gl_type = GL_UNSIGNED_BYTE; - if (width > gl_max_texture_size || height > gl_max_texture_size) + if (width > gl.max_texture_size || height > gl.max_texture_size) { - Log::error("Exceeded Max Texture Size of %i", gl_max_texture_size); + Log::error("Exceeded Max Texture Size of %i", gl.max_texture_size); return; } @@ -757,11 +757,10 @@ namespace Blah { private: GLuint m_id; - Attributes m_attributes; - Uniforms m_uniforms; + Vector m_uniforms; public: - GLint uniforms_loc[BLAH_UNIFORMS] = { 0 }; + Vector uniform_locations; OpenGL_Shader(const ShaderData* data) { @@ -833,56 +832,25 @@ namespace Blah // ready to go m_id = id; - // get attributes - { - GLint active_attributes = 0; - gl.GetProgramiv(id, GL_ACTIVE_ATTRIBUTES, &active_attributes); - if (active_attributes > BLAH_ATTRIBUTES) - { - Log::warn("Exceeding maximum shader attributes (%i / %i)", active_attributes, BLAH_ATTRIBUTES); - active_attributes = BLAH_ATTRIBUTES; - } - - for (int i = 0; i < active_attributes; i++) - { - GLsizei length; - GLsizei size; - GLenum type; - GLchar name[BLAH_ATTRIBUTE_NAME]; - - gl.GetActiveAttrib(id, i, BLAH_ATTRIBUTE_NAME - 1, &length, &size, &type, name); - name[length] = '\0'; - - ShaderAttribute attr; - attr.name = name; - attr.semantic_name = ""; - attr.semantic_location = 0; - m_attributes.push_back(attr); - } - } - // get uniforms { + const int max_name_length = 256; + GLint active_uniforms = 0; gl.GetProgramiv(id, GL_ACTIVE_UNIFORMS, &active_uniforms); - if (active_uniforms > BLAH_UNIFORMS) - { - Log::warn("Exceeding maximum shader uniforms (%i / %i)", active_uniforms, BLAH_ATTRIBUTES); - active_uniforms = BLAH_UNIFORMS; - } for (int i = 0; i < active_uniforms; i++) { GLsizei length; GLsizei size; GLenum type; - GLchar name[BLAH_UNIFORM_NAME]; + GLchar name[max_name_length + 1]; - gl.GetActiveUniform(id, i, BLAH_UNIFORM_NAME - 1, &length, &size, &type, name); + gl.GetActiveUniform(id, i, max_name_length, &length, &size, &type, name); name[length] = '\0'; // array names end with "[0]", and we don't want that - for (int n = 0; n < BLAH_UNIFORM_NAME; n++) + for (int n = 0; n < max_name_length; n++) if (name[n] == '[') { if (name[n + 1] == '0' && name[n + 2] == ']') @@ -892,11 +860,11 @@ namespace Blah } } - ShaderUniform uniform; + UniformInfo uniform; uniform.name = name; uniform.type = UniformType::None; uniform.array_length = size; - uniforms_loc[i] = gl.GetUniformLocation(id, name); + uniform_locations.push_back(gl.GetUniformLocation(id, name)); if (type == GL_FLOAT) uniform.type = UniformType::Float; @@ -935,25 +903,15 @@ namespace Blah return m_id; } - virtual Uniforms& uniforms() override + virtual Vector& uniforms() override { return m_uniforms; } - virtual const Uniforms& uniforms() const override + virtual const Vector& uniforms() const override { return m_uniforms; } - - virtual Attributes& attributes() override - { - return m_attributes; - } - - virtual const Attributes& attributes() const override - { - return m_attributes; - } }; class OpenGL_Mesh : public Mesh @@ -970,8 +928,8 @@ namespace Blah uint16_t m_instance_size; uint8_t m_vertex_attribs_enabled; uint8_t m_instance_attribs_enabled; - GLuint m_vertex_attribs[BLAH_ATTRIBUTES]; - GLuint m_instance_attribs[BLAH_ATTRIBUTES]; + Vector m_vertex_attribs; + Vector m_instance_attribs; public: @@ -988,8 +946,6 @@ namespace Blah m_instance_size = 0; m_vertex_attribs_enabled = 0; m_instance_attribs_enabled = 0; - m_vertex_attribs[0] = 0; - m_instance_attribs[0] = 0; gl.GenVertexArrays(1, &m_id); } @@ -1107,17 +1063,19 @@ namespace Blah bool GraphicsBackend::init() { + gl = State(); + // create gl context - gl_context = PlatformBackend::gl_context_create(); - if (gl_context == nullptr) + gl.context = PlatformBackend::gl_context_create(); + if (gl.context == nullptr) { Log::error("Failed to create OpenGL Context"); return false; } - PlatformBackend::gl_context_make_current(gl_context); + PlatformBackend::gl_context_make_current(gl.context); // bind opengl functions - #define GL_FUNC(name, ...) gl.name = (GL::name ## Func)(PlatformBackend::gl_get_func("gl" #name)); + #define GL_FUNC(name, ...) gl.name = (State::name ## Func)(PlatformBackend::gl_get_func("gl" #name)); GL_FUNCTIONS #undef GL_FUNC @@ -1130,13 +1088,13 @@ namespace Blah } // get opengl info - gl.GetIntegerv(0x8CDF, &gl_max_color_attachments); - gl.GetIntegerv(0x80E9, &gl_max_element_indices); - gl.GetIntegerv(0x80E8, &gl_max_element_vertices); - gl.GetIntegerv(0x84E8, &gl_max_renderbuffer_size); - gl.GetIntegerv(0x8D57, &gl_max_samples); - gl.GetIntegerv(0x8872, &gl_max_texture_image_units); - gl.GetIntegerv(0x0D33, &gl_max_texture_size); + gl.GetIntegerv(0x8CDF, &gl.max_color_attachments); + gl.GetIntegerv(0x80E9, &gl.max_element_indices); + gl.GetIntegerv(0x80E8, &gl.max_element_vertices); + gl.GetIntegerv(0x84E8, &gl.max_renderbuffer_size); + gl.GetIntegerv(0x8D57, &gl.max_samples); + gl.GetIntegerv(0x8872, &gl.max_texture_image_units); + gl.GetIntegerv(0x0D33, &gl.max_texture_size); // log Log::print("OpenGL %s, %s", @@ -1148,9 +1106,9 @@ namespace Blah gl.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // assign info - gl_info.instancing = true; - gl_info.origin_bottom_left = true; - gl_info.max_texture_size = gl_max_texture_size; + gl.info.instancing = true; + gl.info.origin_bottom_left = true; + gl.info.max_texture_size = gl.max_texture_size; return true; } @@ -1162,13 +1120,13 @@ namespace Blah void GraphicsBackend::shutdown() { - PlatformBackend::gl_context_destroy(gl_context); - gl_context = nullptr; + PlatformBackend::gl_context_destroy(gl.context); + gl.context = nullptr; } const GraphicsInfo* GraphicsBackend::info() { - return &gl_info; + return &gl.info; } void GraphicsBackend::frame() {} @@ -1261,7 +1219,7 @@ namespace Blah auto& uniforms = shader->uniforms(); for (int i = 0; i < uniforms.size(); i++) { - auto location = shader->uniforms_loc[i]; + auto location = shader->uniform_locations[i]; auto& uniform = uniforms[i]; // Sampler 2D @@ -1340,7 +1298,6 @@ namespace Blah ((int)call.blend.mask & (int)BlendMask::Blue), ((int)call.blend.mask & (int)BlendMask::Alpha)); - unsigned char r = call.blend.rgba >> 24; unsigned char g = call.blend.rgba >> 16; unsigned char b = call.blend.rgba >> 8; diff --git a/private/blah/internal/platform_backend_sdl2.cpp b/private/blah/internal/platform_backend_sdl2.cpp index 1e3b545..95d020e 100644 --- a/private/blah/internal/platform_backend_sdl2.cpp +++ b/private/blah/internal/platform_backend_sdl2.cpp @@ -396,7 +396,6 @@ void PlatformBackend::set_size(int width, int height) void PlatformBackend::get_draw_size(int* width, int* height) { - auto config = App::config(); if (GraphicsBackend::renderer() == GraphicsRenderer::OpenGL) { SDL_GL_GetDrawableSize(window, width, height); diff --git a/public/blah/drawing/batch.cpp b/public/blah/drawing/batch.cpp index 0dce22e..baf041a 100644 --- a/public/blah/drawing/batch.cpp +++ b/public/blah/drawing/batch.cpp @@ -5,6 +5,7 @@ #include #include #include +#include using namespace Blah; diff --git a/public/blah/drawing/spritefont.cpp b/public/blah/drawing/spritefont.cpp index a5204b8..ad98d97 100644 --- a/public/blah/drawing/spritefont.cpp +++ b/public/blah/drawing/spritefont.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include using namespace Blah; diff --git a/public/blah/graphics/framebuffer.h b/public/blah/graphics/framebuffer.h index 05cfd42..aa8b119 100644 --- a/public/blah/graphics/framebuffer.h +++ b/public/blah/graphics/framebuffer.h @@ -1,9 +1,11 @@ #pragma once -#include #include #include #include +// 4 color attachments + 1 depth/stencil +#define BLAH_ATTACHMENTS 5 + namespace Blah { typedef StackVector Attachments; diff --git a/public/blah/graphics/graphics.cpp b/public/blah/graphics/graphics.cpp index c9fa50a..598a963 100644 --- a/public/blah/graphics/graphics.cpp +++ b/public/blah/graphics/graphics.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/public/blah/graphics/graphics.h b/public/blah/graphics/graphics.h index 135c0b2..df01165 100644 --- a/public/blah/graphics/graphics.h +++ b/public/blah/graphics/graphics.h @@ -1,78 +1,19 @@ #pragma once #include -#include -#include #include #include +#include +#include +#include +#include +#include #include -#define BLAH_ATTACHMENTS 5 // 4 color attachments + 1 depth/stencil -#define BLAH_UNIFORMS 16 // 16 shader uniforms -#define BLAH_ATTRIBUTES 16 // 16 shader attributes -#define BLAH_ATTRIBUTE_NAME 32 // max shader attribute name length -#define BLAH_UNIFORM_NAME 32 // max shader uniform name length - namespace Blah { class Stream; class Image; - class Texture; - typedef std::shared_ptr TextureRef; - - class FrameBuffer; - typedef std::shared_ptr FrameBufferRef; - - class Material; - typedef std::shared_ptr MaterialRef; - - class Shader; - typedef std::shared_ptr ShaderRef; - - class Mesh; - typedef std::shared_ptr MeshRef; - - enum class GraphicsRenderer - { - None = -1, - OpenGL, - D3D11, - Metal, - Count - }; - - struct GraphicsInfo - { - bool instancing = false; - bool origin_bottom_left = false; - int max_texture_size = 0; - }; - - enum class TextureFilter - { - None, - Linear, - Nearest - }; - - enum class TextureWrap - { - None, - Clamp, - Repeat - }; - - enum class TextureFormat - { - None, - R, - RG, - RGB, - RGBA, - DepthStencil, - Count - }; - enum class Compare { None, @@ -129,11 +70,11 @@ namespace Blah enum class BlendMask { None = 0, - Red = 1, + Red = 1, Green = 2, Blue = 4, Alpha = 8, - RGB = Red | Green | Blue, + RGB = Red | Green | Blue, RGBA = Red | Green | Blue | Alpha, }; @@ -185,78 +126,6 @@ namespace Blah static const BlendMode Subtract; }; - enum class UniformType - { - None, - Float, - Float2, - Float3, - Float4, - Mat3x2, - Mat4x4, - Texture - }; - - enum class VertexSemantics - { - None, - Position, - Normal, - Bitangent, - Color0, - Color1, - Color2, - Color3, - Indices, - Weight, - Texcoord0, - Texcoord1, - Texcoord2, - Texcoord3, - Texcoord4, - Texcoord5, - Texcoord6, - Texcoord7 - }; - - enum class VertexAttributeType - { - None, - Byte, - Short, - Int, - Float - }; - - struct VertexAttribute - { - int index; - VertexSemantics semantics; - VertexAttributeType type; - int components; - bool normalized; - }; - - struct ShaderData - { - const char* vertex; - const char* fragment; - }; - - struct ShaderUniform - { - StrOf name; - UniformType type; - int array_length; - }; - - struct ShaderAttribute - { - StrOf name; - StrOf semantic_name; - int semantic_location; - }; - struct RenderCall { // Framebuffer to draw to @@ -302,6 +171,22 @@ namespace Blah RenderCall(); }; + enum class GraphicsRenderer + { + None = -1, + OpenGL, + D3D11, + Metal, + Count + }; + + struct GraphicsInfo + { + bool instancing = false; + bool origin_bottom_left = false; + int max_texture_size = 0; + }; + namespace Graphics { const FrameBufferRef backbuffer; diff --git a/public/blah/graphics/material.cpp b/public/blah/graphics/material.cpp index 86ee593..6dcf9ec 100644 --- a/public/blah/graphics/material.cpp +++ b/public/blah/graphics/material.cpp @@ -5,7 +5,7 @@ using namespace Blah; namespace { - int calc_uniform_size(const ShaderUniform& uniform) + int calc_uniform_size(const UniformInfo& uniform) { int components = 0; @@ -31,8 +31,8 @@ Material::Material(const ShaderRef& shader) BLAH_ASSERT(shader, "Material is being created with an invalid shader"); m_shader = shader; - Uniforms uniforms = shader->uniforms(); - StackVector float_offsets; + auto& uniforms = shader->uniforms(); + Vector float_offsets; size_t float_size = 0; for (auto& uniform : uniforms) diff --git a/public/blah/graphics/mesh.h b/public/blah/graphics/mesh.h index b94f0a6..3c10b8c 100644 --- a/public/blah/graphics/mesh.h +++ b/public/blah/graphics/mesh.h @@ -1,10 +1,49 @@ #pragma once #include -#include #include namespace Blah { + enum class VertexSemantics + { + None, + Position, + Normal, + Bitangent, + Color0, + Color1, + Color2, + Color3, + Indices, + Weight, + Texcoord0, + Texcoord1, + Texcoord2, + Texcoord3, + Texcoord4, + Texcoord5, + Texcoord6, + Texcoord7 + }; + + enum class VertexAttributeType + { + None, + Byte, + Short, + Int, + Float + }; + + struct VertexAttribute + { + int index; + VertexSemantics semantics; + VertexAttributeType type; + int components; + bool normalized; + }; + class Mesh { public: diff --git a/public/blah/graphics/shader.h b/public/blah/graphics/shader.h index 8ecd1fe..165b42c 100644 --- a/public/blah/graphics/shader.h +++ b/public/blah/graphics/shader.h @@ -1,12 +1,34 @@ #pragma once -#include #include +#include #include namespace Blah { - typedef StackVector Uniforms; - typedef StackVector Attributes; + enum class UniformType + { + None, + Float, + Float2, + Float3, + Float4, + Mat3x2, + Mat4x4, + Texture + }; + + struct UniformInfo + { + String name; + UniformType type; + int array_length; + }; + + struct ShaderData + { + const char* vertex; + const char* fragment; + }; class Shader { @@ -14,16 +36,10 @@ namespace Blah virtual ~Shader() = default; // Gets a list of Shader Uniforms from Shader - virtual Uniforms& uniforms() = 0; + virtual Vector& uniforms() = 0; // Gets a list of Shader Uniforms from Shader - virtual const Uniforms& uniforms() const = 0; - - // Gets a list of Shader Attributes from Shader - virtual Attributes& attributes() = 0; - - // Gets a list of Shader Attributes from Shader - virtual const Attributes& attributes() const = 0; + virtual const Vector& uniforms() const = 0; }; typedef std::shared_ptr ShaderRef; diff --git a/public/blah/graphics/texture.h b/public/blah/graphics/texture.h index ac1fb71..47d805a 100644 --- a/public/blah/graphics/texture.h +++ b/public/blah/graphics/texture.h @@ -1,9 +1,33 @@ #pragma once -#include #include namespace Blah { + enum class TextureFilter + { + None, + Linear, + Nearest + }; + + enum class TextureWrap + { + None, + Clamp, + Repeat + }; + + enum class TextureFormat + { + None, + R, + RG, + RGB, + RGBA, + DepthStencil, + Count + }; + class Texture { public: diff --git a/public/blah/math/rect.cpp b/public/blah/math/rect.cpp index db39da5..8f7dbc1 100644 --- a/public/blah/math/rect.cpp +++ b/public/blah/math/rect.cpp @@ -156,6 +156,11 @@ bool Rect::contains(const Vec2& pt) const return pt.x >= x && pt.x < x + w && pt.y >= y && pt.y < y + h; } +bool Rect::overlaps(const Rect& rect) const +{ + return x + w >= rect.x && y + h >= rect.y && x < rect.x + rect.w && y < rect.y + rect.h; +} + Rect Rect::overlap_rect(const Rect& against) const { Rect result(0, 0, 0, 0); diff --git a/public/blah/math/rect.h b/public/blah/math/rect.h index bfdbc83..1b9714d 100644 --- a/public/blah/math/rect.h +++ b/public/blah/math/rect.h @@ -50,7 +50,7 @@ namespace Blah bool contains(const Point& pt) const; bool contains(const Vec2& pt) const; - bool overlaps(const Rect& rect) const { return x + w >= rect.x && y + h >= rect.y && x < rect.x + rect.w && y < rect.y + rect.h; } + bool overlaps(const Rect& rect) const; Rect overlap_rect(const Rect& other) const; bool intersects(const Line& line) const;