Files
XCEngine/engine/src/RHI/OpenGL/OpenGLCommandList.cpp

515 lines
18 KiB
C++

#define GLFW_INCLUDE_NONE
#include "XCEngine/RHI/OpenGL/OpenGLCommandList.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
namespace XCEngine {
namespace RHI {
static unsigned int ToGLPrimitiveType(PrimitiveType type) {
switch (type) {
case PrimitiveType::Points: return GL_POINTS;
case PrimitiveType::Lines: return GL_LINES;
case PrimitiveType::LineStrip: return GL_LINE_STRIP;
case PrimitiveType::Triangles: return GL_TRIANGLES;
case PrimitiveType::TriangleStrip: return GL_TRIANGLE_STRIP;
case PrimitiveType::TriangleFan: return GL_TRIANGLE_FAN;
case PrimitiveType::LineListAdj: return GL_LINES_ADJACENCY;
case PrimitiveType::TriangleListAdj: return GL_TRIANGLES_ADJACENCY;
case PrimitiveType::Patch: return GL_PATCHES;
default: return GL_TRIANGLES;
}
}
OpenGLCommandList::OpenGLCommandList()
: m_primitiveType(GL_TRIANGLES)
, m_currentVAO(0)
, m_currentProgram(0) {
}
OpenGLCommandList::~OpenGLCommandList() {
}
void OpenGLCommandList::Clear(float r, float g, float b, float a, unsigned int buffers) {
glClearColor(r, g, b, a);
unsigned int glBuffers = 0;
if (buffers & 1) glBuffers |= GL_COLOR_BUFFER_BIT;
if (buffers & 2) glBuffers |= GL_DEPTH_BUFFER_BIT;
if (buffers & 4) glBuffers |= GL_STENCIL_BUFFER_BIT;
glClear(glBuffers);
}
void OpenGLCommandList::ClearColor(float r, float g, float b, float a) {
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT);
}
void OpenGLCommandList::ClearDepth(float depth) {
glClearDepth(depth);
glClear(GL_DEPTH_BUFFER_BIT);
}
void OpenGLCommandList::ClearStencil(int stencil) {
glClearStencil(stencil);
glClear(GL_STENCIL_BUFFER_BIT);
}
void OpenGLCommandList::ClearDepthStencil(float depth, int stencil) {
glClearDepth(depth);
glClearStencil(stencil);
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
void OpenGLCommandList::SetPipelineState(void* pipelineState) {
}
void OpenGLCommandList::SetVertexBuffer(unsigned int buffer, size_t offset, size_t stride) {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glVertexAttribPointer(0, stride / sizeof(float), GL_FLOAT, GL_FALSE, stride, (void*)offset);
glEnableVertexAttribArray(0);
}
void OpenGLCommandList::SetVertexBuffers(unsigned int startSlot, unsigned int count, const unsigned int* buffers, const size_t* offsets, const size_t* strides) {
for (unsigned int i = 0; i < count; i++) {
glBindBuffer(GL_ARRAY_BUFFER, buffers[i]);
glEnableVertexAttribArray(startSlot + i);
glVertexAttribPointer(startSlot + i, strides[i] / sizeof(float), GL_FLOAT, GL_FALSE, strides[i], (void*)offsets[i]);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void OpenGLCommandList::SetIndexBuffer(unsigned int buffer, unsigned int type) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
}
void OpenGLCommandList::SetIndexBuffer(unsigned int buffer, unsigned int type, size_t offset) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
(void)offset;
}
void OpenGLCommandList::BindVertexArray(unsigned int vao) {
m_currentVAO = vao;
glBindVertexArray(vao);
}
void OpenGLCommandList::BindVertexArray(unsigned int vao, unsigned int indexBuffer, unsigned int indexType) {
m_currentVAO = vao;
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
(void)indexType;
}
void OpenGLCommandList::UseShader(unsigned int program) {
m_currentProgram = program;
glUseProgram(program);
}
void OpenGLCommandList::SetViewport(int x, int y, int width, int height) {
glViewport(x, y, width, height);
}
void OpenGLCommandList::SetViewport(float x, float y, float width, float height, float minDepth, float maxDepth) {
glViewportIndexedf(0, x, y, width, height);
glDepthRange(minDepth, maxDepth);
}
void OpenGLCommandList::SetViewports(unsigned int count, const float* viewports) {
for (unsigned int i = 0; i < count; i++) {
glViewportIndexedf(i, viewports[i * 6 + 0], viewports[i * 6 + 1], viewports[i * 6 + 2], viewports[i * 6 + 3]);
glDepthRangeIndexed(i, viewports[i * 6 + 4], viewports[i * 6 + 5]);
}
}
void OpenGLCommandList::SetScissor(int x, int y, int width, int height) {
glScissor(x, y, width, height);
}
void OpenGLCommandList::SetScissorRects(unsigned int count, const int* rects) {
for (unsigned int i = 0; i < count; i++) {
glScissorIndexed(i, rects[i * 4 + 0], rects[i * 4 + 1], rects[i * 4 + 2], rects[i * 4 + 3]);
}
}
void OpenGLCommandList::EnableScissorTest(bool enable) {
if (enable) glEnable(GL_SCISSOR_TEST);
else glDisable(GL_SCISSOR_TEST);
}
void OpenGLCommandList::EnableDepthTest(bool enable) {
if (enable) glEnable(GL_DEPTH_TEST);
else glDisable(GL_DEPTH_TEST);
}
void OpenGLCommandList::EnableDepthWrite(bool enable) {
glDepthMask(enable ? GL_TRUE : GL_FALSE);
}
void OpenGLCommandList::SetDepthFunc(unsigned int func) {
glDepthFunc(func);
}
void OpenGLCommandList::EnableStencilTest(bool enable) {
if (enable) glEnable(GL_STENCIL_TEST);
else glDisable(GL_STENCIL_TEST);
}
void OpenGLCommandList::SetStencilFunc(unsigned int func, int ref, unsigned int mask) {
glStencilFunc(func, ref, mask);
}
void OpenGLCommandList::SetStencilOp(unsigned int fail, unsigned int zfail, unsigned int zpass) {
glStencilOp(fail, zfail, zpass);
}
void OpenGLCommandList::EnableBlending(bool enable) {
if (enable) glEnable(GL_BLEND);
else glDisable(GL_BLEND);
}
void OpenGLCommandList::SetBlendFunc(unsigned int src, unsigned int dst) {
glBlendFunc(src, dst);
}
void OpenGLCommandList::SetBlendFuncSeparate(unsigned int srcRGB, unsigned int dstRGB, unsigned int srcAlpha, unsigned int dstAlpha) {
glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
}
void OpenGLCommandList::SetBlendEquation(unsigned int mode) {
glBlendEquation(mode);
}
void OpenGLCommandList::SetBlendColor(float r, float g, float b, float a) {
glBlendColor(r, g, b, a);
}
void OpenGLCommandList::EnableCulling(bool enable) {
if (enable) glEnable(GL_CULL_FACE);
else glDisable(GL_CULL_FACE);
}
void OpenGLCommandList::SetCullFace(unsigned int face) {
glCullFace(face);
}
void OpenGLCommandList::SetFrontFace(unsigned int face) {
glFrontFace(face);
}
void OpenGLCommandList::SetPolygonMode(unsigned int mode) {
glPolygonMode(GL_FRONT_AND_BACK, mode);
}
void OpenGLCommandList::SetPolygonOffset(float factor, float units) {
glPolygonOffset(factor, units);
}
void OpenGLCommandList::SetPrimitiveType(PrimitiveType type) {
m_primitiveType = ToGLPrimitiveType(type);
}
void OpenGLCommandList::Draw(PrimitiveType type, unsigned int vertexCount, unsigned int startVertex) {
m_primitiveType = ToGLPrimitiveType(type);
glDrawArrays(m_primitiveType, startVertex, vertexCount);
}
void OpenGLCommandList::DrawInstanced(PrimitiveType type, unsigned int vertexCount, unsigned int instanceCount, unsigned int startVertex, unsigned int startInstance) {
m_primitiveType = ToGLPrimitiveType(type);
glDrawArraysInstanced(m_primitiveType, startVertex, vertexCount, instanceCount);
(void)startInstance;
}
void OpenGLCommandList::DrawIndexed(PrimitiveType type, unsigned int indexCount, unsigned int startIndex, int baseVertex) {
m_primitiveType = ToGLPrimitiveType(type);
glDrawElements(m_primitiveType, indexCount, GL_UNSIGNED_INT, (void*)(startIndex * sizeof(unsigned int)));
(void)baseVertex;
}
void OpenGLCommandList::DrawIndexedInstanced(PrimitiveType type, unsigned int indexCount, unsigned int instanceCount, unsigned int startIndex, int baseVertex, unsigned int startInstance) {
m_primitiveType = ToGLPrimitiveType(type);
glDrawElementsInstanced(m_primitiveType, indexCount, GL_UNSIGNED_INT, (void*)(startIndex * sizeof(unsigned int)), instanceCount);
(void)baseVertex;
(void)startInstance;
}
void OpenGLCommandList::DrawIndirect(PrimitiveType type, unsigned int buffer, size_t offset, unsigned int drawCount, unsigned int stride) {
m_primitiveType = ToGLPrimitiveType(type);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer);
glDrawArraysIndirect(m_primitiveType, (void*)offset);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
(void)drawCount;
(void)stride;
}
void OpenGLCommandList::DrawIndexedIndirect(PrimitiveType type, unsigned int buffer, size_t offset, unsigned int drawCount, unsigned int stride) {
m_primitiveType = ToGLPrimitiveType(type);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer);
glDrawElementsIndirect(m_primitiveType, GL_UNSIGNED_INT, (void*)offset);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
(void)drawCount;
(void)stride;
}
void OpenGLCommandList::MultiDrawArrays(PrimitiveType type, const int* first, const int* count, unsigned int drawCount) {
m_primitiveType = ToGLPrimitiveType(type);
glMultiDrawArrays(m_primitiveType, first, count, drawCount);
}
void OpenGLCommandList::MultiDrawElements(PrimitiveType type, const int* count, unsigned int type_, const void* const* indices, unsigned int drawCount) {
m_primitiveType = ToGLPrimitiveType(type);
glMultiDrawElements(m_primitiveType, count, type_, indices, drawCount);
}
void OpenGLCommandList::Dispatch(unsigned int x, unsigned int y, unsigned int z) {
glDispatchCompute(x, y, z);
}
void OpenGLCommandList::DispatchIndirect(unsigned int buffer, size_t offset) {
glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, buffer);
glDispatchComputeIndirect((GLintptr)offset);
glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0);
}
void OpenGLCommandList::DispatchCompute(unsigned int x, unsigned int y, unsigned int z, unsigned int groupX, unsigned int groupY, unsigned int groupZ) {
glDispatchCompute(groupX, groupY, groupZ);
(void)x;
(void)y;
(void)z;
}
void OpenGLCommandList::MemoryBarrier(unsigned int barriers) {
glMemoryBarrier(barriers);
}
void OpenGLCommandList::TextureBarrier() {
glTextureBarrier();
}
void OpenGLCommandList::BindTexture(unsigned int target, unsigned int unit, unsigned int texture) {
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(target, texture);
}
void OpenGLCommandList::BindTextures(unsigned int first, unsigned int count, const unsigned int* textures) {
glBindTextures(first, count, textures);
}
void OpenGLCommandList::BindSampler(unsigned int unit, unsigned int sampler) {
glBindSampler(unit, sampler);
}
void OpenGLCommandList::BindSamplers(unsigned int first, unsigned int count, const unsigned int* samplers) {
glBindSamplers(first, count, samplers);
}
void OpenGLCommandList::BindImageTexture(unsigned int unit, unsigned int texture, int level, bool layered, int layer, unsigned int access, unsigned int format) {
glBindImageTexture(unit, texture, level, layered ? GL_TRUE : GL_FALSE, layer, access, format);
}
void OpenGLCommandList::BindBufferBase(unsigned int target, unsigned int index, unsigned int buffer) {
glBindBufferBase(target, index, buffer);
}
void OpenGLCommandList::BindBufferRange(unsigned int target, unsigned int index, unsigned int buffer, size_t offset, size_t size) {
glBindBufferRange(target, index, buffer, offset, size);
}
void OpenGLCommandList::Enable(unsigned int cap) {
glEnable(cap);
}
void OpenGLCommandList::Disable(unsigned int cap) {
glDisable(cap);
}
void OpenGLCommandList::Enablei(unsigned int cap, unsigned int index) {
glEnablei(cap, index);
}
void OpenGLCommandList::Disablei(unsigned int cap, unsigned int index) {
glDisablei(cap, index);
}
void OpenGLCommandList::SetUniform1i(int location, int v) {
glUniform1i(location, v);
}
void OpenGLCommandList::SetUniform1f(int location, float v) {
glUniform1f(location, v);
}
void OpenGLCommandList::SetUniform2f(int location, float x, float y) {
glUniform2f(location, x, y);
}
void OpenGLCommandList::SetUniform3f(int location, float x, float y, float z) {
glUniform3f(location, x, y, z);
}
void OpenGLCommandList::SetUniform4f(int location, float x, float y, float z, float w) {
glUniform4f(location, x, y, z, w);
}
void OpenGLCommandList::SetUniform1fv(int location, int count, const float* v) {
glUniform1fv(location, count, v);
}
void OpenGLCommandList::SetUniform2fv(int location, int count, const float* v) {
glUniform2fv(location, count, v);
}
void OpenGLCommandList::SetUniform3fv(int location, int count, const float* v) {
glUniform3fv(location, count, v);
}
void OpenGLCommandList::SetUniform4fv(int location, int count, const float* v) {
glUniform4fv(location, count, v);
}
void OpenGLCommandList::SetUniformMatrix4fv(int location, int count, bool transpose, const float* v) {
glUniformMatrix4fv(location, count, transpose ? GL_TRUE : GL_FALSE, v);
}
void OpenGLCommandList::UseProgram(unsigned int program) {
glUseProgram(program);
}
void OpenGLCommandList::BindFragDataLocation(unsigned int program, unsigned int colorNumber, const char* name) {
glBindFragDataLocation(program, colorNumber, name);
}
void OpenGLCommandList::BindFragDataLocationIndexed(unsigned int program, unsigned int colorNumber, unsigned int index, const char* name) {
glBindFragDataLocationIndexed(program, colorNumber, index, name);
}
void OpenGLCommandList::BeginQuery(unsigned int target, unsigned int id) {
glBeginQuery(target, id);
}
void OpenGLCommandList::EndQuery(unsigned int target) {
glEndQuery(target);
}
void OpenGLCommandList::GetQueryObjectiv(unsigned int id, unsigned int pname, int* params) {
glGetQueryObjectiv(id, pname, params);
}
void OpenGLCommandList::GetQueryObjectuiv(unsigned int id, unsigned int pname, unsigned int* params) {
glGetQueryObjectuiv(id, pname, params);
}
void OpenGLCommandList::ReadPixels(int x, int y, int width, int height, unsigned int format, unsigned int type, void* data) {
glReadPixels(x, y, width, height, format, type, data);
}
void OpenGLCommandList::BlitFramebuffer(int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, int dstY1, unsigned int mask, unsigned int filter) {
glBlitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
}
void OpenGLCommandList::CopyImageSubData(unsigned int srcName, unsigned int srcTarget, int srcLevel, int srcX, int srcY, int srcZ, unsigned int dstName, unsigned int dstTarget, int dstLevel, int dstX, int dstY, int dstZ, int width, int height, int depth) {
glCopyImageSubData(srcName, srcTarget, srcLevel, srcX, srcY, srcZ, dstName, dstTarget, dstLevel, dstX, dstY, dstZ, width, height, depth);
}
void OpenGLCommandList::InvalidateFramebuffer(unsigned int target, unsigned int count, const unsigned int* attachments) {
glInvalidateFramebuffer(target, count, attachments);
}
void OpenGLCommandList::InvalidateSubFramebuffer(unsigned int target, unsigned int count, const unsigned int* attachments, int x, int y, int width, int height) {
glInvalidateSubFramebuffer(target, count, attachments, x, y, width, height);
}
void OpenGLCommandList::PushDebugGroup(unsigned int source, unsigned int id, int length, const char* message) {
glPushDebugGroup(source, id, length, message);
}
void OpenGLCommandList::PopDebugGroup() {
glPopDebugGroup();
}
void OpenGLCommandList::Shutdown() {
}
void OpenGLCommandList::Reset() {
}
void OpenGLCommandList::Close() {
}
void OpenGLCommandList::TransitionBarrier(void* resource, ResourceStates stateBefore, ResourceStates stateAfter) {
}
void OpenGLCommandList::SetViewport(const Viewport& viewport) {
glViewport(static_cast<int>(viewport.topLeftX), static_cast<int>(viewport.topLeftY),
static_cast<int>(viewport.width), static_cast<int>(viewport.height));
}
void OpenGLCommandList::SetViewports(uint32_t count, const Viewport* viewports) {
std::vector<float> viewportsGL(count * 6);
for (uint32_t i = 0; i < count; ++i) {
viewportsGL[i * 6 + 0] = viewports[i].topLeftX;
viewportsGL[i * 6 + 1] = viewports[i].topLeftY;
viewportsGL[i * 6 + 2] = viewports[i].width;
viewportsGL[i * 6 + 3] = viewports[i].height;
viewportsGL[i * 6 + 4] = viewports[i].minDepth;
viewportsGL[i * 6 + 5] = viewports[i].maxDepth;
}
glViewportArrayv(0, count, viewportsGL.data());
}
void OpenGLCommandList::SetScissorRect(const Rect& rect) {
glScissor(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
}
void OpenGLCommandList::SetScissorRects(uint32_t count, const Rect* rects) {
std::vector<int> scissorGL(count * 4);
for (uint32_t i = 0; i < count; ++i) {
scissorGL[i * 4 + 0] = rects[i].left;
scissorGL[i * 4 + 1] = rects[i].top;
scissorGL[i * 4 + 2] = rects[i].right - rects[i].left;
scissorGL[i * 4 + 3] = rects[i].bottom - rects[i].top;
}
glScissorArrayv(0, count, scissorGL.data());
}
void OpenGLCommandList::SetRenderTargets(uint32_t count, void** renderTargets, void* depthStencil) {
}
void OpenGLCommandList::ClearRenderTarget(void* renderTarget, const float color[4]) {
glClearColor(color[0], color[1], color[2], color[3]);
glClear(GL_COLOR_BUFFER_BIT);
}
void OpenGLCommandList::ClearDepthStencil(void* depthStencil, float depth, uint8_t stencil) {
glClearDepth(depth);
glClear(GL_DEPTH_BUFFER_BIT);
}
void OpenGLCommandList::SetPipelineState(void* pipelineState) {
if (pipelineState) {
UseShader(reinterpret_cast<uintptr_t>(pipelineState));
}
}
void OpenGLCommandList::SetVertexBuffer(uint32_t slot, void* buffer, uint64_t offset, uint32_t stride) {
}
void OpenGLCommandList::SetVertexBuffers(uint32_t startSlot, uint32_t count, const uint64_t* buffers, const uint64_t* offsets, const uint32_t* strides) {
}
void OpenGLCommandList::SetIndexBuffer(void* buffer, uint64_t offset, Format format) {
}
void OpenGLCommandList::CopyResource(void* dst, void* src) {
}
void OpenGLCommandList::Draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t startVertex, uint32_t startInstance) {
glDrawArraysInstanced(GL_TRIANGLES, static_cast<GLint>(startVertex), static_cast<GLsizei>(vertexCount), static_cast<GLsizei>(instanceCount));
}
void OpenGLCommandList::DrawIndexed(uint32_t indexCount, uint32_t instanceCount, uint32_t startIndex, int32_t baseVertex, uint32_t startInstance) {
glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(indexCount), GL_UNSIGNED_INT,
reinterpret_cast<const void*>(startIndex * sizeof(GLuint)), static_cast<GLsizei>(instanceCount));
}
} // namespace RHI
} // namespace XCEngine