refactored graphics & streams into single files - easier to maintain & read

This commit is contained in:
Noel Berry
2022-02-12 12:19:53 -08:00
parent 9c7d8a4418
commit 99595f265f
53 changed files with 1323 additions and 1560 deletions

View File

@ -5,22 +5,16 @@
#include "blah/filesystem.h"
#include "blah/time.h"
#include "blah/input.h"
#include "blah/stream.h"
#include "blah/graphics.h"
#include "blah/containers/vector.h"
#include "blah/containers/stackvector.h"
#include "blah/containers/str.h"
#include "blah/graphics/batch.h"
#include "blah/graphics/spritefont.h"
#include "blah/graphics/subtexture.h"
#include "blah/graphics/blend.h"
#include "blah/graphics/material.h"
#include "blah/graphics/mesh.h"
#include "blah/graphics/renderpass.h"
#include "blah/graphics/sampler.h"
#include "blah/graphics/shader.h"
#include "blah/graphics/target.h"
#include "blah/graphics/texture.h"
#include "blah/drawing/batch.h"
#include "blah/drawing/spritefont.h"
#include "blah/drawing/subtexture.h"
#include "blah/images/aseprite.h"
#include "blah/images/font.h"
@ -30,9 +24,4 @@
#include "blah/math/calc.h"
#include "blah/math/spatial.h"
#include "blah/math/color.h"
#include "blah/math/ease.h"
#include "blah/streams/bufferstream.h"
#include "blah/streams/filestream.h"
#include "blah/streams/memorystream.h"
#include "blah/streams/stream.h"
#include "blah/math/ease.h"

View File

@ -1,5 +1,6 @@
#pragma once
#include <blah/common.h>
#include <blah/graphics.h>
#include <blah/math/spatial.h>
namespace Blah
@ -10,31 +11,6 @@ namespace Blah
// Application Logging Functions
using AppLogFn = Func<void, const char*, Log::Category>;
// Type of Renderer the Application is using
enum class RendererType
{
None = -1,
OpenGL,
D3D11,
};
// Renderer Information
struct RendererInfo
{
// The type of Renderer being used
RendererType type = RendererType::None;
// Whether Mesh Instancing is available
bool instancing = false;
// Whether the Texture origin is the bottom left.
// This is true for OpenGL.
bool origin_bottom_left = false;
// Maximum Texture Size available
int max_texture_size = 0;
};
// Application Configuration
struct Config
{
@ -84,10 +60,6 @@ namespace Blah
AppLogFn on_log = nullptr;
};
// Forward declare Target for the BackBuffer
class Target;
using TargetRef = Ref<Target>;
// Application
namespace App
{

View File

@ -1,14 +1,11 @@
#pragma once
#include <blah/containers/vector.h>
#include <blah/containers/str.h>
#include <blah/drawing/spritefont.h>
#include <blah/drawing/subtexture.h>
#include <blah/math/spatial.h>
#include <blah/math/color.h>
#include <blah/graphics/subtexture.h>
#include <blah/graphics/spritefont.h>
#include <blah/containers/vector.h>
#include <blah/graphics/blend.h>
#include <blah/graphics/sampler.h>
#include <blah/graphics/renderpass.h>
#include <blah/app.h>
#include <blah/graphics.h>
namespace Blah
{
@ -235,6 +232,6 @@ namespace Blah
Vector<DrawBatch> m_batches;
int m_batch_insert = 0;
void render_single_batch(RenderPass& pass, const DrawBatch& b, const Mat4x4f& matrix);
void render_single_batch(DrawCall& pass, const DrawBatch& b, const Mat4x4f& matrix);
};
}

View File

@ -2,10 +2,8 @@
#include <blah/common.h>
#include <blah/containers/str.h>
#include <blah/containers/vector.h>
#include <blah/graphics/subtexture.h>
#include <blah/drawing/subtexture.h>
#include <blah/images/font.h>
#include <blah/math/spatial.h>
#include <blah/filesystem.h>
namespace Blah
{

View File

@ -1,5 +1,5 @@
#pragma once
#include <blah/graphics/texture.h>
#include <blah/graphics.h>
#include <blah/math/spatial.h>
namespace Blah

View File

@ -42,6 +42,9 @@ namespace Blah
// Default Destructor
virtual ~File() = default;
// Gets the Mode the File was opened with
FileMode mode() const;
// Gets the File Length
virtual size_t length() = 0;
@ -52,10 +55,13 @@ namespace Blah
virtual size_t seek(size_t position) = 0;
// Reads from the File into the buffer, and returns how many bytes were successfully read
virtual size_t read(unsigned char* buffer, size_t length) = 0;
virtual size_t read(void* buffer, size_t length) = 0;
// Writes from the buffer into the File, nd returns how many bytes were successfully written
virtual size_t write(const unsigned char* buffer, size_t length) = 0;
virtual size_t write(const void* buffer, size_t length) = 0;
private:
FileMode m_mode;
};
namespace Directory

663
include/blah/graphics.h Normal file
View File

@ -0,0 +1,663 @@
#pragma once
#include <blah/common.h>
#include <blah/containers/vector.h>
#include <blah/containers/stackvector.h>
#include <blah/containers/str.h>
#include <blah/math/spatial.h>
#include <blah/images/image.h>
#include <blah/stream.h>
namespace Blah
{
class Shader; using ShaderRef = Ref<Shader>;
class Texture; using TextureRef = Ref<Texture>;
class Target; using TargetRef = Ref<Target>;
class Mesh; using MeshRef = Ref<Mesh>;
class Material; using MaterialRef = Ref<Material>;
// Type of Renderer the Application is using
enum class RendererType
{
None = -1,
OpenGL,
D3D11,
};
// Renderer Information
struct RendererInfo
{
// The type of Renderer being used
RendererType type = RendererType::None;
// Whether Mesh Instancing is available
bool instancing = false;
// Whether the Texture origin is the bottom left.
// This is true for OpenGL.
bool origin_bottom_left = false;
// Maximum Texture Size available
int max_texture_size = 0;
};
// Depth comparison function to use during a draw call
enum class Compare
{
None,
Always,
Never,
Less,
Equal,
LessOrEqual,
Greater,
NotEqual,
GreatorOrEqual
};
// Cull mode during a draw call
enum class Cull
{
// No Culling enabled
None = 0,
// Cull front faces
Front = 1,
// Cull back faces
Back = 2,
};
enum class BlendOp
{
Add,
Subtract,
ReverseSubtract,
Min,
Max
};
enum class BlendFactor
{
Zero,
One,
SrcColor,
OneMinusSrcColor,
DstColor,
OneMinusDstColor,
SrcAlpha,
OneMinusSrcAlpha,
DstAlpha,
OneMinusDstAlpha,
ConstantColor,
OneMinusConstantColor,
ConstantAlpha,
OneMinusConstantAlpha,
SrcAlphaSaturate,
Src1Color,
OneMinusSrc1Color,
Src1Alpha,
OneMinusSrc1Alpha
};
enum class BlendMask
{
None = 0,
Red = 1,
Green = 2,
Blue = 4,
Alpha = 8,
RGB = Red | Green | Blue,
RGBA = Red | Green | Blue | Alpha,
};
// BlendMode using for rendering
struct BlendMode
{
// Normal, pre-multiplied, Blend Mode
static const BlendMode Normal;
// Subtractive Blend Mode
static const BlendMode Subtract;
BlendOp color_op;
BlendFactor color_src;
BlendFactor color_dst;
BlendOp alpha_op;
BlendFactor alpha_src;
BlendFactor alpha_dst;
BlendMask mask;
u32 rgba;
BlendMode() = default;
BlendMode(BlendOp op, BlendFactor src, BlendFactor dst) :
color_op(op), color_src(src), color_dst(dst),
alpha_op(op), alpha_src(src), alpha_dst(dst),
mask(BlendMask::RGBA), rgba(0xffffffff) {}
BlendMode(
BlendOp color_op, BlendFactor color_src, BlendFactor color_dst,
BlendOp alpha_op, BlendFactor alpha_src, BlendFactor alpha_dst,
BlendMask blend_mask, u32 blend_rgba) :
color_op(color_op), color_src(color_src), color_dst(color_dst),
alpha_op(alpha_op), alpha_src(alpha_src), alpha_dst(alpha_dst),
mask(blend_mask), rgba(blend_rgba) {}
constexpr bool operator==(const BlendMode& rhs) const
{
return
color_op == rhs.color_op && color_src == rhs.color_src && color_dst == rhs.color_dst &&
alpha_op == rhs.alpha_op && alpha_src == rhs.alpha_src && alpha_dst == rhs.alpha_dst &&
mask == rhs.mask && rgba == rhs.rgba;
}
constexpr bool operator!=(const BlendMode& rhs) const
{
return !(*this == rhs);
}
};
// Texture filter
enum class TextureFilter
{
None, // Will fallback to whatever default the driver sets
Linear, // Linear interpolation
Nearest // Nearest Neighbour interpolation
};
// Texture Wrap Mode
enum class TextureWrap
{
None, // Will fallback to whatever default the driver sets
Clamp, // Clamps the texture to the edges
Repeat // Repeats the texture
};
// Texture Sampler State, applied during rendering
struct TextureSampler
{
// Filter Mode
TextureFilter filter;
// Wrap X Mode
TextureWrap wrap_x;
// Wrap Y Mode
TextureWrap wrap_y;
TextureSampler() :
filter(TextureFilter::Linear), wrap_x(TextureWrap::Repeat), wrap_y(TextureWrap::Repeat) {}
TextureSampler(TextureFilter filter) :
filter(filter), wrap_x(TextureWrap::Repeat), wrap_y(TextureWrap::Repeat) {}
TextureSampler(TextureFilter filter, TextureWrap wrap_x, TextureWrap wrap_y) :
filter(filter), wrap_x(wrap_x), wrap_y(wrap_y) {}
bool operator==(const TextureSampler& rhs) const
{
return filter == rhs.filter && wrap_x == rhs.wrap_x && wrap_y == rhs.wrap_y;
}
bool operator!=(const TextureSampler& rhs) const
{
return !(*this == rhs);
}
};
enum class TextureFormat
{
None, // Invalid Format
R, // Single 8-bit channe;
RG, // 2 8-bit channels
RGBA, // 4 8-bit channels
DepthStencil, // Depth 24, Stencil 8
Count // Total Formats
};
enum class ClearMask
{
None = 0,
Color = 1,
Depth = 2,
Stencil = 4,
All = (int)Color | (int)Depth | (int)Stencil
};
// Supported Uniform Types
enum class UniformType
{
None,
Float,
Float2,
Float3,
Float4,
Mat3x2,
Mat4x4,
Texture2D,
Sampler2D
};
// Supported Shader Types
enum class ShaderType
{
None = 0,
Vertex = 1 << 0,
Fragment = 1 << 1
};
// Uniform Info, provided by the Shader
struct UniformInfo
{
// Name of the Uniform
String name;
// The Value type of the Uniform
UniformType type;
// The Shader type the Uniform is a part of
ShaderType shader;
// Some rendering APIs have uniform buffers. The `buffer_index`
// specifies which buffer the uniform belongs to
int buffer_index;
// Array length of the Uniform (ex. a vec2[4] would be 4)
int array_length;
};
// Supported Vertex value types
enum class VertexType
{
None,
Float,
Float2,
Float3,
Float4,
Byte4,
UByte4,
Short2,
UShort2,
Short4,
UShort4
};
// Vertex Attribute information
struct VertexAttribute
{
// Location / Attribute Index
int index = 0;
// Vertex Type
VertexType type = VertexType::None;
// Whether the Vertex should be normalized (doesn't apply to Floats)
bool normalized = false;
};
// Vertex Format information.
// Holds a list of attributes and total stride per-vertex.
struct VertexFormat
{
// List of Attributes
StackVector<VertexAttribute, 16> attributes;
// Total size in bytes of each Vertex element
int stride = 0;
VertexFormat() = default;
VertexFormat(const StackVector<VertexAttribute, 16>& attributes, int stride = 0);
};
// Supported Vertex Index formats
enum class IndexFormat
{
// Indices are 16 bit unsigned integers
UInt16,
// Indices are 32 bit unsigned integers
UInt32
};
// Data to be passed to the shader to construct it
struct ShaderData
{
struct HLSL_Attribute
{
// Semantic Name
const char* semantic_name = nullptr;
// (optional) Semantic Index
int semantic_index = 0;
};
// Vertex Shader Program data
String vertex;
// Fragment Shader Program data
String fragment;
// HLSL Attributes - required for D3D11
StackVector<HLSL_Attribute, 16> hlsl_attributes;
};
// A shader used during Rendering
class Shader
{
protected:
Shader() = default;
public:
// Copy / Moves not allowed
Shader(const Shader&) = delete;
Shader(Shader&&) = delete;
Shader& operator=(const Shader&) = delete;
Shader& operator=(Shader&&) = delete;
// Default Destructor
virtual ~Shader() = default;
// Creates a Shader with the given Shader Data.
// If the Shader creation fails, it will return an invalid ShaderRef.
static ShaderRef create(const ShaderData& data);
// Gets a list of Shader Uniforms from Shader
virtual Vector<UniformInfo>& uniforms() = 0;
// Gets a list of Shader Uniforms from Shader
virtual const Vector<UniformInfo>& uniforms() const = 0;
};
// A 2D Texture held by the GPU to be used during rendering
class Texture
{
protected:
Texture() = default;
public:
// Copy / Moves not allowed
Texture(const Texture&) = delete;
Texture(Texture&&) = delete;
Texture& operator=(const Texture&) = delete;
Texture& operator=(Texture&&) = delete;
// Default Destructor
virtual ~Texture() = default;
// Creates a new Texture.
// If the Texture creation fails, it will return an invalid TextureRef.
static TextureRef create(const Image& image);
// Creates a new Texture.
// If image data is provided, it should be the full size of the texture.
// If the Texture creation fails, it will return an invalid TextureRef.
static TextureRef create(int width, int height, TextureFormat format, unsigned char* data = nullptr);
// Creates a new Texture from a Stream.
// If the Texture creation fails, it will return an invalid TextureRef.
static TextureRef create(Stream& stream);
// Creates a new Texture from a File.
// If the Texture creation fails, it will return an invalid TextureRef.
static TextureRef create(const FilePath& file);
// gets the width of the texture
virtual int width() const = 0;
// gets the height of the texture
virtual int height() const = 0;
// Gets the format of the Texture
virtual TextureFormat format() const = 0;
// Sets the data of the Texture.
// Note that the data should be the same format and size as the Texture. There is no row padding.
virtual void set_data(const u8* data) = 0;
// Sets the data of the Texture to the provided Color buffer.
// If the Texture Format is not RGBA, this won't do anything.
void set_data(const Color* data);
// Gets the data of the Texture.
// Note that the data will be written to in the same format as the Texture,
// and you should allocate enough space for the full texture. There is no row padding.
virtual void get_data(u8* data) = 0;
// Gets the data of the Texture.
// If the Texture Format is not RGBA, this won't do anything.
void get_data(Color* data);
// Returns true if the Texture is part of a FrameBuffer
virtual bool is_framebuffer() const = 0;
};
// Up to 4 color textures + 1 depth/stencil
using Attachments = StackVector<TextureRef, 5>;
using AttachmentFormats = StackVector<TextureFormat, 5>;
// Target is a 2D Buffer that can be drawn to.
// It can hold up to 4 color Textures, and 1 Depth/Stencil Texture.
class Target
{
protected:
Target() = default;
public:
// Copy / Moves not allowed
Target(const Target&) = delete;
Target(Target&&) = delete;
Target& operator=(const Target&) = delete;
Target& operator=(Target&&) = delete;
// Default Destructor
virtual ~Target() = default;
// Creates a new Target with a single Color texture
// If the Target creation fails, it will return an invalid TargetRef.
static TargetRef create(int width, int height);
// Creates a new Target with the given Texture Attachments. You must provide at least one Attachment.
// If the Target creation fails, it will return an invalid TargetRef.
static TargetRef create(int width, int height, const AttachmentFormats& textures);
// Gets the list of Attachments from the Target
virtual Attachments& textures() = 0;
// Gets the list of Attachments from the Target
virtual const Attachments& textures() const = 0;
// Gets the Attachment at a given index from the Target
TextureRef& texture(int index);
// Gets the Attachment at a given index from the Target
const TextureRef& texture(int index) const;
// Gets the width of the Target
virtual int width() const;
// Gets the height of the Target
virtual int height() const;
// Clears the Target
virtual void clear(Color color = Color::black, float depth = 1.0f, u8 stencil = 0, ClearMask mask = ClearMask::All) = 0;
};
// A Mesh is a set of Indices and Vertices which are used for drawing
class Mesh
{
protected:
Mesh() = default;
public:
// Copy / Moves not allowed
Mesh(const Mesh&) = delete;
Mesh(Mesh&&) = delete;
Mesh& operator=(const Mesh&) = delete;
Mesh& operator=(Mesh&&) = delete;
// Default Destructor
virtual ~Mesh() = default;
// Creates a new Mesh.
// If the Mesh creation fails, it will return an invalid Mesh.
static MeshRef create();
// Uploads the given index buffer to the Mesh
virtual void index_data(IndexFormat format, const void* indices, i64 count) = 0;
// Uploads the given vertex buffer to the Mesh
virtual void vertex_data(const VertexFormat& format, const void* vertices, i64 count) = 0;
// Uploads the given instance buffer to the Mesh
virtual void instance_data(const VertexFormat& format, const void* instances, i64 count) = 0;
// Gets the index count of the Mesh
virtual i64 index_count() const = 0;
// Gets the vertex count of the Mesh
virtual i64 vertex_count() const = 0;
// Gets the instance count of the Mesh
virtual i64 instance_count() const = 0;
};
// Materials hold values that can be assigned to a shader during rendering
class Material final
{
private:
Material(const ShaderRef& shader);
public:
// Copy / Moves not allowed
Material(const Material&) = delete;
Material(Material&&) = delete;
Material& operator=(const Material&) = delete;
Material& operator=(Material&&) = delete;
// Default destructor
~Material() = default;
// Creates a new Material from the given Shader.
// If the Shader is invalid, it will return an invalid MaterialRef.
static MaterialRef create(const ShaderRef& shader);
// Clones the material and returns a new one
MaterialRef clone() const;
// Returns the Shader assigned to the Material.
ShaderRef shader() const;
// Sets the texture
void set_texture(const char* name, const TextureRef& texture, int array_index = 0);
// Sets the texture
void set_texture(int slot, const TextureRef& texture, int array_index = 0);
// Gets the texture, or an empty reference if invalid
TextureRef get_texture(const char* name, int array_index = 0) const;
// Gets the texture, or an empty reference if invalid
TextureRef get_texture(int slot, int array_index = 0) const;
// Sets the sampler
void set_sampler(const char* name, const TextureSampler& sampler, int array_index = 0);
// Sets the sampler
void set_sampler(int slot, const TextureSampler& sampler, int array_index = 0);
// Gets the sampler
TextureSampler get_sampler(const char* name, int array_index = 0) const;
// Gets the sampler
TextureSampler get_sampler(int slot, int array_index = 0) const;
// Sets the value. `length` is the total number of floats to set
// For example if the uniform is a float2[4], a total of 8 float values
// can be set.
void set_value(const char* name, const float* value, i64 length);
// Shorthands to more easily assign uniform values
void set_value(const char* name, float value);
void set_value(const char* name, const Vec2f& value);
void set_value(const char* name, const Vec3f& value);
void set_value(const char* name, const Vec4f& value);
void set_value(const char* name, const Mat3x2f& value);
void set_value(const char* name, const Mat4x4f& value);
void set_value(const char* name, const Vector<float>& value);
void set_value(const char* name, const Vector<Vec2f>& value);
void set_value(const char* name, const Vector<Vec3f>& value);
void set_value(const char* name, const Vector<Vec4f>& value);
void set_value(const char* name, const Vector<Mat3x2f>& value);
void set_value(const char* name, const Vector<Mat4x4f>& value);
// Gets a pointer to the values of the given Uniform, or nullptr if it doesn't exist.
const float* get_value(const char* name, i64* length = nullptr) const;
// Checks if the shader attached to the material has a uniform value with the given name
bool has_value(const char* name) const;
// Returns the internal Texture buffer
const Vector<TextureRef>& textures() const;
// Returns the internal Sampler buffer
const Vector<TextureSampler>& samplers() const;
// Returns the interal float buffer of all the values
const float* data() const;
private:
ShaderRef m_shader;
Vector<TextureRef> m_textures;
Vector<TextureSampler> m_samplers;
Vector<float> m_data;
};
// A single draw call
struct DrawCall
{
// Framebuffer to draw to
TargetRef target;
// Mesh to draw with
MeshRef mesh;
// Material to draw with
MaterialRef material;
// Whether the DrawCall should use a specific viewport
bool has_viewport;
// Whether the DrawCall should use a scissor rectangle
bool has_scissor;
// The viewport (only used if hasViewport is true)
Rectf viewport;
// The scissor rectangle (only used if hasScissor is true)
Rectf scissor;
// First index in the Mesh to draw from
i64 index_start;
// Total amount of indices to draw from the Mesh
i64 index_count;
// Total amount of instances to draw from the Mesh
i64 instance_count;
// Depth Compare Function
Compare depth;
// Cull Mode
Cull cull;
// Blend Mode
BlendMode blend;
// Initializes a default DrawCall
DrawCall();
// Performs the render
void perform();
};
}

View File

@ -1,105 +0,0 @@
#pragma once
#include <blah/common.h>
namespace Blah
{
enum class BlendOp
{
Add,
Subtract,
ReverseSubtract,
Min,
Max
};
enum class BlendFactor
{
Zero,
One,
SrcColor,
OneMinusSrcColor,
DstColor,
OneMinusDstColor,
SrcAlpha,
OneMinusSrcAlpha,
DstAlpha,
OneMinusDstAlpha,
ConstantColor,
OneMinusConstantColor,
ConstantAlpha,
OneMinusConstantAlpha,
SrcAlphaSaturate,
Src1Color,
OneMinusSrc1Color,
Src1Alpha,
OneMinusSrc1Alpha
};
enum class BlendMask
{
None = 0,
Red = 1,
Green = 2,
Blue = 4,
Alpha = 8,
RGB = Red | Green | Blue,
RGBA = Red | Green | Blue | Alpha,
};
// BlendMode using for rendering
struct BlendMode
{
// Normal, pre-multiplied, Blend Mode
static const BlendMode Normal;
// Subtractive Blend Mode
static const BlendMode Subtract;
BlendOp color_op;
BlendFactor color_src;
BlendFactor color_dst;
BlendOp alpha_op;
BlendFactor alpha_src;
BlendFactor alpha_dst;
BlendMask mask;
u32 rgba;
BlendMode() = default;
BlendMode(BlendOp op, BlendFactor src, BlendFactor dst) :
color_op(op),
color_src(src),
color_dst(dst),
alpha_op(op),
alpha_src(src),
alpha_dst(dst),
mask(BlendMask::RGBA),
rgba(0xffffffff) {}
BlendMode(
BlendOp color_op, BlendFactor color_src, BlendFactor color_dst,
BlendOp alpha_op, BlendFactor alpha_src, BlendFactor alpha_dst,
BlendMask blend_mask, u32 blend_rgba) :
color_op(color_op),
color_src(color_src),
color_dst(color_dst),
alpha_op(alpha_op),
alpha_src(alpha_src),
alpha_dst(alpha_dst),
mask(blend_mask),
rgba(blend_rgba) {}
bool operator==(const BlendMode& rhs) const
{
return
color_op == rhs.color_op && color_src == rhs.color_src && color_dst == rhs.color_dst &&
alpha_op == rhs.alpha_op && alpha_src == rhs.alpha_src && alpha_dst == rhs.alpha_dst &&
mask == rhs.mask && rgba == rhs.rgba;
}
bool operator!=(const BlendMode& rhs) const
{
return !(*this == rhs);
}
};
}

View File

@ -1,104 +0,0 @@
#pragma once
#include <blah/common.h>
#include <blah/graphics/texture.h>
#include <blah/graphics/shader.h>
#include <blah/graphics/sampler.h>
#include <blah/containers/vector.h>
#include <blah/math/spatial.h>
namespace Blah
{
class Material;
using MaterialRef = Ref<Material>;
// Materials hold values that can be assigned to a shader during rendering
class Material final
{
private:
Material(const ShaderRef& shader);
public:
// Copy / Moves not allowed
Material(const Material&) = delete;
Material(Material&&) = delete;
Material& operator=(const Material&) = delete;
Material& operator=(Material&&) = delete;
// Default destructor
~Material() = default;
// Creates a new Material from the given Shader.
// If the Shader is invalid, it will return an invalid MaterialRef.
static MaterialRef create(const ShaderRef& shader);
// Clones the material and returns a new one
MaterialRef clone() const;
// Returns the Shader assigned to the Material.
ShaderRef shader() const;
// Sets the texture
void set_texture(const char* name, const TextureRef& texture, int array_index = 0);
// Sets the texture
void set_texture(int slot, const TextureRef& texture, int array_index = 0);
// Gets the texture, or an empty reference if invalid
TextureRef get_texture(const char* name, int array_index = 0) const;
// Gets the texture, or an empty reference if invalid
TextureRef get_texture(int slot, int array_index = 0) const;
// Sets the sampler
void set_sampler(const char* name, const TextureSampler& sampler, int array_index = 0);
// Sets the sampler
void set_sampler(int slot, const TextureSampler& sampler, int array_index = 0);
// Gets the sampler
TextureSampler get_sampler(const char* name, int array_index = 0) const;
// Gets the sampler
TextureSampler get_sampler(int slot, int array_index = 0) const;
// Sets the value. `length` is the total number of floats to set
// For example if the uniform is a float2[4], a total of 8 float values
// can be set.
void set_value(const char* name, const float* value, i64 length);
// Shorthands to more easily assign uniform values
void set_value(const char* name, float value);
void set_value(const char* name, const Vec2f& value);
void set_value(const char* name, const Vec3f& value);
void set_value(const char* name, const Vec4f& value);
void set_value(const char* name, const Mat3x2f& value);
void set_value(const char* name, const Mat4x4f& value);
void set_value(const char* name, const Vector<float>& value);
void set_value(const char* name, const Vector<Vec2f>& value);
void set_value(const char* name, const Vector<Vec3f>& value);
void set_value(const char* name, const Vector<Vec4f>& value);
void set_value(const char* name, const Vector<Mat3x2f>& value);
void set_value(const char* name, const Vector<Mat4x4f>& value);
// Gets a pointer to the values of the given Uniform, or nullptr if it doesn't exist.
const float* get_value(const char* name, i64* length = nullptr) const;
// Checks if the shader attached to the material has a uniform value with the given name
bool has_value(const char* name) const;
// Returns the internal Texture buffer
const Vector<TextureRef>& textures() const;
// Returns the internal Sampler buffer
const Vector<TextureSampler>& samplers() const;
// Returns the interal float buffer of all the values
const float* data() const;
private:
ShaderRef m_shader;
Vector<TextureRef> m_textures;
Vector<TextureSampler> m_samplers;
Vector<float> m_data;
};
}

View File

@ -1,101 +0,0 @@
#pragma once
#include <blah/common.h>
#include <blah/containers/stackvector.h>
namespace Blah
{
// Supported Vertex value types
enum class VertexType
{
None,
Float,
Float2,
Float3,
Float4,
Byte4,
UByte4,
Short2,
UShort2,
Short4,
UShort4
};
// Vertex Attribute information
struct VertexAttribute
{
// Location / Attribute Index
int index = 0;
// Vertex Type
VertexType type = VertexType::None;
// Whether the Vertex should be normalized (doesn't apply to Floats)
bool normalized = false;
};
// Vertex Format information.
// Holds a list of attributes and total stride per-vertex.
struct VertexFormat
{
// List of Attributes
StackVector<VertexAttribute, 16> attributes;
// Total size in bytes of each Vertex element
int stride = 0;
VertexFormat() = default;
VertexFormat(const StackVector<VertexAttribute, 16>& attributes, int stride = 0);
};
// Supported Vertex Index formats
enum class IndexFormat
{
// Indices are 16 bit unsigned integers
UInt16,
// Indices are 32 bit unsigned integers
UInt32
};
class Mesh;
using MeshRef = Ref<Mesh>;
// A Mesh is a set of Indices and Vertices which are used for drawing
class Mesh
{
protected:
Mesh() = default;
public:
// Copy / Moves not allowed
Mesh(const Mesh&) = delete;
Mesh(Mesh&&) = delete;
Mesh& operator=(const Mesh&) = delete;
Mesh& operator=(Mesh&&) = delete;
// Default Destructor
virtual ~Mesh() = default;
// Creates a new Mesh.
// If the Mesh creation fails, it will return an invalid Mesh.
static MeshRef create();
// Uploads the given index buffer to the Mesh
virtual void index_data(IndexFormat format, const void* indices, i64 count) = 0;
// Uploads the given vertex buffer to the Mesh
virtual void vertex_data(const VertexFormat& format, const void* vertices, i64 count) = 0;
// Uploads the given instance buffer to the Mesh
virtual void instance_data(const VertexFormat& format, const void* instances, i64 count) = 0;
// Gets the index count of the Mesh
virtual i64 index_count() const = 0;
// Gets the vertex count of the Mesh
virtual i64 vertex_count() const = 0;
// Gets the instance count of the Mesh
virtual i64 instance_count() const = 0;
};
}

View File

@ -1,89 +0,0 @@
#pragma once
#include <blah/common.h>
#include <blah/math/spatial.h>
#include <blah/containers/str.h>
#include <blah/graphics/texture.h>
#include <blah/graphics/target.h>
#include <blah/graphics/mesh.h>
#include <blah/graphics/shader.h>
#include <blah/graphics/material.h>
#include <blah/graphics/blend.h>
namespace Blah
{
// Depth comparison function to use during a draw call
enum class Compare
{
None,
Always,
Never,
Less,
Equal,
LessOrEqual,
Greater,
NotEqual,
GreatorOrEqual
};
// Cull mode during a draw call
enum class Cull
{
// No Culling enabled
None = 0,
// Cull front faces
Front = 1,
// Cull back faces
Back = 2,
};
// A single draw call
struct RenderPass
{
// Framebuffer to draw to
TargetRef target;
// Mesh to draw with
MeshRef mesh;
// Material to draw with
MaterialRef material;
// Whether the RenderPass should use a specific viewport
bool has_viewport;
// Whether the RenderPass should use a scissor rectangle
bool has_scissor;
// The viewport (only used if hasViewport is true)
Rectf viewport;
// The scissor rectangle (only used if hasScissor is true)
Rectf scissor;
// First index in the Mesh to draw from
i64 index_start;
// Total amount of indices to draw from the Mesh
i64 index_count;
// Total amount of instances to draw from the Mesh
i64 instance_count;
// Depth Compare Function
Compare depth;
// Cull Mode
Cull cull;
// Blend Mode
BlendMode blend;
// Initializes a default RenderPass
RenderPass();
// Performs the render
void perform();
};
}

View File

@ -1,71 +0,0 @@
#pragma once
namespace Blah
{
// Texture filter
enum class TextureFilter
{
// None will fallback to whatever default the driver sets
None,
// Linear interpolation
Linear,
// Nearest Neighbour interpolation
Nearest
};
// Texture Wrap Mode
enum class TextureWrap
{
// None will fallback to whatever default the driver sets
None,
// Clamps the texture to the edges
Clamp,
// Repeats the texture
Repeat
};
// Texture Sampler State, applied during rendering
struct TextureSampler
{
// Filter Mode
TextureFilter filter;
// Wrap X Mode
TextureWrap wrap_x;
// Wrap Y Mode
TextureWrap wrap_y;
TextureSampler() :
filter(TextureFilter::Linear),
wrap_x(TextureWrap::Repeat),
wrap_y(TextureWrap::Repeat) {}
TextureSampler(TextureFilter filter) :
filter(filter),
wrap_x(TextureWrap::Repeat),
wrap_y(TextureWrap::Repeat) {}
TextureSampler(TextureFilter filter, TextureWrap wrap_x, TextureWrap wrap_y) :
filter(filter),
wrap_x(wrap_x),
wrap_y(wrap_y) {}
bool operator==(const TextureSampler& rhs) const
{
return
filter == rhs.filter &&
wrap_x == rhs.wrap_x &&
wrap_y == rhs.wrap_y;
}
bool operator!=(const TextureSampler& rhs) const
{
return !(*this == rhs);
}
};
}

View File

@ -1,102 +0,0 @@
#pragma once
#include <blah/common.h>
#include <blah/containers/stackvector.h>
#include <blah/containers/str.h>
namespace Blah
{
// Supported Uniform Types
enum class UniformType
{
None,
Float,
Float2,
Float3,
Float4,
Mat3x2,
Mat4x4,
Texture2D,
Sampler2D
};
// Supported Shader Types
enum class ShaderType
{
None = 0,
Vertex = 1 << 0,
Fragment = 1 << 1
};
// Uniform Info, provided by the Shader
struct UniformInfo
{
// Name of the Uniform
String name;
// The Value type of the Uniform
UniformType type;
// The Shader type the Uniform is a part of
ShaderType shader;
// Some rendering APIs have uniform buffers. The `buffer_index`
// specifies which buffer the uniform belongs to
int buffer_index;
// Array length of the Uniform (ex. a vec2[4] would be 4)
int array_length;
};
// Data to be passed to the shader to construct it
struct ShaderData
{
struct HLSL_Attribute
{
// Semantic Name
const char* semantic_name = nullptr;
// (optional) Semantic Index
int semantic_index = 0;
};
// Vertex Shader Program data
String vertex;
// Fragment Shader Program data
String fragment;
// HLSL Attributes - required for D3D11
StackVector<HLSL_Attribute, 16> hlsl_attributes;
};
class Shader;
using ShaderRef = Ref<Shader>;
// A shader used during Rendering
class Shader
{
protected:
Shader() = default;
public:
// Copy / Moves not allowed
Shader(const Shader&) = delete;
Shader(Shader&&) = delete;
Shader& operator=(const Shader&) = delete;
Shader& operator=(Shader&&) = delete;
// Default Destructor
virtual ~Shader() = default;
// Creates a Shader with the given Shader Data.
// If the Shader creation fails, it will return an invalid ShaderRef.
static ShaderRef create(const ShaderData& data);
// Gets a list of Shader Uniforms from Shader
virtual Vector<UniformInfo>& uniforms() = 0;
// Gets a list of Shader Uniforms from Shader
virtual const Vector<UniformInfo>& uniforms() const = 0;
};
}

View File

@ -1,72 +0,0 @@
#pragma once
#include <blah/common.h>
#include <blah/graphics/texture.h>
#include <blah/containers/stackvector.h>
#include <blah/math/color.h>
namespace Blah
{
enum class ClearMask
{
None = 0,
Color = 1,
Depth = 2,
Stencil = 4,
All = (int)Color | (int)Depth | (int)Stencil
};
// Up to 4 color textures + 1 depth/stencil
using Attachments = StackVector<TextureRef, 5>;
using AttachmentFormats = StackVector<TextureFormat, 5>;
class Target;
using TargetRef = Ref<Target>;
// Target is a 2D Buffer that can be drawn to.
// It can hold up to 4 color Textures, and 1 Depth/Stencil Texture.
class Target
{
protected:
Target() = default;
public:
// Copy / Moves not allowed
Target(const Target&) = delete;
Target(Target&&) = delete;
Target& operator=(const Target&) = delete;
Target& operator=(Target&&) = delete;
// Default Destructor
virtual ~Target() = default;
// Creates a new Target with a single Color texture
// If the Target creation fails, it will return an invalid TargetRef.
static TargetRef create(int width, int height);
// Creates a new Target with the given Texture Attachments. You must provide at least one Attachment.
// If the Target creation fails, it will return an invalid TargetRef.
static TargetRef create(int width, int height, const AttachmentFormats& textures);
// Gets the list of Attachments from the Target
virtual Attachments& textures() = 0;
// Gets the list of Attachments from the Target
virtual const Attachments& textures() const = 0;
// Gets the Attachment at a given index from the Target
TextureRef& texture(int index);
// Gets the Attachment at a given index from the Target
const TextureRef& texture(int index) const;
// Gets the width of the Target
virtual int width() const;
// Gets the height of the Target
virtual int height() const;
// Clears the Target
virtual void clear(Color color = Color::black, float depth = 1.0f, u8 stencil = 0, ClearMask mask = ClearMask::All) = 0;
};
}

View File

@ -1,96 +0,0 @@
#pragma once
#include <blah/common.h>
#include <blah/filesystem.h>
#include <blah/math/color.h>
namespace Blah
{
enum class TextureFormat
{
// Invalid Format
None,
// Single 8-bit channe;
R,
// 2 8-bit channels
RG,
// 4 8-bit channels
RGBA,
// Depth 24, Stencil 8
DepthStencil,
// Total Formats
Count
};
class Image;
class Stream;
class Texture;
using TextureRef = Ref<Texture>;
// A 2D Texture held by the GPU to be used during rendering
class Texture
{
protected:
Texture() = default;
public:
// Copy / Moves not allowed
Texture(const Texture&) = delete;
Texture(Texture&&) = delete;
Texture& operator=(const Texture&) = delete;
Texture& operator=(Texture&&) = delete;
// Default Destructor
virtual ~Texture() = default;
// Creates a new Texture.
// If the Texture creation fails, it will return an invalid TextureRef.
static TextureRef create(const Image& image);
// Creates a new Texture.
// If image data is provided, it should be the full size of the texture.
// If the Texture creation fails, it will return an invalid TextureRef.
static TextureRef create(int width, int height, TextureFormat format, unsigned char* data = nullptr);
// Creates a new Texture from a Stream.
// If the Texture creation fails, it will return an invalid TextureRef.
static TextureRef create(Stream& stream);
// Creates a new Texture from a File.
// If the Texture creation fails, it will return an invalid TextureRef.
static TextureRef create(const FilePath& file);
// gets the width of the texture
virtual int width() const = 0;
// gets the height of the texture
virtual int height() const = 0;
// Gets the format of the Texture
virtual TextureFormat format() const = 0;
// Sets the data of the Texture.
// Note that the data should be the same format and size as the Texture. There is no row padding.
virtual void set_data(const u8* data) = 0;
// Sets the data of the Texture to the provided Color buffer.
// If the Texture Format is not RGBA, this won't do anything.
void set_data(const Color* data);
// Gets the data of the Texture.
// Note that the data will be written to in the same format as the Texture,
// and you should allocate enough space for the full texture. There is no row padding.
virtual void get_data(u8* data) = 0;
// Gets the data of the Texture.
// If the Texture Format is not RGBA, this won't do anything.
void get_data(Color* data);
// Returns true if the Texture is part of a FrameBuffer
virtual bool is_framebuffer() const = 0;
};
}

View File

@ -1,9 +1,9 @@
#pragma once
#include <blah/common.h>
#include <blah/math/color.h>
#include <blah/images/image.h>
#include <blah/containers/str.h>
#include <blah/streams/stream.h>
#include <blah/filesystem.h>
#include <blah/stream.h>
namespace Blah
{

View File

@ -1,6 +1,6 @@
#pragma once
#include <blah/common.h>
#include <blah/streams/stream.h>
#include <blah/stream.h>
#include <blah/images/image.h>
#include <blah/containers/str.h>
#include <blah/containers/vector.h>

View File

@ -2,6 +2,7 @@
#include <blah/math/color.h>
#include <blah/math/spatial.h>
#include <blah/filesystem.h>
#include <blah/stream.h>
namespace Blah
{

View File

@ -1,10 +1,11 @@
#pragma once
#include <blah/common.h>
#include <blah/images/image.h>
#include <blah/math/color.h>
#include <blah/math/spatial.h>
#include <blah/containers/str.h>
#include <blah/containers/vector.h>
#include <blah/streams/bufferstream.h>
#include <blah/stream.h>
#include <blah/filesystem.h>
namespace Blah

View File

@ -233,15 +233,15 @@ namespace Blah
static const Color teal;
};
inline const Color Color::transparent = Color(0, 0, 0, 0);
inline const Color Color::white = Color(255, 255, 255, 255);
inline const Color Color::black = Color(0, 0, 0, 255);
inline const Color Color::red = Color(255, 0, 0, 255);
inline const Color Color::green = Color(0, 255, 0, 255);
inline const Color Color::blue = Color(0, 0, 255, 255);
inline const Color Color::yellow = Color(255, 255, 0, 255);
inline const Color Color::purple = Color(255, 0, 255, 255);
inline const Color Color::teal = Color(0, 255, 255, 255);
inline const Color Color::transparent = Color( 0, 0, 0, 0);
inline const Color Color::white = Color(255, 255, 255);
inline const Color Color::black = Color( 0, 0, 0);
inline const Color Color::red = Color(255, 0, 0);
inline const Color Color::green = Color( 0, 255, 0);
inline const Color Color::blue = Color( 0, 0, 255);
inline const Color Color::yellow = Color(255, 255, 0);
inline const Color Color::purple = Color(255, 0, 255);
inline const Color Color::teal = Color( 0, 255, 255);
}
#undef BLAH_HEX_VALUE

View File

@ -1,6 +1,6 @@
#pragma once
#include <blah/math/calc.h>
#include <blah/common.h>
#include <blah/math/calc.h>
namespace Blah
{
@ -9,16 +9,16 @@ namespace Blah
enum class Easers
{
Linear,
QuadIn, QuadOut, QuadInOut,
CubeIn, CubeOut, CubeInOut,
QuartIn, QuartOut, QuartInOut,
QuintIn, QuintOut, QuintInOut,
SineIn, SineOut, SineInOut,
CircIn, CircOut, CircInOut,
ExpIn, ExpOut, ExpInOut,
QuadIn, QuadOut, QuadInOut,
CubeIn, CubeOut, CubeInOut,
QuartIn, QuartOut, QuartInOut,
QuintIn, QuintOut, QuintInOut,
SineIn, SineOut, SineInOut,
CircIn, CircOut, CircInOut,
ExpIn, ExpOut, ExpInOut,
ElasticIn, ElasticOut, ElasticInOut,
BackIn, BackOut, BackInOut,
BounceIn, BounceOut, BounceInOut,
BackIn, BackOut, BackInOut,
BounceIn, BounceOut, BounceInOut,
Count
};

View File

@ -1,10 +1,15 @@
#pragma once
#include <blah/common.h>
#include <blah/containers/str.h>
#include <blah/containers/vector.h>
#include <blah/math/calc.h>
#include <blah/filesystem.h>
namespace Blah
{
// Stream is a generic handler to read and write data to various buffers.
// The purpose is to allow serializers to not care what they're writing data to,
// be it a file or memory.
class Stream
{
public:
@ -44,7 +49,7 @@ namespace Blah
// reads a string until a newline '\n' or null-terminator '\0' is found
String read_line();
// reads a number
// reads a number, returns the value read.
u8 read_u8 (Endian endian = Endian::Little);
u16 read_u16(Endian endian = Endian::Little);
u32 read_u32(Endian endian = Endian::Little);
@ -81,4 +86,86 @@ namespace Blah
// writes the amount of bytes to the stream from the given buffer, and returns the amount written
virtual size_t write_data(const void* buffer, size_t length) = 0;
};
// File Stream reads & writes over a File handle
class FileStream : public Stream
{
public:
FileStream() = default;
FileStream(const FilePath& path, FileMode mode);
FileStream(const FileRef& file);
size_t length() const override;
size_t position() const override;
size_t seek(size_t position) override;
bool is_open() const override;
bool is_readable() const override;
bool is_writable() const override;
protected:
size_t read_data(void* ptr, size_t length) override;
size_t write_data(const void* ptr, size_t length) override;
private:
FileRef m_file;
};
// Memory Stream moves over an existing buffer.
// The Buffer must exist for the duration of the Memory Stream.
class MemoryStream : public Stream
{
public:
MemoryStream() = default;
MemoryStream(u8* data, size_t length);
MemoryStream(const u8* data, size_t length);
size_t length() const override;
size_t position() const override;
size_t seek(size_t seek_to) override;
bool is_open() const override;
bool is_readable() const override;
bool is_writable() const override;
u8* data();
const u8* data() const;
protected:
size_t read_data(void* ptr, size_t length) override;
size_t write_data(const void* ptr, size_t length) override;
private:
u8* m_data = nullptr;
const u8* m_const_data = nullptr;
size_t m_length = 0;
size_t m_position = 0;
};
// Buffer Stream reads and writes to an internal buffer.
// It will grow the capacity if it needs to while writing.
class BufferStream : public Stream
{
public:
BufferStream() = default;
BufferStream(int capacity);
size_t length() const override;
size_t position() const override;
size_t seek(size_t seek_to) override;
bool is_open() const override;
bool is_readable() const override;
bool is_writable() const override;
void resize(size_t length);
void clear();
u8* data();
const u8* data() const;
protected:
size_t read_data(void* ptr, size_t length) override;
size_t write_data(const void* ptr, size_t length) override;
private:
Vector<u8> m_buffer;
size_t m_position = 0;
};
}

View File

@ -1,35 +0,0 @@
#pragma once
#include <blah/streams/stream.h>
namespace Blah
{
// Buffer Stream reads and writes to an internal buffer.
// It will grow the capacity if it needs to while writing.
class BufferStream : public Stream
{
public:
BufferStream() = default;
BufferStream(int capacity);
size_t length() const override;
size_t position() const override;
size_t seek(size_t seek_to) override;
bool is_open() const override;
bool is_readable() const override;
bool is_writable() const override;
void resize(size_t length);
void clear();
u8* data();
const u8* data() const;
protected:
size_t read_data(void* ptr, size_t length) override;
size_t write_data(const void* ptr, size_t length) override;
private:
Vector<u8> m_buffer;
size_t m_position = 0;
};
}

View File

@ -1,30 +0,0 @@
#pragma once
#include <blah/streams/stream.h>
#include <blah/filesystem.h>
namespace Blah
{
class FileStream : public Stream
{
public:
FileStream() = default;
FileStream(const FilePath& path, FileMode mode);
FileStream(FileStream&& fs) noexcept;
FileStream& operator=(FileStream&& fs) noexcept;
size_t length() const override;
size_t position() const override;
size_t seek(size_t position) override;
bool is_open() const override;
bool is_readable() const override;
bool is_writable() const override;
protected:
size_t read_data(void* ptr, size_t length) override;
size_t write_data(const void* ptr, size_t length) override;
private:
FileMode m_mode = FileMode::OpenRead;
FileRef m_file;
};
}

View File

@ -1,35 +0,0 @@
#pragma once
#include <blah/streams/stream.h>
namespace Blah
{
// Memory Stream moves over an existing buffer.
// The Buffer must exist for the duration of the Memory Stream.
class MemoryStream : public Stream
{
public:
MemoryStream() = default;
MemoryStream(u8* data, size_t length);
MemoryStream(const u8* data, size_t length);
size_t length() const override;
size_t position() const override;
size_t seek(size_t seek_to) override;
bool is_open() const override;
bool is_readable() const override;
bool is_writable() const override;
u8* data();
const u8* data() const;
protected:
size_t read_data(void* ptr, size_t length) override;
size_t write_data(const void* ptr, size_t length) override;
private:
u8* m_data = nullptr;
const u8* m_const_data = nullptr;
size_t m_length = 0;
size_t m_position = 0;
};
}