Cogs.Core
ContextGL20.h
1#pragma once
2
3#include "../IContext.h"
4
5#include "CommonGL20.h"
6#include "OpenGLState.h"
7#include "../Base/ContextCommon.h"
8#include "../BaseGL/SyncObjects.h"
9
10namespace Cogs
11{
12 struct CapabilitiesGL20;
13 struct BufferGL20;
14 struct BuffersGL20;
15 struct TexturesGL20;
16 struct EffectsGL20;
17 struct RenderTargetsGL20;
18
20 {
21 int effectAtomicCounterVariableIndex;
22 GLuint backingBuffer;
23 };
24
26 {
27 BufferGL20 * currentVertexBuffers[kMaxVertexInputSlots];
28 BufferGL20 * currentIndexBuffer;
29 uint32_t indexBufferStride = 4;
30 };
31
33 {
35
36 void initialize(CapabilitiesGL20* capabilities,
37 BuffersGL20* buffers,
38 TexturesGL20* textures,
39 EffectsGL20* effects,
40 RenderTargetsGL20* renderTargets,
41 GLSyncObjects* sync);
42
43 void releaseResources();
44
45 EffectHandle getCurrentEffect() override;
46 IEffects * getEffects() override;
47 InputAssemblerStateCommon * getIAState() override { return &iaState; }
48 InputAssemblerStateGL20 iaState = {};
49
50 CapabilitiesGL20 * capabilities = nullptr;
51 BuffersGL20 * buffers = nullptr;
52 TexturesGL20 * textures = nullptr;
53 EffectsGL20 * effects = nullptr;
54 RenderTargetsGL20 * renderTargets = nullptr;
55 GLSyncObjects* syncObjects = nullptr;
56
57 void signal(FenceHandle fenceHandle) override;
58
59 void beginRenderPass(const RenderPassInfo &info) override;
60 void endRenderPass() override;
61
62 void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle) override;
63 void setViewport(const float x, const float y, const float width, const float height) override;
64 void setScissor(const int x, const int y, const int width, const int height) override;
65
66 void clearRenderTarget(const float * color) override;
67 void clearRenderTarget(const float ** colors, const int count) override;
68
69 void clearDepth(const float depth = 1.0f) override;
70
71 void setDepthStencilState(const DepthStencilStateHandle handle) override;
72 void setBlendState(const BlendStateHandle handle, const float* constant) override;
73 void setRasterizerState(const RasterizerStateHandle handle) override;
74
75 void resolveResource(TextureHandle source, TextureHandle destination) override;
76
77 void setEffect(EffectHandle effectHandle) override;
78 EffectHandle getEffect() const;
79
80 void setTexture(const TextureBindingHandle textureBindingHandle, const TextureHandle textureHandle) override;
81 void setTexture(const TextureBindingHandle textureBindingHandle, TextureViewHandle textureViewHandle) override;
82 void setSamplerState(const SamplerStateBindingHandle samplerStateBindingHandle, const SamplerStateHandle samplerStateHandle) override;
83
84 void setInputLayout(const InputLayoutHandle inputLayoutHandle) override;
85 void setVertexBuffers(const VertexBufferHandle * vertexBufferHandles, const size_t count, const uint32_t * strides, const uint32_t * offsets) override;
86 void setVertexBuffers(const VertexBufferHandle * vertexBufferHandles, const size_t count) override;
87 void setIndexBuffer(IndexBufferHandle indexBufferHandle, uint32_t stride, uint32_t offset) override;
88
89 void setConstantBuffer(const ConstantBufferBindingHandle bufferBinding, const BufferHandle bufferHandle, const uint32_t offset, const uint32_t size) override;
90
91 void setBuffer(const BufferBindingHandle bufferBindingHandle, BufferHandle bufferHandle) override;
92 void setBufferCounter(BufferHandle bufferHandle, uint32_t value) override;
93 void setBufferCounter(BufferHandle bufferHandle, BufferHandle sourceBufferHandle) override;
94 void getBufferCounter(BufferHandle bufferHandle, BufferHandle destinationBufferHandle) override;
95
96 uint32_t getBufferCounter(BufferHandle bufferHandle) override;
97
98 void draw(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numPrimitives) override;
99 void drawIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex = 0) override;
100
101 void drawInstanced(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes, const size_t startInstance, const size_t numInstances) override;
102 void drawInstancedIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startInstance, const size_t numInstances, const size_t startIndex, const size_t numIndexes) override;
103
104 void dispatchCompute(const unsigned int threadGroupsX, const unsigned int threadGroupsY, const unsigned int threadGroupsZ) override;
105
106 void readDepthBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer::EFrameBuffer framebuffer) override;
107 void readColorBuffer(BufferHandle bufferHandle, int x, int y, int width, int height, Framebuffer::EFrameBuffer framebuffer) override;
108
109 void * map(BufferHandle bufferHandle, MapMode::EMapMode mapMode, uint32_t * stride) override;
110 void unmap(BufferHandle bufferHandle) override;
111
112 void * map(TextureHandle /*textureHandle*/, MapMode::EMapMode /*accessMode*/, uint32_t * /*rowPitch*/, uint32_t * /*depthPitch*/) override { return nullptr; }
113 void unmap(TextureHandle) override {}
114
115 void updateSubTexture(TextureHandle textureHandle, const size_t level, const void * data) override;
116 void updateSubBuffer(BufferHandle bufferHandle, const size_t offset, const size_t size, const void * data) override;
117
118 void copyResource(BufferHandle destinationHandle, BufferHandle sourceHandle) override;
119 void copyResource(TextureHandle destinationHandle, TextureHandle sourceHandle) override;
120
121 void copySubResource(BufferHandle destinationHandle, size_t destinationOffset, BufferHandle sourceHandle, size_t sourceOffset, size_t size);
122
123 void copyTexture(TextureHandle dstHandle, unsigned dstSub, unsigned dstX, unsigned dstY, unsigned dstZ, TextureHandle sourceHandle, unsigned srcSub) override;
124
125 void clearResource(BufferHandle destinationHandle, uint32_t *Values) override;
126 void clearResource(BufferHandle destinationHandle, float *Values) override;
127
128 void preDraw();
129 void postDraw();
130
131 void syncCounterToEffect(const AtomicCounterMapGL20 mapping);
132 void syncCounterFromEffect(const AtomicCounterMapGL20 mapping);
133
134 void setDefaults();
135 void reset() override;
136 void setupVertexAttributes();
137 void clearInputState();
138
139 void pushCommandGroupAnnotation(const StringView & name) override;
140 void popCommandGroupAnnotation() override;
141 void setAnnotationMarker(const StringView & name) override;
142
143 SamplerStateHandle defaultSamplerStateHandle;
144
145 private:
146 std::vector<AtomicCounterMapGL20> atomicCounterMappings;
147
148 EffectHandle currentEffect;
149 GLuint currentProgramId = 0;
150 unsigned char * currentAttributeMap = nullptr;
151
152 bool inRenderPass = false;
153 RenderPassInfo renderPassInfo = {};
154
155 RenderTargetHandle currentRenderTarget;
156 DepthStencilStateHandle currentDepthStencilState;
157
158 std::vector<GLint> enabledAttributes;
159
160 TextureHandle boundTextures[192];
161 SamplerStateHandle boundSamplerStates[192];
162 GLuint boundTarget[192];
163#ifndef __APPLE__
164 GLuint maxImageUnits = 8;
165#endif
166
167 OpenGLState glState;
168 GLuint vao = 0;
169
170 bool needVertexAttributeSetup = true;
171 bool enableMappingWorkaround = true;
172 bool unorderedMemoryAcccess = false;
173 };
174}
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 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:112
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 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 unmap(TextureHandle) override
Release a host mapping of a staging texture.
Definition: ContextGL20.h:113
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 drawIndexed(PrimitiveType::EPrimitiveType primitiveType, const size_t startIndex, const size_t numIndexes, const size_t startVertex=0) override
Draws indexed, non-instanced primitives.
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 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 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 draw(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numPrimitives) override
Draws non-indexed, non-instanced primitives.
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 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 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 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 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 resolveResource(TextureHandle source, TextureHandle destination) override
Resolves the given source resource target into the given destination texture.
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.
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