Cogs.Core
ContextGLES30.h
1#pragma once
2
3#include "../IContext.h"
4
5#include "CommonGLES30.h"
6#include "../Base/ContextCommon.h"
7#include "../RasterizerState.h"
8#include "../BlendState.h"
9#include "../DepthStencilState.h"
10#include "../BaseGL/SyncObjects.h"
11
12#include <map>
13
14namespace Cogs
15{
16 struct TextureGLES30;
17 struct BuffersGLES30;
18 struct TexturesGLES30;
19 struct EffectsGLES30;
20 struct RenderTargetsGLES30;
21 struct CapabilitiesGLES30;
22
24 {
25 BufferGLES30* currentVertexBuffers[kMaxVertexInputSlots];
26 BufferGLES30* currentIndexBuffer = nullptr;
27 size_t requiredVertexBuffers = 0;
28 size_t indexBufferStride;
29 };
30
32 {
33 friend struct TexturesGLES30;
34 friend struct BuffersGLES30;
35
36 void initialize(BuffersGLES30 * buffers, TexturesGLES30 * textures, EffectsGLES30 * effects, RenderTargetsGLES30 * renderTargets, CapabilitiesGLES30* caps, GLSyncObjects* sync) {
37 this->buffers = buffers;
38 this->textures = textures;
39 this->effects = effects;
40 this->renderTargets = renderTargets;
41 this->capabilities = caps;
42 this->syncObjects = sync;
43 }
44
45 InputAssemblerStateCommon* getIAState() override { return nullptr; }
46
47 EffectHandle getCurrentEffect() override { return currentEffect.handle; }
48 IEffects* getEffects() override;
49
50 void setDefaults();
51 void clearCachedState() final;
52
53 void signal(FenceHandle fenceHandle) override;
54
55 void beginRenderPass(const RenderPassInfo &info) override;
56 void endRenderPass() override;
57
58 void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override;
59 void setViewport(const float x, const float y, const float width, const float height) override;
60 void setScissor(const int x, const int y, const int width, const int height) override;
61
62 void clearRenderTarget(const float * color) override;
63 void clearRenderTarget(const float ** colors, const int count) override;
64 void clearDepth(const float depth = 1.0f) override;
65
66 void setDepthStencilState(const DepthStencilStateHandle handle) override;
67 void setBlendState(const BlendStateHandle handle, const float* constant) override;
68 void setRasterizerState(const RasterizerStateHandle handle) override;
69
70 void setEffect(EffectHandle handle) override;
71 EffectHandle getEffect() { return currentEffect.handle; }
72
73 void setMatrixVariable(const StringView& name, const float * value) override;
74 void setScalarVariable(const StringView& name, const float value) override;
75 void setScalarVariable(const StringView& name, int value) override;
76 void setVector2Variable(const StringView& name, const float * value) override;
77 void setVector3Variable(const StringView& name, const float * value) override;
78 void setVector4Variable(const StringView& name, const float * value) override;
79
80 void setMatrixVariable(const EffectVariableHandle variableHandle, const float* value) override { setMatrixVariable(variableHandle, value, 1); }
81 void setMatrixVariable(const EffectVariableHandle variableHandle, const float* value, size_t count) override;
82
83 void setScalarVariable(const EffectVariableHandle variableHandle, const float value) override { setScalarVariable(variableHandle, &value, 1); }
84 void setScalarVariable(const EffectVariableHandle variableHandle, const float* value, size_t count) override;
85
86 void setScalarVariable(const EffectVariableHandle variableHandle, int value) override { setScalarVariable(variableHandle, &value, 1); }
87 void setScalarVariable(const EffectVariableHandle variableHandle, const int* value, size_t count) override;
88
89 void setVector2Variable(const EffectVariableHandle variableHandle, const float* value, size_t count) override;
90 void setVector3Variable(const EffectVariableHandle variableHandle, const float* value, size_t count) override;
91 void setVector4Variable(const EffectVariableHandle variableHandle, const float* value, size_t count) override;
92 void setVector4Variable(const EffectVariableHandle variableHandle, const int* value, size_t count) override;
93
94 void setVariable(const EffectVariableHandle index, const uint8_t* data, size_t size) override;
95
96 void setTexture(const TextureBindingHandle textureBindingHandle, TextureViewHandle textureViewHandle) final;
97 void setTexture(const TextureBindingHandle textureBindingHandle, const TextureHandle textureHandle) final;
98 void setSamplerState(const SamplerStateBindingHandle samplerStateBindingHandle, const SamplerStateHandle samplerStateHandle) override;
99 void setConstantBuffer(const ConstantBufferBindingHandle bufferBindingHandle, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size) override;
100
101 void setInputLayout(const InputLayoutHandle inputLayoutHandle) override;
102 void setVertexBuffers(const VertexBufferHandle* vertexBufferHandles, const size_t count, const uint32_t* strides, const uint32_t* offsets) override;
103 void setVertexBuffers(const VertexBufferHandle * vertexBufferHandles, const size_t numVertexBuffers) override;
104
105 void setIndexBuffer(IndexBufferHandle indexBufferHandle, uint32_t stride, uint32_t offset) override;
106
107 void setVertexArrayObject(VertexArrayObjectHandle vertexArrayObject) override;
108
109 void draw(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes) override;
110 void drawIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex = 0) override;
111
112 void drawInstanced(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes, const size_t startInstance, const size_t numInstances) override;
113 void drawInstancedIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startInstance, const size_t numInstances, const size_t startIndex, const size_t numIndexes) override;
114
115 void dispatchCompute(const unsigned int /*threadGroupsX*/, const unsigned int /*threadGroupsY*/, const unsigned int /*threadGroupsZ*/) {}
116
117 //
118 // Render target, texture and buffer manipulation.
119 //
120
121 void resolveResource(TextureHandle source, TextureHandle destination) override;
122
123 void readDepthBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer::EFrameBuffer framebuffer) override;
124 void readColorBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer::EFrameBuffer framebuffer) override;
125
126 void * map(BufferHandle bufferHandle, MapMode::EMapMode mapMode, uint32_t * stride = nullptr) override;
127 void * map(TextureHandle /*textureHandle*/, MapMode::EMapMode /*accessMode*/, uint32_t* /*rowPitch*/, uint32_t* /*depthPitch*/) override { return nullptr; }
128
129 void unmap(BufferHandle bufferHandle) override;
130 void unmap(TextureHandle) override {}
131
132 void updateBuffer(BufferHandle bufferHandle, const void* data, size_t size) override;
133
134 void updateSubTexture(TextureHandle textureHandle, const size_t level, const void * data) override;
135 void updateSubBuffer(BufferHandle bufferHandle, const size_t offset, const size_t size, const void * data) override;
136
137 void copyTexture(TextureHandle dstHandle, unsigned dstSub, unsigned dstX, unsigned dstY, unsigned dstZ, TextureHandle sourceHandle, unsigned srcSub) override;
138
139 void clearResource(BufferHandle /*destinationHandle*/, uint32_t* /*Values*/) override {}
140 void clearResource(BufferHandle /*destinationHandle*/, float* /*Values*/) override {}
141
142 void setBuffer(const BufferBindingHandle bufferBindingHandle, BufferHandle bufferHandle) override;
143
144 void setBufferCounter(BufferHandle bufferHandle, uint32_t value) override;
145
146 void setBufferCounter(BufferHandle bufferHandle, BufferHandle sourceBufferHandle) override;
147
148 void getBufferCounter(BufferHandle bufferHandle, BufferHandle destinationBufferHandle) override;
149
150 uint32_t getBufferCounter(BufferHandle bufferHandle) override;
151
152 void copyResource(BufferHandle destinationHandle, BufferHandle sourceHandle) override;
153 void copyResource(TextureHandle destinationHandle, TextureHandle sourceHandle) override;
154
155 void reset() override;
156
157
158 protected:
159
160 // wrapper for glBindTexture that updates state. Slot=-1 means just recycle current slot.
161 // Returns either a pointer to the bound texture or null
162 TextureGLES30* bindTexture(TextureHandle texture, GLuint unit = GLuint(~0u));
163
164 GLenum bindBuffer(OpenGLES30::BufferTarget target, GLuint buffer);
165
166 // Wrapper for binding a buffer for transfer operations.
167 //
168 // Here we usually use GL_COPY_(READ|WRITE)_BUFFER to avoid messing with the draw bind
169 // points. However, WebGL does not allow a buffer ever bound to anything else than
170 // GL_ELEMENT_ARRAY_BUFFER to be used as an index buffer, so we must use that as binding
171 // target. And binding that target can mess with the current VAO if one is bound. So
172 // we must unbind the VAO first in that case
173 GLenum bindBufferCopy(OpenGLES30::BufferTarget target, BufferGLES30& buffer)
174 {
175 if (buffer.target == OpenGLES30::BufferTarget::ElementArrayBuffer) {
176 unbindVAO();
177 return bindBuffer(OpenGLES30::BufferTarget::ElementArrayBuffer, buffer.bufferId);
178 }
179 return bindBuffer(target, buffer.bufferId);
180 }
181
182 // Run through all binding points and release if bound
183 void unbindBuffer(GLuint buffer);
184
185 void unbindVAO();
186
187 private:
188 void setupVertexAttributes(size_t baseVertex);
189
190 bool setupState();
191 bool setupDraw(size_t baseVertex);
192 bool setupIndexedDraw(GLenum& indexType, GLsizei& indexCount, const GLvoid*& indexOffset, const size_t startIndex, const size_t numIndices, const size_t baseVertex);
193
194 BuffersGLES30* buffers = nullptr;
195 TexturesGLES30* textures = nullptr;
196 EffectsGLES30* effects = nullptr;
197 RenderTargetsGLES30* renderTargets = nullptr;
198 CapabilitiesGLES30* capabilities = nullptr;
199 GLSyncObjects* syncObjects = nullptr;
200
201 SamplerStateHandle defaultSamplerStateHandle = SamplerStateHandle::NoHandle;
202
203 GLuint activeTexUnit = 0;
204 struct {
205 TextureHandle texture = TextureHandle::NoHandle;
206 SamplerStateHandle sampler = SamplerStateHandle::NoHandle;
207 GLenum target = GL_TEXTURE_2D;
208 } texUnits[OpenGLES30::maxTexUnits];
209
210 bool inRenderPass = false;
211 RenderPassInfo renderPassInfo = {};
212
213 RenderTargetHandle currentRenderTarget = RenderTargetHandle::NoHandle;
214 DepthStencilHandle currentDepthStencilTarget = DepthStencilHandle::NoHandle;
215
216 struct {
217 GLuint buffer = 0;
218 } bufferTargets[size_t(OpenGLES30::BufferTarget::Count)];
219
220 // Empty uniform buffer at block index 0 so that unused
221 // but defined uniform buffers have an index that passes
222 // validation. Bound in setDefaults.
223 BufferHandle emptyUniformBuffer = BufferHandle::NoHandle;
224
225 struct {
226 GLuint buffer = 0;
227 GLuint offset = 0;
228 GLuint size = 0;
229 } uniformBufferTargets[size_t(OpenGLES30::maxUniformBuffers)];
230
231 struct {
232 struct {
233 VertexBufferHandle handle = VertexBufferHandle::NoHandle;
234 uint32_t stride = 0;
235 uint32_t offset = 0;
236 } currItems[OpenGLES30::maxVertexAttributes];
237 uint32_t baseVertex = 0; // baseVertex is the vertex that corresponds to index 0.
238 uint32_t count = 0;
239 } vertexBuffers;
240
241 struct {
242 InputLayoutHandle prevHandle = InputLayoutHandle::NoHandle;
243 InputLayoutHandle currHandle = InputLayoutHandle::NoHandle;
244 } inputLayout;
245
246 struct {
247 IndexBufferHandle handle = IndexBufferHandle::NoHandle;
248 uint32_t stride = 0;
249 uint32_t offset = 0;
250 //uint32_t count = 0;
251 //GLenum type = 0;
252 } indexBuffer;
253
254 struct {
255 VertexArrayObjectHandle prevHandle = VertexArrayObjectHandle::NoHandle;
256 VertexArrayObjectHandle currHandle = VertexArrayObjectHandle::NoHandle;
257 GLenum indexType = GL_INVALID_ENUM;
258 GLsizei indexCount = 0;
259 GLsizei indexStride = 0;
260 } vertexArrayObject;
261
262 struct {
263 EffectHandle handle = EffectHandle::NoHandle;
264 EffectGLES30* ptr = nullptr;
265 } currentEffect;
266 uint32_t currentAttributeMask = 0;
267
268 RasterizerState currentRasterizerState = RasterizerState::DefaultState();
269
270 struct {
271 BlendStateHandle handle = BlendStateHandle::NoHandle;
272 float constant[4] = {};
273 BlendStateGLES30 state;
274 } currBlendState;
275
276 struct {
277 BlendStateHandle handle = BlendStateHandle::NoHandle;
278 float constant[4] = {};
279 } nextBlendState;
280
281 DepthStencilState currentDepthStencilState = DepthStencilState::DefaultState();
282
283 bool needVertexAttributeSetup = true;
284 };
285}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
void signal(FenceHandle fenceHandle) override
Insert a fence in the command stream that will signal when all commands before the fence are complete...
void setScalarVariable(const EffectVariableHandle variableHandle, const float value) override
Sets the given scalar floating point variable to the given value.
Definition: ContextGLES30.h:83
void setVector4Variable(const StringView &name, const float *value) override
Sets the vector variable with the given name to the given four-component value.
void setVector3Variable(const StringView &name, const float *value) override
Sets the vector variable with the given name to the given three-component value.
void setDepthStencilState(const DepthStencilStateHandle handle) override
Set the current depth stencil state.
void draw(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes) override
Draws non-indexed, non-instanced primitives.
void setViewport(const float x, const float y, const float width, const float height) override
Sets the current viewport to the given location and dimensions.
void setInputLayout(const InputLayoutHandle inputLayoutHandle) override
Sets the current input layout.
void drawIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex=0) override
Draws indexed, non-instanced primitives.
void beginRenderPass(const RenderPassInfo &info) override
Begin a render pass.
void setSamplerState(const SamplerStateBindingHandle samplerStateBindingHandle, const SamplerStateHandle samplerStateHandle) override
Sets the sampler state binding given to the given sampler state.
void readDepthBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer::EFrameBuffer framebuffer) override
Reads data from the current depth target into the given bufferHandle.
void setBuffer(const BufferBindingHandle bufferBindingHandle, BufferHandle bufferHandle) override
Sets a buffer to bind to the given binding.
void endRenderPass() override
End a render pass.
void setVertexArrayObject(VertexArrayObjectHandle vertexArrayObject) override
void updateSubTexture(TextureHandle textureHandle, const size_t level, const void *data) override
Update the data of a level in the given texture.
void unmap(BufferHandle bufferHandle) override
Unmaps the given buffer, applying any synchronization necessary to reflect changes in the mapped memo...
void setConstantBuffer(const ConstantBufferBindingHandle bufferBindingHandle, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size) override
Sets a constant buffer to the given constant buffer binding.
void updateSubBuffer(BufferHandle bufferHandle, const size_t offset, const size_t size, const void *data) override
Update a region of data in a buffer.
void setVector2Variable(const StringView &name, const float *value) override
Sets the vector variable with the given name to the given two-component value.
void setScissor(const int x, const int y, const int width, const int height) override
Sets the current scissor rectangle.
void setBlendState(const BlendStateHandle handle, const float *constant) override
Set the current blend state.
void setRasterizerState(const RasterizerStateHandle handle) override
Set the current rasterizer state.
void resolveResource(TextureHandle source, TextureHandle destination) override
Resolves the given source resource target into the given destination texture.
void readColorBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer::EFrameBuffer framebuffer) override
Reads data from the current render target into the given bufferHandle.
void reset() override
Resets all state changes made to the GPU since the last call to beginFrame.
void clearDepth(const float depth=1.0f) override
Clear the currently set depth/stencil target to the given depth.
void getBufferCounter(BufferHandle bufferHandle, BufferHandle destinationBufferHandle) override
Get the associated counter of a buffer.
void clearRenderTarget(const float *color) override
Clear the currently set render target to the given value (4 component floating point RGBA).
void setBufferCounter(BufferHandle bufferHandle, uint32_t value) override
Set the associated counter of a buffer.
void dispatchCompute(const unsigned int, const unsigned int, const unsigned int)
Dispatch computing work on the graphics device using the desired thread group count.
void setIndexBuffer(IndexBufferHandle indexBufferHandle, uint32_t stride, uint32_t offset) override
Sets the current index buffer.
void unmap(TextureHandle) override
Release a host mapping of a staging texture.
void * map(TextureHandle, MapMode::EMapMode, uint32_t *, uint32_t *) override
Create host mapping of a staging texture.
void drawInstanced(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes, const size_t startInstance, const size_t numInstances) override
Draws non-indexed, instanced primitives.
void drawInstancedIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startInstance, const size_t numInstances, const size_t startIndex, const size_t numIndexes) override
Draws indexed, instanced primitives.
void setVariable(const EffectVariableHandle index, const uint8_t *data, size_t size) override
Sets the variable with the given name and size.
void clearCachedState() final
Prepare context for external manipulation of graphics device.
void setMatrixVariable(const StringView &name, const float *value) override
Sets the matrix variable with the given name in the current effect to value.
void setMatrixVariable(const EffectVariableHandle variableHandle, const float *value) override
Sets the given matrix variable to value.
Definition: ContextGLES30.h:80
void setScalarVariable(const EffectVariableHandle variableHandle, int value) override
Sets the given scalar integer point variable to the given value.
Definition: ContextGLES30.h:86
void setEffect(EffectHandle handle) override
Set the current effect.
void setVertexBuffers(const VertexBufferHandle *vertexBufferHandles, const size_t count, const uint32_t *strides, const uint32_t *offsets) override
Sets the current vertex buffers.
void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override
Sets the current render target and an associated depth stencil target.
void setScalarVariable(const StringView &name, const float value) override
Sets the scalar floating point variable with the given name to the given value.
void updateBuffer(BufferHandle bufferHandle, const void *data, size_t size) override
Replace contents of buffer with new data.
static DepthStencilState DefaultState()
Constructs a depth stencil state object initialized with the default values.
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:77
Provides effects and shader management functionality.
Definition: IEffects.h:148
EMapMode
Mapping mode enumeration.
Definition: Flags.h:93
EPrimitiveType
Primitive type enumeration.
Definition: Common.h:114
static RasterizerState DefaultState()
Constructs a rasterizer state initialized with the default values.