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 setTexture(const TextureBindingHandle textureBindingHandle, TextureViewHandle textureViewHandle) final;
74 void setTexture(const TextureBindingHandle textureBindingHandle, const TextureHandle textureHandle) final;
75 void setSamplerState(const SamplerStateBindingHandle samplerStateBindingHandle, const SamplerStateHandle samplerStateHandle) override;
76 void setConstantBuffer(const ConstantBufferBindingHandle bufferBindingHandle, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size) override;
77
78 void setInputLayout(const InputLayoutHandle inputLayoutHandle) override;
79 void setVertexBuffers(const VertexBufferHandle* vertexBufferHandles, const size_t count, const uint32_t* strides, const uint32_t* offsets) override;
80 void setVertexBuffers(const VertexBufferHandle * vertexBufferHandles, const size_t numVertexBuffers) override;
81
82 void setIndexBuffer(IndexBufferHandle indexBufferHandle, uint32_t stride, uint32_t offset) override;
83
84 void setVertexArrayObject(VertexArrayObjectHandle vertexArrayObject) override;
85
86 void draw(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes) override;
87 void drawIndexed(PrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex = 0) override;
88
89 void drawInstanced(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes, const size_t startInstance, const size_t numInstances) override;
90 void drawInstancedIndexed(PrimitiveType primitiveType, const size_t startInstance, const size_t numInstances, const size_t startIndex, const size_t numIndexes) override;
91
92 void dispatchCompute(const unsigned int /*threadGroupsX*/, const unsigned int /*threadGroupsY*/, const unsigned int /*threadGroupsZ*/) {}
93
94 //
95 // Render target, texture and buffer manipulation.
96 //
97
98 void resolveResource(TextureHandle source, TextureHandle destination) override;
99
100 void readDepthBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer framebuffer) override;
101 void readColorBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer framebuffer) override;
102
103 void * map(BufferHandle bufferHandle, MapMode::EMapMode mapMode, uint32_t * stride = nullptr) override;
104 void * map(TextureHandle /*textureHandle*/, MapMode::EMapMode /*accessMode*/, uint32_t* /*rowPitch*/, uint32_t* /*depthPitch*/) override { return nullptr; }
105
106 void unmap(BufferHandle bufferHandle) override;
107 void unmap(TextureHandle) override {}
108
109 void updateBuffer(BufferHandle bufferHandle, const void* data, size_t size) override;
110
111 void updateSubTexture(TextureHandle textureHandle, const size_t level, const void * data) override;
112 void updateSubBuffer(BufferHandle bufferHandle, const size_t offset, const size_t size, const void * data) override;
113
114 void copyTexture(TextureHandle dstHandle, unsigned dstSub, unsigned dstX, unsigned dstY, unsigned dstZ, TextureHandle sourceHandle, unsigned srcSub) override;
115
116 void clearResource(BufferHandle /*destinationHandle*/, uint32_t* /*Values*/) override {}
117 void clearResource(BufferHandle /*destinationHandle*/, float* /*Values*/) override {}
118
119 void setBuffer(const BufferBindingHandle bufferBindingHandle, BufferHandle bufferHandle) override;
120
121 void setBufferCounter(BufferHandle bufferHandle, uint32_t value) override;
122
123 void setBufferCounter(BufferHandle bufferHandle, BufferHandle sourceBufferHandle) override;
124
125 void getBufferCounter(BufferHandle bufferHandle, BufferHandle destinationBufferHandle) override;
126
127 uint32_t getBufferCounter(BufferHandle bufferHandle) override;
128
129 void copyResource(BufferHandle destinationHandle, BufferHandle sourceHandle) override;
130 void copyResource(TextureHandle destinationHandle, TextureHandle sourceHandle) override;
131
132 void reset() override;
133
134
135 protected:
136
137 // wrapper for glBindTexture that updates state. Slot=-1 means just recycle current slot.
138 // Returns either a pointer to the bound texture or null
139 TextureGLES30* bindTexture(TextureHandle texture, GLuint unit = GLuint(~0u));
140
141 GLenum bindBuffer(OpenGLES30::BufferTarget target, GLuint buffer);
142
143 // Wrapper for binding a buffer for transfer operations.
144 //
145 // Here we usually use GL_COPY_(READ|WRITE)_BUFFER to avoid messing with the draw bind
146 // points. However, WebGL does not allow a buffer ever bound to anything else than
147 // GL_ELEMENT_ARRAY_BUFFER to be used as an index buffer, so we must use that as binding
148 // target. And binding that target can mess with the current VAO if one is bound. So
149 // we must unbind the VAO first in that case
150 GLenum bindBufferCopy(OpenGLES30::BufferTarget target, BufferGLES30& buffer)
151 {
152 if (buffer.target == OpenGLES30::BufferTarget::ElementArrayBuffer) {
153 unbindVAO();
154 return bindBuffer(OpenGLES30::BufferTarget::ElementArrayBuffer, buffer.bufferId);
155 }
156 return bindBuffer(target, buffer.bufferId);
157 }
158
159 // Run through all binding points and release if bound
160 void unbindBuffer(GLuint buffer);
161
162 void unbindVAO();
163
164 private:
165 void setupVertexAttributes(size_t baseVertex);
166
167 bool setupState();
168 bool setupDraw(size_t baseVertex);
169 bool setupIndexedDraw(GLenum& indexType, GLsizei& indexCount, const GLvoid*& indexOffset, const size_t startIndex, const size_t numIndices, const size_t baseVertex);
170
171 BuffersGLES30* buffers = nullptr;
172 TexturesGLES30* textures = nullptr;
173 EffectsGLES30* effects = nullptr;
174 RenderTargetsGLES30* renderTargets = nullptr;
175 CapabilitiesGLES30* capabilities = nullptr;
176 GLSyncObjects* syncObjects = nullptr;
177
178 SamplerStateHandle defaultSamplerStateHandle = SamplerStateHandle::NoHandle;
179
180 GLuint activeTexUnit = 0;
181 struct {
182 TextureHandle texture = TextureHandle::NoHandle;
183 SamplerStateHandle sampler = SamplerStateHandle::NoHandle;
184 GLenum target = GL_TEXTURE_2D;
185 } texUnits[OpenGLES30::maxTexUnits];
186
187 bool inRenderPass = false;
188 RenderPassInfo renderPassInfo = {};
189
190 RenderTargetHandle currentRenderTarget = RenderTargetHandle::NoHandle;
191 DepthStencilHandle currentDepthStencilTarget = DepthStencilHandle::NoHandle;
192
193 struct {
194 GLuint buffer = 0;
195 } bufferTargets[size_t(OpenGLES30::BufferTarget::Count)];
196
197 // Empty uniform buffer at block index 0 so that unused
198 // but defined uniform buffers have an index that passes
199 // validation. Bound in setDefaults.
200 BufferHandle emptyUniformBuffer = BufferHandle::NoHandle;
201
202 struct {
203 GLuint buffer = 0;
204 GLuint offset = 0;
205 GLuint size = 0;
206 } uniformBufferTargets[size_t(OpenGLES30::maxUniformBuffers)];
207
208 struct {
209 struct {
210 VertexBufferHandle handle = VertexBufferHandle::NoHandle;
211 uint32_t stride = 0;
212 uint32_t offset = 0;
213 } currItems[OpenGLES30::maxVertexAttributes];
214 uint32_t baseVertex = 0; // baseVertex is the vertex that corresponds to index 0.
215 uint32_t count = 0;
216 } vertexBuffers;
217
218 struct {
219 InputLayoutHandle prevHandle = InputLayoutHandle::NoHandle;
220 InputLayoutHandle currHandle = InputLayoutHandle::NoHandle;
221 } inputLayout;
222
223 struct {
224 IndexBufferHandle handle = IndexBufferHandle::NoHandle;
225 uint32_t stride = 0;
226 uint32_t offset = 0;
227 //uint32_t count = 0;
228 //GLenum type = 0;
229 } indexBuffer;
230
231 struct {
232 VertexArrayObjectHandle prevHandle = VertexArrayObjectHandle::NoHandle;
233 VertexArrayObjectHandle currHandle = VertexArrayObjectHandle::NoHandle;
234 GLenum indexType = GL_INVALID_ENUM;
235 GLsizei indexCount = 0;
236 GLsizei indexStride = 0;
237 } vertexArrayObject;
238
239 struct {
240 EffectHandle handle = EffectHandle::NoHandle;
241 EffectGLES30* ptr = nullptr;
242 } currentEffect;
243 uint32_t currentAttributeMask = 0;
244
245 RasterizerState currentRasterizerState = RasterizerState::DefaultState();
246
247 struct {
248 BlendStateHandle handle = BlendStateHandle::NoHandle;
249 float constant[4] = {};
250 BlendStateGLES30 state;
251 } currBlendState;
252
253 struct {
254 BlendStateHandle handle = BlendStateHandle::NoHandle;
255 float constant[4] = {};
256 } nextBlendState;
257
258 DepthStencilState currentDepthStencilState = DepthStencilState::DefaultState();
259
260 bool needVertexAttributeSetup = true;
261 };
262}
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Framebuffer
Framebuffers to select from when doing framebuffer operations.
Definition: Common.h:147
PrimitiveType
Primitive types for interpreting vertex data sent to the graphics pipeline.
Definition: Common.h:112
void signal(FenceHandle fenceHandle) override
Insert a fence in the command stream that will signal when all commands before the fence are complete...
void setDepthStencilState(const DepthStencilStateHandle handle) override
Set the current depth stencil state.
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 drawInstanced(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes, const size_t startInstance, const size_t numInstances) override
Draws non-indexed, 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 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 readDepthBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer framebuffer) override
Reads data from the current depth target into the given bufferHandle.
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 readColorBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer framebuffer) override
Reads data from the current render target into the given bufferHandle.
void resolveResource(TextureHandle source, TextureHandle destination) override
Resolves the given source resource target into the given destination texture.
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.
Definition: ContextGLES30.h:92
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 drawInstancedIndexed(PrimitiveType primitiveType, const size_t startInstance, const size_t numInstances, const size_t startIndex, const size_t numIndexes) override
Draws indexed, instanced primitives.
void clearCachedState() final
Prepare context for external manipulation of graphics device.
void draw(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes) override
Draws non-indexed, non-instanced primitives.
void drawIndexed(PrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex=0) override
Draws indexed, non-instanced primitives.
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 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:78
Provides effects and shader management functionality.
Definition: IEffects.h:148
EMapMode
Mapping mode enumeration.
Definition: Flags.h:93
static RasterizerState DefaultState()
Constructs a rasterizer state initialized with the default values.