more graphics cleanup - removing Shader Attribute Info

This commit is contained in:
Noel Berry 2020-12-24 03:01:05 -08:00
parent 4c4d495fa8
commit 134d26040c
13 changed files with 183 additions and 253 deletions

View File

@ -1,14 +1,10 @@
#ifdef BLAH_USE_OPENGL #ifdef BLAH_USE_OPENGL
#include <blah/graphics/graphics.h> #include <blah/graphics/graphics.h>
#include <blah/internal/graphics_backend.h> #include <blah/internal/graphics_backend.h>
#include <blah/internal/platform_backend.h> #include <blah/internal/platform_backend.h>
#include <blah/graphics/texture.h>
#include <blah/graphics/framebuffer.h>
#include <blah/graphics/mesh.h>
#include <blah/graphics/shader.h>
#include <blah/graphics/material.h>
#include <blah/containers/stackvector.h>
#include <blah/log.h> #include <blah/log.h>
#include <blah/app.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stddef.h> #include <stddef.h>
@ -346,25 +342,29 @@ typedef void (APIENTRY* DEBUGPROC)(GLenum source,
namespace Blah namespace Blah
{ {
// GL function pointers struct State
struct GL
{ {
// GL function pointers
#define GL_FUNC(name, ret, ...) typedef ret (*name ## Func) (__VA_ARGS__); name ## Func name; #define GL_FUNC(name, ret, ...) typedef ret (*name ## Func) (__VA_ARGS__); name ## Func name;
GL_FUNCTIONS GL_FUNCTIONS
#undef GL_FUNC #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 // static state
GL gl; State 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;
// debug callback // debug callback
void APIENTRY gl_message_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) 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_format = GL_RED;
m_gl_type = GL_UNSIGNED_BYTE; 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; return;
} }
@ -757,11 +757,10 @@ namespace Blah
{ {
private: private:
GLuint m_id; GLuint m_id;
Attributes m_attributes; Vector<UniformInfo> m_uniforms;
Uniforms m_uniforms;
public: public:
GLint uniforms_loc[BLAH_UNIFORMS] = { 0 }; Vector<GLint> uniform_locations;
OpenGL_Shader(const ShaderData* data) OpenGL_Shader(const ShaderData* data)
{ {
@ -833,56 +832,25 @@ namespace Blah
// ready to go // ready to go
m_id = id; 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 // get uniforms
{ {
const int max_name_length = 256;
GLint active_uniforms = 0; GLint active_uniforms = 0;
gl.GetProgramiv(id, GL_ACTIVE_UNIFORMS, &active_uniforms); 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++) for (int i = 0; i < active_uniforms; i++)
{ {
GLsizei length; GLsizei length;
GLsizei size; GLsizei size;
GLenum type; 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'; 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
for (int n = 0; n < BLAH_UNIFORM_NAME; n++) for (int n = 0; n < max_name_length; n++)
if (name[n] == '[') if (name[n] == '[')
{ {
if (name[n + 1] == '0' && name[n + 2] == ']') if (name[n + 1] == '0' && name[n + 2] == ']')
@ -892,11 +860,11 @@ namespace Blah
} }
} }
ShaderUniform uniform; UniformInfo uniform;
uniform.name = name; uniform.name = name;
uniform.type = UniformType::None; uniform.type = UniformType::None;
uniform.array_length = size; uniform.array_length = size;
uniforms_loc[i] = gl.GetUniformLocation(id, name); uniform_locations.push_back(gl.GetUniformLocation(id, name));
if (type == GL_FLOAT) if (type == GL_FLOAT)
uniform.type = UniformType::Float; uniform.type = UniformType::Float;
@ -935,25 +903,15 @@ namespace Blah
return m_id; return m_id;
} }
virtual Uniforms& uniforms() override virtual Vector<UniformInfo>& uniforms() override
{ {
return m_uniforms; return m_uniforms;
} }
virtual const Uniforms& uniforms() const override virtual const Vector<UniformInfo>& uniforms() const override
{ {
return m_uniforms; return m_uniforms;
} }
virtual Attributes& attributes() override
{
return m_attributes;
}
virtual const Attributes& attributes() const override
{
return m_attributes;
}
}; };
class OpenGL_Mesh : public Mesh class OpenGL_Mesh : public Mesh
@ -970,8 +928,8 @@ namespace Blah
uint16_t m_instance_size; uint16_t m_instance_size;
uint8_t m_vertex_attribs_enabled; uint8_t m_vertex_attribs_enabled;
uint8_t m_instance_attribs_enabled; uint8_t m_instance_attribs_enabled;
GLuint m_vertex_attribs[BLAH_ATTRIBUTES]; Vector<GLuint> m_vertex_attribs;
GLuint m_instance_attribs[BLAH_ATTRIBUTES]; Vector<GLuint> m_instance_attribs;
public: public:
@ -988,8 +946,6 @@ namespace Blah
m_instance_size = 0; m_instance_size = 0;
m_vertex_attribs_enabled = 0; m_vertex_attribs_enabled = 0;
m_instance_attribs_enabled = 0; m_instance_attribs_enabled = 0;
m_vertex_attribs[0] = 0;
m_instance_attribs[0] = 0;
gl.GenVertexArrays(1, &m_id); gl.GenVertexArrays(1, &m_id);
} }
@ -1107,17 +1063,19 @@ namespace Blah
bool GraphicsBackend::init() bool GraphicsBackend::init()
{ {
gl = State();
// create gl context // create gl context
gl_context = PlatformBackend::gl_context_create(); gl.context = PlatformBackend::gl_context_create();
if (gl_context == nullptr) if (gl.context == nullptr)
{ {
Log::error("Failed to create OpenGL Context"); Log::error("Failed to create OpenGL Context");
return false; return false;
} }
PlatformBackend::gl_context_make_current(gl_context); PlatformBackend::gl_context_make_current(gl.context);
// bind opengl functions // 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 GL_FUNCTIONS
#undef GL_FUNC #undef GL_FUNC
@ -1130,13 +1088,13 @@ namespace Blah
} }
// get opengl info // get opengl info
gl.GetIntegerv(0x8CDF, &gl_max_color_attachments); gl.GetIntegerv(0x8CDF, &gl.max_color_attachments);
gl.GetIntegerv(0x80E9, &gl_max_element_indices); gl.GetIntegerv(0x80E9, &gl.max_element_indices);
gl.GetIntegerv(0x80E8, &gl_max_element_vertices); gl.GetIntegerv(0x80E8, &gl.max_element_vertices);
gl.GetIntegerv(0x84E8, &gl_max_renderbuffer_size); gl.GetIntegerv(0x84E8, &gl.max_renderbuffer_size);
gl.GetIntegerv(0x8D57, &gl_max_samples); gl.GetIntegerv(0x8D57, &gl.max_samples);
gl.GetIntegerv(0x8872, &gl_max_texture_image_units); gl.GetIntegerv(0x8872, &gl.max_texture_image_units);
gl.GetIntegerv(0x0D33, &gl_max_texture_size); gl.GetIntegerv(0x0D33, &gl.max_texture_size);
// log // log
Log::print("OpenGL %s, %s", Log::print("OpenGL %s, %s",
@ -1148,9 +1106,9 @@ namespace Blah
gl.PixelStorei(GL_UNPACK_ALIGNMENT, 1); gl.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
// assign info // assign info
gl_info.instancing = true; gl.info.instancing = true;
gl_info.origin_bottom_left = true; gl.info.origin_bottom_left = true;
gl_info.max_texture_size = gl_max_texture_size; gl.info.max_texture_size = gl.max_texture_size;
return true; return true;
} }
@ -1162,13 +1120,13 @@ namespace Blah
void GraphicsBackend::shutdown() void GraphicsBackend::shutdown()
{ {
PlatformBackend::gl_context_destroy(gl_context); PlatformBackend::gl_context_destroy(gl.context);
gl_context = nullptr; gl.context = nullptr;
} }
const GraphicsInfo* GraphicsBackend::info() const GraphicsInfo* GraphicsBackend::info()
{ {
return &gl_info; return &gl.info;
} }
void GraphicsBackend::frame() {} void GraphicsBackend::frame() {}
@ -1261,7 +1219,7 @@ namespace Blah
auto& uniforms = shader->uniforms(); auto& uniforms = shader->uniforms();
for (int i = 0; i < uniforms.size(); i++) for (int i = 0; i < uniforms.size(); i++)
{ {
auto location = shader->uniforms_loc[i]; auto location = shader->uniform_locations[i];
auto& uniform = uniforms[i]; auto& uniform = uniforms[i];
// Sampler 2D // Sampler 2D
@ -1340,7 +1298,6 @@ namespace Blah
((int)call.blend.mask & (int)BlendMask::Blue), ((int)call.blend.mask & (int)BlendMask::Blue),
((int)call.blend.mask & (int)BlendMask::Alpha)); ((int)call.blend.mask & (int)BlendMask::Alpha));
unsigned char r = call.blend.rgba >> 24; unsigned char r = call.blend.rgba >> 24;
unsigned char g = call.blend.rgba >> 16; unsigned char g = call.blend.rgba >> 16;
unsigned char b = call.blend.rgba >> 8; unsigned char b = call.blend.rgba >> 8;

View File

@ -396,7 +396,6 @@ void PlatformBackend::set_size(int width, int height)
void PlatformBackend::get_draw_size(int* width, int* height) void PlatformBackend::get_draw_size(int* width, int* height)
{ {
auto config = App::config();
if (GraphicsBackend::renderer() == GraphicsRenderer::OpenGL) if (GraphicsBackend::renderer() == GraphicsRenderer::OpenGL)
{ {
SDL_GL_GetDrawableSize(window, width, height); SDL_GL_GetDrawableSize(window, width, height);

View File

@ -5,6 +5,7 @@
#include <blah/graphics/shader.h> #include <blah/graphics/shader.h>
#include <blah/graphics/material.h> #include <blah/graphics/material.h>
#include <blah/math/calc.h> #include <blah/math/calc.h>
#include <blah/app.h>
using namespace Blah; using namespace Blah;

View File

@ -1,6 +1,7 @@
#include <blah/drawing/spritefont.h> #include <blah/drawing/spritefont.h>
#include <blah/images/font.h> #include <blah/images/font.h>
#include <blah/images/packer.h> #include <blah/images/packer.h>
#include <blah/graphics/graphics.h>
#include <blah/log.h> #include <blah/log.h>
using namespace Blah; using namespace Blah;

View File

@ -1,9 +1,11 @@
#pragma once #pragma once
#include <blah/graphics/graphics.h>
#include <blah/graphics/texture.h> #include <blah/graphics/texture.h>
#include <blah/containers/stackvector.h> #include <blah/containers/stackvector.h>
#include <memory> #include <memory>
// 4 color attachments + 1 depth/stencil
#define BLAH_ATTACHMENTS 5
namespace Blah namespace Blah
{ {
typedef StackVector<TextureRef, BLAH_ATTACHMENTS> Attachments; typedef StackVector<TextureRef, BLAH_ATTACHMENTS> Attachments;

View File

@ -5,6 +5,7 @@
#include <blah/graphics/material.h> #include <blah/graphics/material.h>
#include <blah/graphics/shader.h> #include <blah/graphics/shader.h>
#include <blah/log.h> #include <blah/log.h>
#include <blah/app.h>
#include <blah/internal/graphics_backend.h> #include <blah/internal/graphics_backend.h>
#include <blah/images/image.h> #include <blah/images/image.h>
#include <string.h> #include <string.h>

View File

@ -1,78 +1,19 @@
#pragma once #pragma once
#include <inttypes.h> #include <inttypes.h>
#include <blah/app.h>
#include <blah/math/point.h>
#include <blah/math/rect.h> #include <blah/math/rect.h>
#include <blah/containers/str.h> #include <blah/containers/str.h>
#include <blah/graphics/texture.h>
#include <blah/graphics/framebuffer.h>
#include <blah/graphics/mesh.h>
#include <blah/graphics/shader.h>
#include <blah/graphics/material.h>
#include <memory> #include <memory>
#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 namespace Blah
{ {
class Stream; class Stream;
class Image; class Image;
class Texture;
typedef std::shared_ptr<Texture> TextureRef;
class FrameBuffer;
typedef std::shared_ptr<FrameBuffer> FrameBufferRef;
class Material;
typedef std::shared_ptr<Material> MaterialRef;
class Shader;
typedef std::shared_ptr<Shader> ShaderRef;
class Mesh;
typedef std::shared_ptr<Mesh> 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 enum class Compare
{ {
None, None,
@ -129,11 +70,11 @@ namespace Blah
enum class BlendMask enum class BlendMask
{ {
None = 0, None = 0,
Red = 1, Red = 1,
Green = 2, Green = 2,
Blue = 4, Blue = 4,
Alpha = 8, Alpha = 8,
RGB = Red | Green | Blue, RGB = Red | Green | Blue,
RGBA = Red | Green | Blue | Alpha, RGBA = Red | Green | Blue | Alpha,
}; };
@ -185,78 +126,6 @@ namespace Blah
static const BlendMode Subtract; 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<BLAH_UNIFORM_NAME> name;
UniformType type;
int array_length;
};
struct ShaderAttribute
{
StrOf<BLAH_ATTRIBUTE_NAME> name;
StrOf<BLAH_ATTRIBUTE_NAME> semantic_name;
int semantic_location;
};
struct RenderCall struct RenderCall
{ {
// Framebuffer to draw to // Framebuffer to draw to
@ -302,6 +171,22 @@ namespace Blah
RenderCall(); 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 namespace Graphics
{ {
const FrameBufferRef backbuffer; const FrameBufferRef backbuffer;

View File

@ -5,7 +5,7 @@ using namespace Blah;
namespace namespace
{ {
int calc_uniform_size(const ShaderUniform& uniform) int calc_uniform_size(const UniformInfo& uniform)
{ {
int components = 0; int components = 0;
@ -31,8 +31,8 @@ Material::Material(const ShaderRef& shader)
BLAH_ASSERT(shader, "Material is being created with an invalid shader"); BLAH_ASSERT(shader, "Material is being created with an invalid shader");
m_shader = shader; m_shader = shader;
Uniforms uniforms = shader->uniforms(); auto& uniforms = shader->uniforms();
StackVector<size_t, BLAH_UNIFORMS> float_offsets; Vector<size_t> float_offsets;
size_t float_size = 0; size_t float_size = 0;
for (auto& uniform : uniforms) for (auto& uniform : uniforms)

View File

@ -1,10 +1,49 @@
#pragma once #pragma once
#include <inttypes.h> #include <inttypes.h>
#include <blah/graphics/graphics.h>
#include <memory> #include <memory>
namespace Blah 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 class Mesh
{ {
public: public:

View File

@ -1,12 +1,34 @@
#pragma once #pragma once
#include <blah/graphics/graphics.h>
#include <blah/containers/stackvector.h> #include <blah/containers/stackvector.h>
#include <blah/containers/str.h>
#include <memory> #include <memory>
namespace Blah namespace Blah
{ {
typedef StackVector<ShaderUniform, BLAH_UNIFORMS> Uniforms; enum class UniformType
typedef StackVector<ShaderAttribute, BLAH_ATTRIBUTES> Attributes; {
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 class Shader
{ {
@ -14,16 +36,10 @@ namespace Blah
virtual ~Shader() = default; virtual ~Shader() = default;
// Gets a list of Shader Uniforms from Shader // Gets a list of Shader Uniforms from Shader
virtual Uniforms& uniforms() = 0; virtual Vector<UniformInfo>& uniforms() = 0;
// Gets a list of Shader Uniforms from Shader // Gets a list of Shader Uniforms from Shader
virtual const Uniforms& uniforms() const = 0; virtual const Vector<UniformInfo>& 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;
}; };
typedef std::shared_ptr<Shader> ShaderRef; typedef std::shared_ptr<Shader> ShaderRef;

View File

@ -1,9 +1,33 @@
#pragma once #pragma once
#include <blah/graphics/graphics.h>
#include <memory> #include <memory>
namespace Blah 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 class Texture
{ {
public: public:

View File

@ -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; 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 Rect::overlap_rect(const Rect& against) const
{ {
Rect result(0, 0, 0, 0); Rect result(0, 0, 0, 0);

View File

@ -50,7 +50,7 @@ namespace Blah
bool contains(const Point& pt) const; bool contains(const Point& pt) const;
bool contains(const Vec2& 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; Rect overlap_rect(const Rect& other) const;
bool intersects(const Line& line) const; bool intersects(const Line& line) const;