]> git.tdb.fi Git - libs/gl.git/blobdiff - source/render/renderer.h
Support compute shaders and compute operations
[libs/gl.git] / source / render / renderer.h
index 36fe6c919a5e063de7b5fa0e74353fe0643b700d..3341d9aa061e038f3acb59c5515cc6f776c9d5d7 100644 (file)
@@ -4,25 +4,30 @@
 #include <set>
 #include <vector>
 #include "commands.h"
+#include "cullface.h"
 #include "matrix.h"
-#include "pipelinestate.h"
 #include "programdata.h"
+#include "renderer_backend.h"
 #include "tag.h"
 
 namespace Msp {
 namespace GL {
 
 class Batch;
+class Blend;
 class Buffer;
 class Camera;
 union ClearValue;
+class DepthTest;
 class Material;
 class Mesh;
 class Lighting;
 class Program;
 class QueryPool;
+struct Rect;
 class Renderable;
 class Sampler;
+class StencilTest;
 class Texture;
 class VertexSetup;
 
@@ -39,8 +44,10 @@ Renderables can save the state by pushing it on the stack before beginning
 their work, and pop it afterwards to restore it without disturbing state set
 by outer scopes.
 */
-class Renderer
+class Renderer: public RendererBackend
 {
+       friend RendererBackend;
+
 public:
        /**
        RAII helper class for pushing state on the stack.
@@ -56,13 +63,25 @@ public:
        };
 
 private:
-       struct BoundTexture
+       struct SampledTexture
        {
-               Tag tag;
-               mutable int binding = -1;
                const Texture *texture = 0;
                const Sampler *sampler = 0;
+               int level = -1;
+
+               SampledTexture() = default;
+               SampledTexture(const Texture *t, const Sampler *s, int l): texture(t), sampler(s), level(l) { }
+
+               bool operator==(const SampledTexture &o) const { return texture==o.texture && sampler==o.sampler && level==o.level; }
+       };
+
+       template<typename T>
+       struct BoundResource
+       {
+               Tag tag;
+               mutable int binding = -1;
                int replaced = -1;
+               T resource;
        };
 
        struct BoundProgramData
@@ -75,6 +94,7 @@ private:
 
        struct State
        {
+               std::uintptr_t pipeline_key = 0;
                const Camera *camera = 0;
                Matrix model_matrix;
                const Framebuffer *framebuffer = 0;
@@ -94,48 +114,78 @@ private:
 
        enum ChangeMask
        {
+               PIPELINE_KEY = 1,
                MATRIX = 2,
+               CAMERA = 4,
                SHADER_DATA = 16
        };
 
+       unsigned frame_index = 0;
        unsigned char changed = 0;
        std::vector<State> state_stack;
-       State *state;
-       std::vector<BoundTexture> texture_stack;
+       State *current_state = 0;
        ProgramData standard_shdata;
        std::vector<BoundProgramData> shdata_stack;
-       PipelineState pipeline_state;
+       std::vector<BoundResource<SampledTexture>> texture_stack;
+       const Texture &placeholder_texture;
+       const Sampler &default_sampler;
+       PipelineState *last_pipeline = 0;
        Commands commands;
 
+       static const Tag world_obj_matrix_tag;
+       static const Tag world_obj_normal_matrix_tag;
+
 public:
        Renderer();
-       ~Renderer();
+
+       /** Begins rendering, allowing commands to be issued. */
+       void begin();
+
+       /** Ends rendering.  Any global state is reset to defaults.  No further
+       commands are allowed before the next call to begin(). */
+       void end();
+
+       using RendererBackend::begin;
+       using RendererBackend::end;
+
+       /** Saves the current state so it can be restored later. */
+       void push_state();
+
+       /** Restores a previously saved state.  Must be matched with an earlier
+       push_state call. */
+       void pop_state();
+
+private:
+       State &get_state() const;
+
+public:
+       void set_pipeline_key(std::uintptr_t);
+       void set_pipeline_key(const void *p) { set_pipeline_key(reinterpret_cast<uintptr_t>(p)); }
+
+       template<typename T>
+       void set_pipeline_key(std::uintptr_t k, T d)
+       { set_pipeline_key(k^(static_cast<uintptr_t>(d)<<((sizeof(std::uintptr_t)-sizeof(T))*std::numeric_limits<char>::digits))); }
+
+       template<typename T>
+       void set_pipeline_key(const void *p, T d) { set_pipeline_key(reinterpret_cast<uintptr_t>(p), d); }
 
        /** Sets the camera to render from.  The model matrix is reset to identity. */
        void set_camera(const Camera &);
 
-       const Camera *get_camera() const { return state->camera; }
+       const Camera *get_camera() const { return get_state().camera; }
 
        /** Replaces the Renderer's model matrix. */
        void set_matrix(const Matrix &);
 
-       /** Applies a transform to the Renderer's model matrix. */
-       void transform(const Matrix &);
-
        /** Returns the current model matrix. */
-       const Matrix &get_matrix() const { return state->model_matrix; }
+       const Matrix &get_matrix() const { return get_state().model_matrix; }
 
        void set_framebuffer(const Framebuffer *);
        void set_viewport(const Rect *);
        void set_scissor(const Rect *);
 
-       const Framebuffer *get_framebuffer() const { return state->framebuffer; }
-
-       void set_texture(Tag, const Texture *, const Sampler * = 0);
-private:
-       void flush_textures();
+       const Framebuffer *get_framebuffer() const { return get_state().framebuffer; }
 
-public:
        /** Sets the shader program to use.  As a convenience, uniform values may be
        specified at the same time. */
        void set_shader_program(const Program *prog, const ProgramData *data = 0);
@@ -145,9 +195,19 @@ public:
        last will be used. */
        void add_shader_data(const ProgramData &data);
 
+       void set_texture(Tag, const Texture *, const Sampler * = 0);
+       void set_texture(Tag, const Texture *, int, const Sampler * = 0);
+       void set_storage_texture(Tag, const Texture *);
+
 private:
+       template<typename T>
+       static void set_resource(std::vector<BoundResource<T>> &, unsigned &, Tag, const T &);
+
        void flush_shader_data();
 
+       template<typename T>
+       static void flush_resources(std::vector<BoundResource<T>> &, unsigned &);
+
 public:
        void set_vertex_setup(const VertexSetup *);
        void set_front_face(FaceWinding);
@@ -158,21 +218,12 @@ public:
        void set_blend(const Blend *);
 
        void set_object_lod_bias(unsigned);
-       unsigned get_object_lod_bias() const { return state->object_lod_bias; }
+       unsigned get_object_lod_bias() const { return get_state().object_lod_bias; }
 
-       /** Saves the current state so it can be restored later. */
-       void push_state();
-
-       /** Restores a previously saved state.  Must be matched with an earlier
-       push_state call. */
-       void pop_state();
-
-       /** Unbinds all objects and resets related state.  There must be no unpopped
-       state in the stack.  The Renderer remains valid and may be reused for
-       further rendering. */
-       void end();
-
-       void clear(const ClearValue *);
+       /** Clears framebuffer contents.  If values is not null, it must contain one
+       element for each attachment.  Otherwise the framebuffer contents are
+       discarded and become undefined. */
+       void clear(const ClearValue *values);
 
        /** Draws a batch of primitives.  A shader must be active. */
        void draw(const Batch &);
@@ -180,6 +231,9 @@ public:
        /** Draws multiple instances of a batch of primitives.  A shader must be active. */
        void draw_instanced(const Batch &, unsigned);
 
+       /** Dispatches a compute operation. */
+       void dispatch(unsigned, unsigned = 1, unsigned = 1);
+
        /** Resolves multisample attachments from the active framebuffer into
        target. */
        void resolve_multisample(Framebuffer &target);
@@ -188,6 +242,8 @@ public:
        void end_query(const QueryPool &, unsigned);
 
 private:
+       PipelineState &get_pipeline_state();
+       void apply_framebuffer();
        void apply_state();
 };