#pragma once #include "classes.h" #include #include #include #include #include #include #include #include namespace KAGE::Graphics { class ImageBase; class RenderBatch; using ImagePtr = std::shared_ptr; constexpr int QUAD_CORNERS = 4; constexpr int TEXTURE_SLOTS = 8; // The depth the first quad of a frame is drawn at, and how much nearer each next one is. constexpr float DEPTH_START = -1; constexpr float DEPTH_STEP = 0.01f; constexpr const char* COLOR_SHADER = "KAGE_ColorShader"; constexpr const char* COLOR_TEXTURE_SHADER = "KAGE_ColorTextureShader"; constexpr const char* INDEXED_TEXTURE_SHADER = "KAGE_IndexedTextureShader"; // REPENTOGON's eBlendEquation. enum BlendEquation { EQUATION_ADD, EQUATION_SUBTRACT, EQUATION_REVERSE_SUBTRACT, EQUATION_MIN, EQUATION_MAX }; // REPENTOGON's eBlendFactor. enum BlendFactor { FACTOR_ZERO, FACTOR_ONE, FACTOR_SRC_COLOR, FACTOR_ONE_MINUS_SRC_COLOR, FACTOR_DST_COLOR, FACTOR_ONE_MINUS_DST_COLOR, FACTOR_SRC_ALPHA, FACTOR_ONE_MINUS_SRC_ALPHA, FACTOR_DST_ALPHA, FACTOR_ONE_MINUS_DST_ALPHA }; // The blend modes BlendMode::SetMode knows. enum BlendType { BLEND_OPAQUE, BLEND_NORMAL, BLEND_ADDITIVE }; // REPENTOGON's eVertexAttributeFormat: float vectors of 1 to 4 components, and the three slots images fill in. // Format 8 is one more single float. enum VertexFormat { VERTEX_END, VERTEX_FLOAT, VERTEX_VEC2, VERTEX_VEC3, VERTEX_VEC4, VERTEX_POSITION, VERTEX_COLOR, VERTEX_TEXCOORD, VERTEX_FLOAT_OTHER, VERTEX_FORMAT_COUNT }; // A shader uniform's type. Types below UNIFORM_FLOAT are texture slots. enum UniformType { UNIFORM_FLOAT = TEXTURE_SLOTS, UNIFORM_VEC2, UNIFORM_VEC3, UNIFORM_VEC4, UNIFORM_INT, UNIFORM_IVEC2, UNIFORM_IVEC3, UNIFORM_IVEC4, UNIFORM_UINT, UNIFORM_UVEC2, UNIFORM_UVEC3, UNIFORM_UVEC4, UNIFORM_MAT2, UNIFORM_MAT2X3, UNIFORM_MAT2X4, UNIFORM_MAT3X2, UNIFORM_MAT3, UNIFORM_MAT3X4, UNIFORM_MAT4X2, UNIFORM_MAT4X3, UNIFORM_MAT4, UNIFORM_TYPE_END }; // How an image's texels are stored: palette indices, RGB or RGBA, one byte a channel. enum ImageFormat { IMAGE_INDEXED = 0, IMAGE_RGB = 6, IMAGE_RGBA = 8, IMAGE_NONE = 11 }; // The file types image_create decodes: the image files, and raw RGBA (16-bit width and height, then the rows). enum ImageFileType { FILE_PNG, FILE_JPG, FILE_BMP, FILE_PCX, FILE_RGBA, FILE_PLATFORM }; // Whether a source quad's corners are texel positions or already fractions of the image. enum CoordinateSpace { COORDINATES_NORMALIZED, COORDINATES_PIXEL }; // Whether a render target is drawn at its own size or with the screen's projection. enum TargetSize { TARGET_OWN_SIZE, TARGET_SCREEN_SIZE }; // Whether Present ends the frame on the screen (swapping) or only clears the depth of the bound render target. enum PresentMode { PRESENT_FLUSH, PRESENT_FINISH }; // How a draw blends into its target: the equation, then the color and alpha factors. struct BlendMode { BlendEquation equation = EQUATION_ADD; BlendFactor rgbSource = FACTOR_SRC_ALPHA; BlendFactor rgbDest = FACTOR_ONE_MINUS_SRC_ALPHA; BlendFactor alphaSource = FACTOR_ONE; BlendFactor alphaDest = FACTOR_ONE_MINUS_SRC_ALPHA; bool operator==(const BlendMode&) const = default; void SetMode(BlendType type); }; constexpr BlendMode BLEND_MODE_OPAQUE = {EQUATION_ADD, FACTOR_ONE, FACTOR_ZERO, FACTOR_ONE, FACTOR_ZERO}; // A destination quad with a color at each corner, in the order of its corners. struct ColorQuad : DestinationQuad { std::array colors = {}; ColorQuad() = default; ColorQuad(const DestinationQuad& quad, KColor color); }; // A vertex attribute a shader reads, by name. struct VertexAttribute { std::string name; VertexFormat format = VERTEX_END; }; // The GLSL of a shader's two stages. struct ShaderSource { std::string_view vertex; std::string_view fragment; }; // A uniform the game set by name, with every value it took since the frame began: a batch replays the values that // were current when it was recorded. struct ShaderUniform { std::string name; std::vector> values; UniformType type = UNIFORM_FLOAT; }; // KAGE's GLSL program (ShaderBase and Shader): its vertex attributes, the textures it samples, and its uniforms with // their history. uniformState holds the index of each uniform's current value. class Shader { public: std::array textureNames; std::vector attributes; std::vector uniforms; std::vector uniformState; std::string name; unsigned int program = 0; bool isLoaded = false; bool isUsed = false; ~Shader(); bool load(std::string_view shaderName, std::span vertexAttributes, ShaderSource source); void Shutdown(); void apply(const std::vector& state); void frame_end(); void set_uniform(std::string_view uniformName, UniformType type, const void* data); ShaderUniform* find_uniform(std::string_view uniformName); int vertex_size_get() const; void attributes_enable(const float* vertices, int stride); void attributes_disable(); private: bool compile(ShaderSource source); void texture_bind(int slot, const ImageBase& image); void uniform_upload(const ShaderUniform& uniform, const void* data); }; // A texture the renderer draws from: a decoded image file, raw pixels, or a render target the game draws into. // The texture is padded to powers of two, and uvScale takes the image's far corner to the padded texture's. // batches lists the opaque batches it drew into this frame, which later opaque draws of it may join. class ImageBase : public std::enable_shared_from_this { public: std::vector batches; std::string name; std::string path; ImageFormat format = IMAGE_NONE; float uvScaleX = 0; float uvScaleY = 0; int height = 0; int textureHeight = 0; int textureWidth = 0; int width = 0; int magFilter = -1; int minFilter = -1; int wrapS = -1; int wrapT = -1; unsigned int texture = 0; bool isLoaded = false; bool isRenderTarget = false; ImageBase() = default; ImageBase(const ImageBase&) = delete; ~ImageBase(); float* Render(std::span sources, std::span quads); float* Render(const SourceQuad& source, const ColorQuad& quad, const KColor& color); float* Render(const SourceQuad& source, const Vector& position, const KColor& color); float* Render(const ColorQuad& quad, const KColor& color); float* Render(const Vector& position, const KColor& color); void bind(int slot); bool set_size(int imageWidth, int imageHeight); bool texels_load(std::vector& texels); void filter_set(int min, int mag); void wrap_set(int s, int t); void loaded_set(); }; // One draw call's worth of quads: the image, blend mode and shader they share, with the shader's uniform values when // the batch began. An opaque batch (BLEND_MODE_OPAQUE) can take quads drawn later with the same state. // Destruction (00a66d90) is the default. The vertex and index buffers (GraphicsBufferObject: 00a67fe0, 00a680a0, // 00a680d0, 00a68160, 00a68210, 00a68240, 00a68280, 00a68320, 00a68350, 00a68360) are std::vector. class RenderBatch { public: BlendMode blendMode; ImagePtr image; std::vector uniformState; std::vector vertices; ImageBase* imageKey = nullptr; Shader* shader = nullptr; bool isListed = false; bool isOpaque = false; void set_state(ImageBase* target); bool is_state_equal(const ImageBase* target) const; float* quads_add(std::span sources, std::span quads); void submit(); }; // What a render target is made with: its size, its name in the image cache and the color its texels start as. struct RenderTargetDesc { KColor clearColor = {}; std::string name; int height = 0; int width = 0; }; // A render target pushed with Manager::render_target_push, to go back to. struct RenderTargetEntry { ImageBase* target = nullptr; TargetSize size = TARGET_OWN_SIZE; }; // KAGE's Graphics::Manager, with its image and shader managers: the GL state, the batches of the frame, the render // target stack and the image and shader caches. The window and its framebuffer are Platform's. // Images and render targets are cached by their normalized path or name, holding them only while used elsewhere. class Manager { public: std::array textures = {}; std::unordered_multimap> images; std::unordered_map> shaders; std::vector> batchPool; std::vector freeBatches; std::vector pendingBatches; std::vector renderTargets; std::vector shaderStack; std::vector usedShaders; std::vector quadIndices; BlendMode blendMode; KColor clearColor = {}; ImageBase* renderTarget = nullptr; RenderBatch* currentBatch = nullptr; Shader* currentShader = nullptr; float depth = DEPTH_START; float orthographicHeight = 1080; float orthographicWidth = 1920; int defaultMagFilter = 0; int defaultMinFilter = 0; int defaultWrapS = 1; int defaultWrapT = 1; bool isCullOffscreen = true; bool isInitialized = false; bool isPixelSnap = true; bool isPremultipliedAlpha = false; bool isScreenTarget = true; bool isSrgb = false; bool isTargetOffset = false; bool Init(); void Shutdown(); void Clear(); void Present(PresentMode mode); void SetRenderTarget(ImageBase* target, TargetSize size); void render_target_push(ImageBase* target, TargetSize size, PresentMode mode); void render_target_pop(PresentMode mode); void update_projection(); void clear_color_set(float red, float green, float blue); void blend_set(BlendType type); void batches_flush(); RenderBatch* batch_get(ImageBase* image); void quads_draw(int vertexCount); float view_width_get(); float view_height_get(); int target_width_get(); int target_height_get(); bool shader_create(std::string_view shaderName, std::span attributes, ShaderSource source); bool shader_load(std::string_view shaderName, std::span attributes, ShaderSource files); Shader* shader_find(std::string_view shaderName); bool shader_default_set(); bool shader_push(std::string_view shaderName); void shader_pop(); void shader_use(Shader& shader, const std::vector& state); ImagePtr image_load(std::string_view imagePath); ImagePtr image_create(std::span file, ImageFileType type, std::string_view imageName); ImagePtr render_target_create(const RenderTargetDesc& desc); ImagePtr image_find(std::string_view imagePath); void image_cache(ImageBase& image); }; extern Manager manager; std::string path_key(std::string_view path); ColorQuad color_quad_rect(const Vector& position, const Vector& size, const KColor& color); SourceQuad source_quad_rect(const Vector& position, const Vector& size); bool is_quad_on_screen(const DestinationQuad& quad); }