#include <vector>
#include <msp/core/noncopyable.h>
+#include "blend.h"
#include "cullface.h"
+#include "depthtest.h"
+#include "pipelinestate_backend.h"
+#include "primitivetype.h"
+#include "rect.h"
+#include "stenciltest.h"
namespace Msp {
namespace GL {
-class Blend;
-class DepthTest;
+class Buffer;
class Framebuffer;
class Program;
-class Rect;
class Sampler;
-class StencilTest;
class Texture;
class UniformBlock;
class VertexSetup;
-class PipelineState: public NonCopyable
+/**
+Stores state for the entire GPU pipeline.
+
+Applications normally use the higher-level Renderer class rather than this.
+*/
+class PipelineState: public PipelineStateBackend
{
+ friend PipelineStateBackend;
+
private:
- struct BoundTexture
+ enum ResourceType: std::uint8_t
{
- unsigned binding;
- mutable bool changed;
- const Texture *texture;
- const Sampler *sampler;
-
- BoundTexture(unsigned);
+ NO_RESOURCE,
+ UNIFORM_BLOCK,
+ SAMPLED_TEXTURE,
+ STORAGE_TEXTURE
};
- struct BoundUniformBlock
+ struct BoundResource
{
- int binding;
- mutable bool changed;
- const UniformBlock *block;
-
- BoundUniformBlock(int);
+ int binding = 0;
+ ResourceType type = NO_RESOURCE;
+ mutable bool changed = false;
+ mutable bool used = false;
+ union
+ {
+ const UniformBlock *block;
+ const Texture *texture;
+ };
+ union
+ {
+ const Buffer *buffer;
+ const Sampler *sampler;
+ };
+ int mip_level = -1;
+
+ BoundResource(int b): binding(b), texture(0), sampler(0) { }
};
enum ChangeMask
{
- SHPROG = 1,
- VERTEX_SETUP = 2,
- FACE_CULL = 4,
- CLIP_PLANES = 8,
- TEXTURES = 16,
- UNIFORMS = 32,
- DEPTH_TEST = 64,
- STENCIL_TEST = 128,
- BLEND = 256,
- FRAMEBUFFER = 512,
- VIEWPORT = 1024,
- SCISSOR = 2048
+ FRAMEBUFFER = 1,
+ VIEWPORT = 2,
+ SCISSOR = 4,
+ SHPROG = 8,
+ RESOURCES = 16,
+ VERTEX_SETUP = 64,
+ FACE_CULL = 128,
+ DEPTH_TEST = 256,
+ STENCIL_TEST = 512,
+ BLEND = 1024,
+ PRIMITIVE_TYPE = 2048
};
- const Framebuffer *framebuffer;
- const Rect *viewport;
- const Rect *scissor;
- const Program *shprog;
- const VertexSetup *vertex_setup;
- FaceWinding front_face;
- CullMode face_cull;
- unsigned enabled_clip_planes;
- std::vector<BoundTexture> textures;
- std::vector<BoundUniformBlock> uniform_blocks;
- const DepthTest *depth_test;
- const StencilTest *stencil_test;
- const Blend *blend;
- mutable unsigned changes;
-
- static const PipelineState *last_applied;
- static std::vector<int> bound_tex_targets;
- static std::vector<char> bound_uniform_blocks;
- static unsigned restart_index;
+ const Framebuffer *framebuffer = 0;
+ Rect viewport = Rect::max();
+ Rect scissor = Rect::max();
+ const Program *shprog = 0;
+ std::vector<BoundResource> resources;
+ const VertexSetup *vertex_setup = 0;
+ PrimitiveType primitive_type = TRIANGLES;
+ FaceWinding front_face = COUNTERCLOCKWISE;
+ CullMode face_cull = NO_CULL;
+ DepthTest depth_test;
+ StencilTest stencil_test;
+ Blend blend;
-public:
- PipelineState();
- ~PipelineState();
-
-private:
template<typename T>
- void set(T &, T, unsigned);
+ void set(T &, const T &, unsigned);
public:
void set_framebuffer(const Framebuffer *);
- void set_viewport(const Rect *);
- void set_scissor(const Rect *);
+ void set_viewport(const Rect &);
+ void set_scissor(const Rect &);
void set_shader_program(const Program *);
+ void set_uniform_block(int, const UniformBlock *);
+ void set_texture(unsigned, const Texture *, const Sampler *);
+ void set_texture(unsigned, const Texture *, int, const Sampler *);
+ void set_storage_texture(unsigned, const Texture *);
+private:
+ void set_texture_resource(unsigned, const Texture *, int, const Sampler *);
+public:
void set_vertex_setup(const VertexSetup *);
+ void set_primitive_type(PrimitiveType);
void set_front_face(FaceWinding);
void set_face_cull(CullMode);
- void set_enabled_clip_planes(unsigned);
- void set_texture(unsigned, const Texture *, const Sampler *);
- void set_uniform_block(int, const UniformBlock *);
- void set_depth_test(const DepthTest *);
- void set_stencil_test(const StencilTest *);
- void set_blend(const Blend *);
+ void set_depth_test(const DepthTest &);
+ void set_stencil_test(const StencilTest &);
+ void set_blend(const Blend &);
const Framebuffer *get_framebuffer() const { return framebuffer; }
+ const Rect &get_viewport() const { return viewport; }
const Program *get_shader_program() const { return shprog; }
const VertexSetup *get_vertex_setup() const { return vertex_setup; }
FaceWinding get_front_face() const { return front_face; }
CullMode get_face_cull() const { return face_cull; }
-
- void apply() const;
-private:
- void apply(unsigned) const;
-public:
- static void clear();
};
} // namespace GL