Cogs.Core
ContextGL20.h
1#pragma once
2
3#include "../IContext.h"
4
5#include "CommonGL20.h"
6#include "../Base/ContextCommon.h"
7#include "../BaseGL/SyncObjects.h"
8
9namespace Cogs
10{
11 struct CapabilitiesGL20;
12 struct BufferGL20;
13 struct BuffersGL20;
14 struct TexturesGL20;
15 struct EffectsGL20;
16 struct RenderTargetsGL20;
17
19 {
20 int effectAtomicCounterVariableIndex;
21 GLuint backingBuffer;
22 };
23
25 {
26 BufferGL20 * currentVertexBuffers[kMaxVertexInputSlots];
27 BufferGL20 * currentIndexBuffer;
28 uint32_t indexBufferStride = 4;
29 };
30
32 {
34
35 void initialize(CapabilitiesGL20* capabilities,
36 BuffersGL20* buffers,
37 TexturesGL20* textures,
38 EffectsGL20* effects,
39 RenderTargetsGL20* renderTargets,
40 GLSyncObjects* sync);
41
42 void releaseResources();
43
44 EffectHandle getCurrentEffect() override;
45 IEffects * getEffects() override;
46 InputAssemblerStateCommon * getIAState() override { return &iaState; }
47 InputAssemblerStateGL20 iaState = {};
48
49 CapabilitiesGL20 * capabilities = nullptr;
50 BuffersGL20 * buffers = nullptr;
51 TexturesGL20 * textures = nullptr;
52 EffectsGL20 * effects = nullptr;
53 RenderTargetsGL20 * renderTargets = nullptr;
54 GLSyncObjects* syncObjects = nullptr;
55
56 void signal(FenceHandle fenceHandle) override;
57
58 void beginRenderPass(const RenderPassInfo &info) override;
59 void endRenderPass() override;
60
61 void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override;
62 void setViewport(const float x, const float y, const float width, const float height) override;
63 void setScissor(const int x, const int y, const int width, const int height) override;
64
65 void clearRenderTarget(const float * color) override;
66 void clearRenderTarget(const float ** colors, const int count) override;
67
68 void clearDepth(const float depth = 1.0f) override;
69
70 void setDepthStencilState(const DepthStencilStateHandle handle) override;
71 void setBlendState(const BlendStateHandle handle, const float* constant) override;
72 void setRasterizerState(const RasterizerStateHandle handle) override;
73
74 void resolveResource(TextureHandle source, TextureHandle destination) override;
75
76 void setEffect(EffectHandle effectHandle) override;
77 EffectHandle getEffect() const;
78
79 void setTexture(const TextureBindingHandle textureBindingHandle, const TextureHandle textureHandle) override;
80 void setTexture(const TextureBindingHandle textureBindingHandle, TextureViewHandle textureViewHandle) override;
81 void setSamplerState(const SamplerStateBindingHandle samplerStateBindingHandle, const SamplerStateHandle samplerStateHandle) override;
82
83 void setInputLayout(const InputLayoutHandle inputLayoutHandle) override;
84 void setVertexBuffers(const VertexBufferHandle * vertexBufferHandles, const size_t count, const uint32_t * strides, const uint32_t * offsets) override;
85 void setVertexBuffers(const VertexBufferHandle * vertexBufferHandles, const size_t count) override;
86 void setIndexBuffer(IndexBufferHandle indexBufferHandle, uint32_t stride, uint32_t offset) override;
87
88 void setConstantBuffer(const ConstantBufferBindingHandle bufferBinding, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size) override;
89
90 void setBuffer(const BufferBindingHandle bufferBindingHandle, BufferHandle bufferHandle) override;
91 void setBufferCounter(BufferHandle bufferHandle, uint32_t value) override;
92 void setBufferCounter(BufferHandle bufferHandle, BufferHandle sourceBufferHandle) override;
93 void getBufferCounter(BufferHandle bufferHandle, BufferHandle destinationBufferHandle) override;
94
95 uint32_t getBufferCounter(BufferHandle bufferHandle) override;
96
97 void draw(PrimitiveType primitiveType, const size_t startVertex, const size_t numPrimitives) override;
98 void drawIndexed(PrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex = 0) override;
99
100 void drawInstanced(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes, const size_t startInstance, const size_t numInstances) override;
101 void drawInstancedIndexed(PrimitiveType primitiveType, const size_t startInstance, const size_t numInstances, const size_t startIndex, const size_t numIndexes) override;
102
103 void dispatchCompute(const unsigned int threadGroupsX, const unsigned int threadGroupsY, const unsigned int threadGroupsZ) override;
104
105 void readDepthBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer framebuffer) override;
106 void readColorBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer framebuffer) override;
107
108 void * map(BufferHandle bufferHandle, MapMode::EMapMode mapMode, uint32_t * stride) override;
109 void unmap(BufferHandle bufferHandle) override;
110
111 void * map(TextureHandle /*textureHandle*/, MapMode::EMapMode /*accessMode*/, uint32_t * /*rowPitch*/, uint32_t * /*depthPitch*/) override { return nullptr; }
112 void unmap(TextureHandle) override {}
113
114 void updateSubTexture(TextureHandle textureHandle, const size_t level, const void * data) override;
115 void updateSubBuffer(BufferHandle bufferHandle, const size_t offset, const size_t size, const void * data) override;
116
117 void copyResource(BufferHandle destinationHandle, BufferHandle sourceHandle) override;
118 void copyResource(TextureHandle destinationHandle, TextureHandle sourceHandle) override;
119
120 void copySubResource(BufferHandle destinationHandle, size_t destinationOffset, BufferHandle sourceHandle, size_t sourceOffset, size_t size);
121
122 void copyTexture(TextureHandle dstHandle, unsigned dstSub, unsigned dstX, unsigned dstY, unsigned dstZ, TextureHandle sourceHandle, unsigned srcSub) override;
123
124 void clearResource(BufferHandle destinationHandle, uint32_t *Values) override;
125 void clearResource(BufferHandle destinationHandle, float *Values) override;
126
127 void preDraw();
128 void postDraw();
129
130 void syncCounterToEffect(const AtomicCounterMapGL20 mapping);
131 void syncCounterFromEffect(const AtomicCounterMapGL20 mapping);
132
133 void setDefaults();
134 void reset() override;
135 void setupVertexAttributes();
136 void clearInputState();
137
138 void pushCommandGroupAnnotation(const StringView & name) override;
139 void popCommandGroupAnnotation() override;
140 void setAnnotationMarker(const StringView & name) override;
141
142 SamplerStateHandle defaultSamplerStateHandle;
143
144 private:
145 std::vector<AtomicCounterMapGL20> atomicCounterMappings;
146
147 EffectHandle currentEffect;
148 GLuint currentProgramId = 0;
149 unsigned char * currentAttributeMap = nullptr;
150
151 bool inRenderPass = false;
152 RenderPassInfo renderPassInfo = {};
153
154 RenderTargetHandle currentRenderTarget;
155 DepthStencilStateHandle currentDepthStencilState;
156
157 std::vector<GLint> enabledAttributes;
158
159 TextureHandle boundTextures[192];
160 SamplerStateHandle boundSamplerStates[192];
161 GLuint boundTarget[192];
162#ifndef __APPLE__
163 GLuint maxImageUnits = 8;
164#endif
165
166 GLuint vao = 0;
167
168 bool needVertexAttributeSetup = true;
169 bool enableMappingWorkaround = true;
170 bool unorderedMemoryAcccess = false;
171 };
172}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
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 pushCommandGroupAnnotation(const StringView &name) override
Begin to tag a sequence of commands as a group in graphics debugger.
void setBufferCounter(BufferHandle bufferHandle, uint32_t value) override
Set the associated counter of a buffer.
void setIndexBuffer(IndexBufferHandle indexBufferHandle, uint32_t stride, uint32_t offset) override
Sets the current index buffer.
void clearRenderTarget(const float *color) override
Clear the currently set render target to the given value (4 component floating point RGBA).
void * map(TextureHandle, MapMode::EMapMode, uint32_t *, uint32_t *) override
Create host mapping of a staging texture.
Definition: ContextGL20.h:111
void getBufferCounter(BufferHandle bufferHandle, BufferHandle destinationBufferHandle) override
Get the associated counter of a buffer.
void unmap(BufferHandle bufferHandle) override
Unmaps the given buffer, applying any synchronization necessary to reflect changes in the mapped memo...
void clearDepth(const float depth=1.0f) override
Clear the currently set depth/stencil target to the given depth.
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 draw(PrimitiveType primitiveType, const size_t startVertex, const size_t numPrimitives) override
Draws non-indexed, non-instanced primitives.
void unmap(TextureHandle) override
Release a host mapping of a staging texture.
Definition: ContextGL20.h:112
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 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 setBlendState(const BlendStateHandle handle, const float *constant) override
Set the current blend state.
void updateSubTexture(TextureHandle textureHandle, const size_t level, const void *data) override
Update the data of a level in the given texture.
void dispatchCompute(const unsigned int threadGroupsX, const unsigned int threadGroupsY, const unsigned int threadGroupsZ) override
Dispatch computing work on the graphics device using the desired thread group count.
void reset() override
Resets all state changes made to the GPU since the last call to beginFrame.
void setBuffer(const BufferBindingHandle bufferBindingHandle, BufferHandle bufferHandle) override
Sets a buffer to bind to the given binding.
void signal(FenceHandle fenceHandle) override
Insert a fence in the command stream that will signal when all commands before the fence are complete...
void setRasterizerState(const RasterizerStateHandle handle) override
Set the current rasterizer state.
void endRenderPass() override
End a render pass.
void setConstantBuffer(const ConstantBufferBindingHandle bufferBinding, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size) override
Sets a constant buffer to the given constant buffer binding.
void setAnnotationMarker(const StringView &name) override
Add a tag in the sequence of commands in graphics debugger.
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 setVertexBuffers(const VertexBufferHandle *vertexBufferHandles, const size_t count, const uint32_t *strides, const uint32_t *offsets) override
Sets the current vertex buffers.
void setDepthStencilState(const DepthStencilStateHandle handle) override
Set the current depth stencil state.
void popCommandGroupAnnotation() override
End to tag a sequence of commands as a group in graphics debugger.
void setSamplerState(const SamplerStateBindingHandle samplerStateBindingHandle, const SamplerStateHandle samplerStateHandle) override
Sets the sampler state binding given to the given sampler state.
void drawIndexed(PrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex=0) override
Draws indexed, non-instanced primitives.
void setTexture(const TextureBindingHandle textureBindingHandle, const TextureHandle textureHandle) override
Sets the texture given to the binding given by textureBindingHandle.
void * map(BufferHandle bufferHandle, MapMode::EMapMode mapMode, uint32_t *stride) override
Maps the given buffer so it can be accessed.
void setInputLayout(const InputLayoutHandle inputLayoutHandle) override
Sets the current input layout.
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 setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override
Sets the current render target and an associated depth stencil target.
void beginRenderPass(const RenderPassInfo &info) override
Begin a render pass.
void setEffect(EffectHandle effectHandle) override
Set the current effect.
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 setScissor(const int x, const int y, const int width, const int height) override
Sets the current scissor rectangle.
void resolveResource(TextureHandle source, TextureHandle destination) override
Resolves the given source resource target into the given destination texture.
Provides effects and shader management functionality.
Definition: IEffects.h:148
EMapMode
Mapping mode enumeration.
Definition: Flags.h:93