2026-03-22 03:03:14 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
|
|
#include <glad/glad.h>
|
|
|
|
|
|
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
|
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLSwapChain.h"
|
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLCommandList.h"
|
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLBuffer.h"
|
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLVertexArray.h"
|
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLShader.h"
|
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLPipelineState.h"
|
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLTexture.h"
|
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLSampler.h"
|
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLScreenshot.h"
|
|
|
|
|
#include "XCEngine/Debug/Logger.h"
|
|
|
|
|
#include "XCEngine/Debug/ConsoleLogSink.h"
|
2026-03-23 20:25:53 +08:00
|
|
|
#include "XCEngine/Debug/RenderDocCapture.h"
|
2026-03-24 16:14:05 +08:00
|
|
|
#include "XCEngine/Core/Containers/String.h"
|
2026-03-22 03:03:14 +08:00
|
|
|
|
|
|
|
|
#pragma comment(lib, "opengl32.lib")
|
|
|
|
|
|
|
|
|
|
using namespace XCEngine::RHI;
|
|
|
|
|
using namespace XCEngine::Debug;
|
|
|
|
|
using namespace XCEngine::Containers;
|
|
|
|
|
|
|
|
|
|
static const int gWidth = 1280;
|
|
|
|
|
static const int gHeight = 720;
|
|
|
|
|
|
|
|
|
|
void Log(const char* format, ...) {
|
|
|
|
|
char buffer[1024];
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
vsnprintf(buffer, sizeof(buffer), format, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
Logger::Get().Debug(LogCategory::Rendering, String(buffer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
|
|
|
|
switch (msg) {
|
|
|
|
|
case WM_CLOSE:
|
|
|
|
|
PostQuitMessage(0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return DefWindowProc(hwnd, msg, wParam, lParam);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
|
|
|
|
|
Logger::Get().Initialize();
|
|
|
|
|
Logger::Get().AddSink(std::make_unique<ConsoleLogSink>());
|
|
|
|
|
Logger::Get().SetMinimumLevel(LogLevel::Debug);
|
|
|
|
|
|
|
|
|
|
Log("[INFO] OpenGL Quad Test Starting");
|
|
|
|
|
|
|
|
|
|
WNDCLASSEXW wc = {};
|
|
|
|
|
wc.cbSize = sizeof(WNDCLASSEXW);
|
|
|
|
|
wc.style = CS_HREDRAW | CS_VREDRAW;
|
|
|
|
|
wc.lpfnWndProc = WindowProc;
|
|
|
|
|
wc.hInstance = hInstance;
|
|
|
|
|
wc.lpszClassName = L"XCEngine_OpenGL_Quad";
|
|
|
|
|
|
|
|
|
|
if (!RegisterClassExW(&wc)) {
|
|
|
|
|
Log("[ERROR] Failed to register window class");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RECT rect = { 0, 0, gWidth, gHeight };
|
|
|
|
|
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
|
|
|
|
|
|
|
|
|
|
HWND hwnd = CreateWindowExW(
|
|
|
|
|
0,
|
|
|
|
|
L"XCEngine_OpenGL_Quad",
|
|
|
|
|
L"OpenGL Quad Test",
|
|
|
|
|
WS_OVERLAPPEDWINDOW,
|
|
|
|
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
|
|
|
|
rect.right - rect.left, rect.bottom - rect.top,
|
|
|
|
|
NULL, NULL, hInstance, NULL
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!hwnd) {
|
|
|
|
|
Log("[ERROR] Failed to create window");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-23 20:25:53 +08:00
|
|
|
RenderDocCapture::Get().Initialize(nullptr, hwnd);
|
|
|
|
|
RenderDocCapture::Get().SetCaptureFilePath(".\\quad_frame30");
|
|
|
|
|
|
2026-03-22 03:03:14 +08:00
|
|
|
OpenGLDevice device;
|
2026-03-23 21:48:46 +08:00
|
|
|
RHIDeviceDesc desc = {};
|
|
|
|
|
desc.windowHandle = hwnd;
|
|
|
|
|
desc.width = gWidth;
|
|
|
|
|
desc.height = gHeight;
|
|
|
|
|
desc.appName = L"OpenGL_Quad_Test";
|
|
|
|
|
desc.enableDebugLayer = true;
|
|
|
|
|
if (!device.Initialize(desc)) {
|
2026-03-22 03:03:14 +08:00
|
|
|
Log("[ERROR] Failed to initialize OpenGL device");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-23 21:48:46 +08:00
|
|
|
RenderDocCapture::Get().SetDevice(device.GetGLContext());
|
2026-03-23 20:25:53 +08:00
|
|
|
|
2026-03-22 03:03:14 +08:00
|
|
|
ShowWindow(hwnd, nShowCmd);
|
|
|
|
|
UpdateWindow(hwnd);
|
|
|
|
|
|
|
|
|
|
Log("[INFO] OpenGL Device: %S", device.GetDeviceInfo().renderer.c_str());
|
|
|
|
|
Log("[INFO] OpenGL Version: %S", device.GetDeviceInfo().version.c_str());
|
|
|
|
|
|
|
|
|
|
OpenGLSwapChain swapChain;
|
2026-03-23 21:48:46 +08:00
|
|
|
swapChain.Initialize(&device, hwnd, gWidth, gHeight);
|
2026-03-22 03:03:14 +08:00
|
|
|
|
|
|
|
|
OpenGLCommandList commandList;
|
|
|
|
|
|
|
|
|
|
struct Vertex {
|
|
|
|
|
float pos[4];
|
|
|
|
|
float texcoord[2];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Vertex vertices[] = {
|
|
|
|
|
{ { -0.5f, -0.5f, 0.0f, 1.0f }, { 0.0f, 1.0f } },
|
|
|
|
|
{ { -0.5f, 0.5f, 0.0f, 1.0f }, { 0.0f, 0.0f } },
|
|
|
|
|
{ { 0.5f, -0.5f, 0.0f, 1.0f }, { 1.0f, 1.0f } },
|
|
|
|
|
{ { 0.5f, 0.5f, 0.0f, 1.0f }, { 1.0f, 0.0f } },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
OpenGLBuffer vertexBuffer;
|
|
|
|
|
if (!vertexBuffer.InitializeVertexBuffer(vertices, sizeof(vertices))) {
|
|
|
|
|
Log("[ERROR] Failed to initialize vertex buffer");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
vertexBuffer.SetStride(sizeof(Vertex));
|
|
|
|
|
|
|
|
|
|
OpenGLVertexArray vertexArray;
|
|
|
|
|
vertexArray.Initialize();
|
|
|
|
|
|
|
|
|
|
VertexAttribute posAttr = {};
|
|
|
|
|
posAttr.index = 0;
|
|
|
|
|
posAttr.count = 4;
|
2026-03-22 14:33:57 +08:00
|
|
|
posAttr.type = VertexAttributeType::Float;
|
|
|
|
|
posAttr.normalized = VertexAttributeNormalized::False;
|
2026-03-22 03:03:14 +08:00
|
|
|
posAttr.stride = sizeof(Vertex);
|
|
|
|
|
posAttr.offset = 0;
|
|
|
|
|
vertexArray.AddVertexBuffer(vertexBuffer.GetID(), posAttr);
|
|
|
|
|
|
|
|
|
|
VertexAttribute texAttr = {};
|
|
|
|
|
texAttr.index = 1;
|
|
|
|
|
texAttr.count = 2;
|
2026-03-22 14:33:57 +08:00
|
|
|
texAttr.type = VertexAttributeType::Float;
|
|
|
|
|
texAttr.normalized = VertexAttributeNormalized::False;
|
2026-03-22 03:03:14 +08:00
|
|
|
texAttr.stride = sizeof(Vertex);
|
|
|
|
|
texAttr.offset = sizeof(float) * 4;
|
|
|
|
|
vertexArray.AddVertexBuffer(vertexBuffer.GetID(), texAttr);
|
|
|
|
|
|
|
|
|
|
OpenGLShader shader;
|
|
|
|
|
if (!shader.CompileFromFile("Res/Shader/quad.vert", "Res/Shader/quad.frag")) {
|
|
|
|
|
Log("[ERROR] Failed to compile shaders");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
Log("[INFO] Shaders compiled successfully");
|
|
|
|
|
|
2026-03-24 19:47:22 +08:00
|
|
|
commandList.SetShader(&shader);
|
|
|
|
|
commandList.SetUniformInt("uTexture", 0);
|
2026-03-22 03:03:14 +08:00
|
|
|
|
|
|
|
|
OpenGLPipelineState pipelineState;
|
|
|
|
|
OpenGLRasterizerState rasterizerState;
|
|
|
|
|
rasterizerState.cullFaceEnable = false;
|
|
|
|
|
pipelineState.SetRasterizerState(rasterizerState);
|
|
|
|
|
|
|
|
|
|
OpenGLDepthStencilState depthStencilState;
|
|
|
|
|
depthStencilState.depthTestEnable = false;
|
|
|
|
|
depthStencilState.depthWriteEnable = false;
|
|
|
|
|
pipelineState.SetDepthStencilState(depthStencilState);
|
|
|
|
|
|
|
|
|
|
ViewportState viewportState = { 0.0f, 0.0f, (float)gWidth, (float)gHeight, 0.0f, 1.0f };
|
|
|
|
|
pipelineState.SetViewport(viewportState);
|
|
|
|
|
|
|
|
|
|
pipelineState.AttachShader(shader.GetID());
|
|
|
|
|
pipelineState.Apply();
|
|
|
|
|
|
|
|
|
|
OpenGLTexture texture;
|
|
|
|
|
if (!texture.LoadFromFile("Res/Image/earth.png", true)) {
|
|
|
|
|
Log("[ERROR] Failed to load texture");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
Log("[INFO] Texture loaded successfully");
|
|
|
|
|
|
|
|
|
|
OpenGLSampler sampler;
|
|
|
|
|
OpenGLSamplerDesc samplerDesc = {};
|
|
|
|
|
samplerDesc.minFilter = SamplerFilter::Linear;
|
|
|
|
|
samplerDesc.magFilter = SamplerFilter::Linear;
|
|
|
|
|
samplerDesc.wrapS = SamplerWrapMode::ClampToEdge;
|
|
|
|
|
samplerDesc.wrapT = SamplerWrapMode::ClampToEdge;
|
|
|
|
|
sampler.Initialize(samplerDesc);
|
|
|
|
|
|
|
|
|
|
MSG msg = {};
|
|
|
|
|
int frameCount = 0;
|
2026-03-24 00:04:22 +08:00
|
|
|
const int captureStartFrame = 29;
|
|
|
|
|
const int captureEndFrame = 30;
|
2026-03-22 03:03:14 +08:00
|
|
|
|
2026-03-24 00:04:22 +08:00
|
|
|
while (true) {
|
2026-03-22 03:03:14 +08:00
|
|
|
if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
|
|
|
if (msg.message == WM_QUIT) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
|
DispatchMessageW(&msg);
|
|
|
|
|
} else {
|
2026-03-23 21:48:46 +08:00
|
|
|
wglMakeCurrent(device.GetPresentationDC(), device.GetGLContext());
|
|
|
|
|
|
2026-03-22 03:03:14 +08:00
|
|
|
commandList.SetViewport(0, 0, gWidth, gHeight);
|
|
|
|
|
commandList.Clear(0.0f, 0.0f, 1.0f, 1.0f, 1);
|
|
|
|
|
|
|
|
|
|
pipelineState.Bind();
|
|
|
|
|
vertexArray.Bind();
|
|
|
|
|
|
|
|
|
|
texture.Bind(0);
|
|
|
|
|
sampler.Bind(0);
|
|
|
|
|
|
|
|
|
|
commandList.Draw(PrimitiveType::TriangleStrip, 4, 0);
|
|
|
|
|
|
|
|
|
|
swapChain.Present(0, 0);
|
|
|
|
|
frameCount++;
|
2026-03-23 20:25:53 +08:00
|
|
|
|
2026-03-24 00:04:22 +08:00
|
|
|
if (frameCount >= captureEndFrame) {
|
2026-03-23 21:48:46 +08:00
|
|
|
RenderDocCapture::Get().EndCapture();
|
|
|
|
|
Log("[INFO] RenderDoc capture ended at frame %d", frameCount);
|
|
|
|
|
break;
|
2026-03-23 20:25:53 +08:00
|
|
|
}
|
2026-03-24 00:04:22 +08:00
|
|
|
|
|
|
|
|
if (frameCount == captureStartFrame) {
|
|
|
|
|
RenderDocCapture::Get().BeginCapture("OpenGL_Quad_Test");
|
|
|
|
|
Log("[INFO] RenderDoc capture started at frame %d", frameCount);
|
|
|
|
|
}
|
2026-03-22 03:03:14 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-23 21:48:46 +08:00
|
|
|
Log("[INFO] Capture complete - taking screenshot!");
|
2026-03-22 03:03:14 +08:00
|
|
|
OpenGLScreenshot::Capture(device, swapChain, "quad.ppm");
|
|
|
|
|
|
|
|
|
|
sampler.Shutdown();
|
|
|
|
|
texture.Shutdown();
|
|
|
|
|
vertexArray.Shutdown();
|
|
|
|
|
vertexBuffer.Shutdown();
|
|
|
|
|
shader.Shutdown();
|
|
|
|
|
pipelineState.Shutdown();
|
|
|
|
|
|
|
|
|
|
swapChain.Shutdown();
|
|
|
|
|
device.Shutdown();
|
2026-03-23 20:25:53 +08:00
|
|
|
|
|
|
|
|
RenderDocCapture::Get().Shutdown();
|
2026-03-22 03:03:14 +08:00
|
|
|
Logger::Get().Shutdown();
|
|
|
|
|
|
|
|
|
|
Log("[INFO] OpenGL Quad Test Finished");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|