|
| 1 | + |
| 2 | +#include "GLCommands.h" |
| 3 | + |
| 4 | +#ifndef USE_GLEW |
| 5 | +#define GL_GLEXT_PROTOTYPES |
| 6 | +#include <gl/glext.h> |
| 7 | +#endif |
| 8 | + |
| 9 | +#if _WIN32 || _WIN64 |
| 10 | +#if _WIN64 |
| 11 | +#define USE_64_BIT |
| 12 | +#else |
| 13 | +#define USE_32_BIT |
| 14 | +#endif |
| 15 | +#endif |
| 16 | +#if __GNUC__ |
| 17 | +#if __x86_64__ || __ppc64__ |
| 18 | +#define USE_64_BIT |
| 19 | +#else |
| 20 | +#define USE_32_BIT |
| 21 | +#endif |
| 22 | +#endif |
| 23 | + |
| 24 | +namespace cmds |
| 25 | +{ |
| 26 | +#ifdef USE_64_BIT |
| 27 | + typedef uint64_t index_size_t; |
| 28 | +#else |
| 29 | + typedef uint32_t index_size_t; |
| 30 | +#endif |
| 31 | + static const index_size_t kIndexSizes[] = { sizeof(uint32_t), sizeof(uint16_t) }; |
| 32 | + static const GLenum kIndexTypes[] = { GL_UNSIGNED_INT, GL_UNSIGNED_SHORT }; |
| 33 | + |
| 34 | + enum class BufferLockType |
| 35 | + { |
| 36 | + eDiscard, |
| 37 | + eReadOnly, |
| 38 | + eWriteOnly |
| 39 | + }; |
| 40 | + |
| 41 | + void drawArrays(const void* commandData, cb::RenderContext*) |
| 42 | + { |
| 43 | + const auto& cmd = *reinterpret_cast<const cmds::DrawArrays*>(commandData); |
| 44 | + glBindVertexArray(cmd.vao); |
| 45 | + glDrawArrays(cmd.primitive, cmd.base, cmd.count); |
| 46 | + } |
| 47 | + |
| 48 | + void drawIndexed(const void* commandData, cb::RenderContext*) |
| 49 | + { |
| 50 | + const auto& cmd = *reinterpret_cast<const cmds::DrawIndexed*>(commandData); |
| 51 | + |
| 52 | + auto base = reinterpret_cast<const void*>(kIndexSizes[cmd.useShortIndices]); |
| 53 | + |
| 54 | + glBindVertexArray(cmd.vao); |
| 55 | + glDrawElements(cmd.primitive, cmd.count, kIndexTypes[cmd.useShortIndices], base); |
| 56 | + } |
| 57 | + |
| 58 | + void drawInstanced(const void* commandData, cb::RenderContext*) |
| 59 | + { |
| 60 | + const auto& cmd = *reinterpret_cast<const cmds::DrawInstanced*>(commandData); |
| 61 | + glBindVertexArray(cmd.vao); |
| 62 | + glDrawArraysInstanced(cmd.primitive, cmd.base, cmd.count, cmd.instanceCount); |
| 63 | + } |
| 64 | + |
| 65 | + void clearColor(const void* commandData, cb::RenderContext*) |
| 66 | + { |
| 67 | + const auto& cmd = *reinterpret_cast<const cmds::ClearColor*>(commandData); |
| 68 | + glClearColor(cmd.red, cmd.green, cmd.blue, cmd.alpha); |
| 69 | + glClear(cmd.flags); |
| 70 | + } |
| 71 | + |
| 72 | + void depthSetup(const void* commandData, cb::RenderContext*) |
| 73 | + { |
| 74 | + const auto& cmd = *reinterpret_cast<const cmds::DepthSetup*>(commandData); |
| 75 | + glDepthMask(cmd.depthWrite); |
| 76 | + if (cmd.depthTest) |
| 77 | + glEnable(GL_DEPTH_TEST); |
| 78 | + else |
| 79 | + glDisable(GL_DEPTH_TEST); |
| 80 | + glDepthFunc(cmd.compare); |
| 81 | + } |
| 82 | + |
| 83 | + void blendSetup(const void* commandData, cb::RenderContext*) |
| 84 | + { |
| 85 | + const auto& cmd = *reinterpret_cast<const cmds::BlendSetup*>(commandData); |
| 86 | + if (cmd.enable) |
| 87 | + glEnable(GL_BLEND); |
| 88 | + else |
| 89 | + glDisable(GL_BLEND); |
| 90 | + glBlendFunc(cmd.src, cmd.dst); |
| 91 | + } |
| 92 | + |
| 93 | + void bindShader(const void* commandData, cb::RenderContext*) |
| 94 | + { |
| 95 | + const auto& cmd = *reinterpret_cast<const cmds::BindShader*>(commandData); |
| 96 | + glUseProgram(cmd.shader); |
| 97 | + } |
| 98 | + |
| 99 | + void bindTexture(const void* commandData, cb::RenderContext*) |
| 100 | + { |
| 101 | + const auto& cmd = *reinterpret_cast<const cmds::BindTexture*>(commandData); |
| 102 | + glActiveTexture(GL_TEXTURE0 + cmd.textureUnit); |
| 103 | + glBindTexture(cmd.type, cmd.texture); |
| 104 | + } |
| 105 | + |
| 106 | + void bindUniformBuffer(const void* commandData, cb::RenderContext*) |
| 107 | + { |
| 108 | + const auto& cmd = *reinterpret_cast<const cmds::BindUniformBuffer*>(commandData); |
| 109 | + glBindBufferBase(GL_UNIFORM_BUFFER, cmd.location, cmd.ubo); |
| 110 | + } |
| 111 | + |
| 112 | + void bindFramebuffer(const void* commandData, cb::RenderContext*) |
| 113 | + { |
| 114 | + const auto& cmd = *reinterpret_cast<const cmds::BindFramebuffer*>(commandData); |
| 115 | + glBindFramebuffer(cmd.target, cmd.fbo); |
| 116 | + } |
| 117 | + |
| 118 | + void unbindFramebuffer(const void* commandData, cb::RenderContext*) |
| 119 | + { |
| 120 | + const auto& cmd = *reinterpret_cast<const cmds::BindFramebuffer*>(commandData); |
| 121 | + glBindFramebuffer(cmd.target, 0); |
| 122 | + } |
| 123 | + |
| 124 | + void vertexBufferUpdate(const void* commandData, cb::RenderContext*) |
| 125 | + { |
| 126 | + const auto& cmd = *reinterpret_cast<const cmds::VertexBufferUpdate*>(commandData); |
| 127 | + glBindBuffer(cmd.target, cmd.buffer); |
| 128 | + glBufferSubData(cmd.target, cmd.offset, cmd.size, cmd.src); |
| 129 | + } |
| 130 | + |
| 131 | + uint8_t* lockBuffer(GLenum target, GLuint buffer, size_t offset, size_t length, bool isWriteOnly, BufferLockType lockType) |
| 132 | + { |
| 133 | + glBindBuffer(target, buffer); |
| 134 | + |
| 135 | + GLenum access = 0; |
| 136 | + if (isWriteOnly) |
| 137 | + { |
| 138 | + access = GL_MAP_WRITE_BIT; |
| 139 | + access |= GL_MAP_FLUSH_EXPLICIT_BIT; |
| 140 | + if (lockType == BufferLockType::eDiscard) |
| 141 | + { |
| 142 | + // Discard the buffer |
| 143 | + access |= GL_MAP_INVALIDATE_RANGE_BIT; |
| 144 | + } |
| 145 | + } |
| 146 | + else if (lockType == BufferLockType::eReadOnly) |
| 147 | + access = GL_MAP_READ_BIT; |
| 148 | + else |
| 149 | + access = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT; |
| 150 | + |
| 151 | + return reinterpret_cast<uint8_t*>(glMapBufferRange(target, (GLintptr)offset, (GLsizeiptr)length, access)); |
| 152 | + } |
| 153 | + |
| 154 | + void unlockBuffer(GLenum target, GLuint buffer, size_t lockStart, size_t lockSize, bool isWriteOnly) |
| 155 | + { |
| 156 | + glBindBuffer(target, buffer); |
| 157 | + // flush for write only buffer |
| 158 | + if (isWriteOnly) |
| 159 | + glFlushMappedBufferRange(target, lockStart, lockSize); |
| 160 | + |
| 161 | + GLboolean success = glUnmapBuffer(target); |
| 162 | + assert(success); |
| 163 | + } |
| 164 | + |
| 165 | + void vertexBufferCopy(const void* commandData, cb::RenderContext*) |
| 166 | + { |
| 167 | + const auto& cmd = *reinterpret_cast<const cmds::VertexBufferCopy*>(commandData); |
| 168 | + |
| 169 | +#ifdef GL_ARB_copy_buffer |
| 170 | + // faster way to copy |
| 171 | + glBindBuffer(GL_COPY_READ_BUFFER, cmd.srcBuffer); |
| 172 | + glBindBuffer(GL_COPY_WRITE_BUFFER, cmd.dstBuffer); |
| 173 | + glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, (GLintptr)cmd.srcOffset, (GLintptr)cmd.dstOffset, |
| 174 | + (GLintptr)cmd.size); |
| 175 | + |
| 176 | + glBindBuffer(GL_COPY_READ_BUFFER, 0); |
| 177 | + glBindBuffer(GL_COPY_WRITE_BUFFER, 0); |
| 178 | +#else |
| 179 | + const uint8_t* srcPtr = lockBuffer(cmd.target, cmd.srcBuffer, cmd.srcOffset, cmd.size, false, BufferLockType::eReadOnly); |
| 180 | + |
| 181 | + glBindBuffer(cmd.target, cmd.dstBuffer); |
| 182 | + glBufferSubData(cmd.target, cmd.dstBuffer, cmd.size, srcPtr); |
| 183 | + |
| 184 | + unlockBuffer(cmd.target, cmd.srcBuffer, cmd.srcOffset, cmd.size, false); |
| 185 | +#endif // #ifdef GL_ARB_copy_buffer |
| 186 | + } |
| 187 | + |
| 188 | + const cb::RenderContext::function_t DrawArrays::kDispatchFunction = &drawArrays; |
| 189 | + const cb::RenderContext::function_t DrawIndexed::kDispatchFunction = &drawIndexed; |
| 190 | + const cb::RenderContext::function_t DrawInstanced::kDispatchFunction = &drawInstanced; |
| 191 | + const cb::RenderContext::function_t ClearColor::kDispatchFunction = &clearColor; |
| 192 | + const cb::RenderContext::function_t DepthSetup::kDispatchFunction = &depthSetup; |
| 193 | + const cb::RenderContext::function_t BlendSetup::kDispatchFunction = &blendSetup; |
| 194 | + const cb::RenderContext::function_t BindShader::kDispatchFunction = &bindShader; |
| 195 | + const cb::RenderContext::function_t BindTexture::kDispatchFunction = &bindTexture; |
| 196 | + const cb::RenderContext::function_t BindUniformBuffer::kDispatchFunction = &bindUniformBuffer; |
| 197 | + const cb::RenderContext::function_t BindFramebuffer::kDispatchFunction = &bindFramebuffer; |
| 198 | + const cb::RenderContext::function_t UnbindFramebuffer::kDispatchFunction = &unbindFramebuffer; |
| 199 | + const cb::RenderContext::function_t VertexBufferUpdate::kDispatchFunction = &vertexBufferUpdate; |
| 200 | +} |
| 201 | + |
| 202 | +#ifndef USE_GLEW |
| 203 | +#undef GL_GLEXT_PROTOTYPES |
| 204 | +#endif |
0 commit comments