Add OpenGLCommandList

This commit is contained in:
2026-03-16 18:35:02 +08:00
parent 0be91748c2
commit fce3d2421c
3 changed files with 125 additions and 0 deletions

View File

@@ -132,12 +132,14 @@ add_library(XCEngine STATIC
include/XCEngine/RHI/OpenGL/OpenGLVertexArray.h
include/XCEngine/RHI/OpenGL/OpenGLTexture.h
include/XCEngine/RHI/OpenGL/OpenGLPipelineState.h
include/XCEngine/RHI/OpenGL/OpenGLCommandList.h
src/RHI/OpenGL/OpenGLDevice.cpp
src/RHI/OpenGL/OpenGLShader.cpp
src/RHI/OpenGL/OpenGLBuffer.cpp
src/RHI/OpenGL/OpenGLVertexArray.cpp
src/RHI/OpenGL/OpenGLTexture.cpp
src/RHI/OpenGL/OpenGLPipelineState.cpp
src/RHI/OpenGL/OpenGLCommandList.cpp
)
target_include_directories(XCEngine PUBLIC

View File

@@ -0,0 +1,49 @@
#pragma once
#include <GLFW/glfw3.h>
#include <vector>
namespace XCEngine {
namespace RHI {
class OpenGLBuffer;
class OpenGLVertexArray;
class OpenGLShader;
class OpenGLTexture;
enum class PrimitiveType {
Points,
Lines,
LineStrip,
Triangles,
TriangleStrip,
TriangleFan
};
class OpenGLCommandList {
public:
OpenGLCommandList();
~OpenGLCommandList();
void Clear(float r, float g, float b, float a, unsigned int buffers);
void SetPipelineState(void* pipelineState);
void SetVertexBuffer(unsigned int buffer, size_t offset, size_t stride);
void SetIndexBuffer(unsigned int buffer, unsigned int type);
void Draw(PrimitiveType type, unsigned int vertexCount, unsigned int startVertex);
void DrawIndexed(PrimitiveType type, unsigned int indexCount, unsigned int startIndex, int baseVertex);
void BindVertexArray(unsigned int vao);
void UseShader(unsigned int program);
void SetViewport(int x, int y, int width, int height);
private:
unsigned int m_primitiveType;
unsigned int m_currentVAO;
unsigned int m_currentProgram;
};
} // namespace RHI
} // namespace XCEngine

View File

@@ -0,0 +1,74 @@
#define GLFW_INCLUDE_NONE
#include "XCEngine/RHI/OpenGL/OpenGLCommandList.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
namespace XCEngine {
namespace RHI {
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 & 0x1) glBuffers |= GL_COLOR_BUFFER_BIT;
if (buffers & 0x2) glBuffers |= GL_DEPTH_BUFFER_BIT;
if (buffers & 0x4) glBuffers |= GL_STENCIL_BUFFER_BIT;
glClear(glBuffers);
}
void OpenGLCommandList::SetPipelineState(void* pipelineState) {
}
void OpenGLCommandList::SetVertexBuffer(unsigned int buffer, size_t offset, size_t stride) {
}
void OpenGLCommandList::SetIndexBuffer(unsigned int buffer, unsigned int type) {
}
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;
default: return GL_TRIANGLES;
}
}
void OpenGLCommandList::Draw(PrimitiveType type, unsigned int vertexCount, unsigned int startVertex) {
m_primitiveType = ToGLPrimitiveType(type);
glDrawArrays(m_primitiveType, startVertex, vertexCount);
}
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 OpenGLCommandList::BindVertexArray(unsigned int vao) {
m_currentVAO = vao;
glBindVertexArray(vao);
}
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);
}
} // namespace RHI
} // namespace XCEngine