This commit is contained in:
Asuro
2025-02-08 18:54:09 +01:00
parent 149de529e6
commit aa18ffd1a5
8 changed files with 285 additions and 339 deletions

View File

@@ -1,188 +1,161 @@
#include "Rendering.h"
#include "../Log.h"
#include "bgfx/defines.h"
#include "bgfx/platform.h"
#include "bx/timer.h"
#include <bx/file.h>
#include <bgfx/bgfx.h>
#include <bx/filepath.h>
#include <bx/readerwriter.h>
namespace Game
{
static VertexData CubeVertices[] =
{
{-1.0f, 1.0f, 1.0f, 0xff000000 },
{ 1.0f, 1.0f, 1.0f, 0xff0000ff },
{-1.0f, -1.0f, 1.0f, 0xff00ff00 },
{ 1.0f, -1.0f, 1.0f, 0xff00ffff },
{-1.0f, 1.0f, -1.0f, 0xffff0000 },
{ 1.0f, 1.0f, -1.0f, 0xffff00ff },
{-1.0f, -1.0f, -1.0f, 0xffffff00 },
{ 1.0f, -1.0f, -1.0f, 0xffffffff },
};
const uint16_t CubeIndices[] =
{
0, 1, 2, // 0
1, 3, 2,
4, 6, 5, // 2
5, 6, 7,
0, 2, 4, // 4
4, 2, 6,
1, 5, 3, // 6
5, 7, 3,
0, 4, 1, // 8
4, 5, 1,
2, 3, 6, // 10
6, 3, 7,
};
namespace
{
static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const bx::FilePath& _filePath)
{
if (bx::open(_reader, _filePath) )
{
uint32_t size = (uint32_t)bx::getSize(_reader);
const bgfx::Memory* mem = bgfx::alloc(size+1);
bx::read(_reader, mem->data, size, bx::ErrorAssert{});
bx::close(_reader);
mem->data[mem->size-1] = '\0';
return mem;
}
struct PosColorVertex
{
float x;
float y;
float z;
uint32_t abgr;
};
Log("Failed to load %s.", _filePath.getCPtr() );
return NULL;
}
bgfx::ShaderHandle LoadShader(const char* name)
{
bx::FilePath filePath{"game/compiled-shaders/"};
switch (bgfx::getRendererType())
{
case bgfx::RendererType::Noop:
case bgfx::RendererType::Direct3D11:
case bgfx::RendererType::Direct3D12: filePath.join("dx11"); break;
case bgfx::RendererType::Agc:
case bgfx::RendererType::Gnm: filePath.join("pssl"); break;
case bgfx::RendererType::Metal: filePath.join("metal"); break;
case bgfx::RendererType::Nvn: filePath.join("nvn"); break;
case bgfx::RendererType::OpenGL: filePath.join("glsl"); break;
case bgfx::RendererType::OpenGLES: filePath.join("essl"); break;
case bgfx::RendererType::Vulkan: filePath.join("spirv"); break;
static PosColorVertex cubeVertices[] =
{
{-1.0f, 1.0f, 1.0f, 0xff000000 },
{ 1.0f, 1.0f, 1.0f, 0xff0000ff },
{-1.0f, -1.0f, 1.0f, 0xff00ff00 },
{ 1.0f, -1.0f, 1.0f, 0xff00ffff },
{-1.0f, 1.0f, -1.0f, 0xffff0000 },
{ 1.0f, 1.0f, -1.0f, 0xffff00ff },
{-1.0f, -1.0f, -1.0f, 0xffffff00 },
{ 1.0f, -1.0f, -1.0f, 0xffffffff },
};
case bgfx::RendererType::Count:
BX_ASSERT(false, "You should not be here!");
break;
}
static const uint16_t cubeTriList[] =
{
0, 1, 2,
1, 3, 2,
4, 6, 5,
5, 6, 7,
0, 2, 4,
4, 2, 6,
1, 5, 3,
5, 7, 3,
0, 4, 1,
4, 5, 1,
2, 3, 6,
6, 3, 7,
};
char fileName[512]{0};
bx::strCopy(fileName, sizeof(fileName), name);
bx::strCat(fileName, sizeof(fileName), ".bin");
filePath.join(fileName);
namespace
{
bx::FileReader fileReader;
const bgfx::Memory* mem = loadMem(&fileReader, filePath.getCPtr());
if (mem == nullptr) return {};
bgfx::ShaderHandle result = bgfx::createShader(mem);
return result;
}
}
void GameRendering::Setup(void* window)
{
bgfx::Init init;
init.type = bgfx::RendererType::OpenGL;
//init.debug = true;
//init.callback = &Callback;
init.platformData.nwh = (void*)window;
init.platformData.ndt = nullptr;
init.platformData.type = bgfx::NativeWindowHandleType::Default;
init.resolution.width = State.WindowWidth;
init.resolution.height = State.WindowHeight;
init.resolution.reset = BGFX_RESET_VSYNC;
if (!bgfx::init(init))
{
Log("BGFX setup failed!");
}
else
{
Log("BGFX setup succeded!");
}
bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
VertLayout.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const bx::FilePath& _filePath)
{
if (bx::open(_reader, _filePath))
{
uint32_t size = (uint32_t)bx::getSize(_reader);
const bgfx::Memory* mem = bgfx::alloc(size + 1);
bx::read(_reader, mem->data, size, bx::ErrorAssert{});
bx::close(_reader);
mem->data[mem->size - 1] = '\0';
return mem;
}
VertexBuffer = bgfx::createVertexBuffer(bgfx::makeRef(CubeVertices, sizeof(CubeVertices)), VertLayout);
IndexBuffer = bgfx::createIndexBuffer(bgfx::makeRef(CubeIndices, sizeof(CubeIndices)));
bgfx::ShaderHandle vertexShader = LoadShader("vert");
bgfx::ShaderHandle fragmentShader = LoadShader("frag");
bgfx::createProgram(vertexShader, fragmentShader, true);
State.StartTime = bx::getHPCounter();
}
void GameRendering::Update()
{
bgfx::setViewRect(0, 0, 0, State.WindowWidth, State.WindowHeight);
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
// Set view and projection matrix for view 0.
{
float view[16];
bx::mtxLookAt(view, eye, at);
float proj[16];
bx::mtxProj(proj, 60.0f, float(State.WindowWidth)/float(State.WindowHeight), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
bgfx::setViewTransform(0, view, proj);
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, State.WindowWidth, State.WindowHeight);
}
// Set render states.
uint64_t state = 0
| BGFX_STATE_WRITE_RGB
| BGFX_STATE_WRITE_A
| BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CW
| BGFX_STATE_MSAA
;
float time = (bx::getHPCounter() - State.StartTime) / (float)bx::getHPFrequency();
for (int32_t i = 0; i < 11 * 11; ++i)
{
uint32_t xx = i % 11;
uint32_t yy = i / 11;
float mtx[16];
bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
mtx[12] = -15.0f + float(xx)*3.0f;
mtx[13] = -15.0f + float(yy)*3.0f;
mtx[14] = 0.0f;
// Set model matrix for rendering.
bgfx::setTransform(mtx);
// Set vertex and index buffer.
bgfx::setVertexBuffer(0, VertexBuffer);
bgfx::setIndexBuffer(IndexBuffer);
bgfx::setState(state);
bgfx::submit(0, Shader);
Log("Failed to load %s.", _filePath.getCPtr());
return NULL;
}
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %f", time);
bgfx::frame();
}
void GameRendering::Shutdown()
{
bgfx::shutdown();
}
bgfx::ShaderHandle loadShader(const char* FILENAME)
{
const char* shaderPath = "???";
switch (bgfx::getRendererType()) {
case bgfx::RendererType::Noop: break;
case bgfx::RendererType::Direct3D11:
case bgfx::RendererType::Direct3D12: shaderPath = "game/compiled-shaders/dx11/"; break;
case bgfx::RendererType::Gnm: shaderPath = "game/compiled-shaders/pssl/"; break;
case bgfx::RendererType::Metal: shaderPath = "game/compiled-shaders/metal/"; break;
case bgfx::RendererType::OpenGL: shaderPath = "game/compiled-shaders/glsl/"; break;
case bgfx::RendererType::OpenGLES: shaderPath = "game/compiled-shaders/essl/"; break;
case bgfx::RendererType::Vulkan: shaderPath = "game/compiled-shaders/spirv/"; break;
}
char buffer[512]{ 0 };
bx::strCopy(buffer, sizeof(buffer), shaderPath);
bx::strCat(buffer, sizeof(buffer), FILENAME);
bx::strCat(buffer, sizeof(buffer), ".bin");
FILE* file = fopen(buffer, "rb");
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
const bgfx::Memory* mem = bgfx::alloc(fileSize + 1);
fread(mem->data, 1, fileSize, file);
mem->data[mem->size - 1] = '\0';
fclose(file);
return bgfx::createShader(mem);
}
}
void GameRendering::Setup(void* window)
{
bgfx::renderFrame();
bgfx::Init init;
init.type = bgfx::RendererType::Direct3D12;
init.debug = true;
init.callback = &Callback;
init.platformData.nwh = window;
init.platformData.ndt = nullptr;
init.platformData.type = bgfx::NativeWindowHandleType::Default;
init.resolution.width = State.WindowWidth;
init.resolution.height = State.WindowHeight;
init.resolution.reset = BGFX_RESET_VSYNC;
if (!bgfx::init(init))
{
Log("BGFX setup failed!");
}
else
{
Log("BGFX setup succeded!");
}
bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
VertLayout.begin()
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
.end();
VertexBuffer = bgfx::createVertexBuffer(bgfx::makeRef(cubeVertices, sizeof(cubeVertices)), VertLayout);
IndexBuffer = bgfx::createIndexBuffer(bgfx::makeRef(cubeTriList, sizeof(cubeTriList)));
bgfx::ShaderHandle vertexShader = loadShader("vert");
bgfx::ShaderHandle fragmentShader = loadShader("frag");
Shader = bgfx::createProgram(vertexShader, fragmentShader, true);
State.StartTime = bx::getHPCounter();
}
void GameRendering::Update()
{
bgfx::touch(0);
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 0.0f, -5.0f };
float view[16];
bx::mtxLookAt(view, eye, at);
float proj[16];
bx::mtxProj(proj, 60.0f, float(State.WindowWidth) / float(State.WindowHeight), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
bgfx::setViewTransform(0, view, proj);
bgfx::setVertexBuffer(0, VertexBuffer);
bgfx::setIndexBuffer(IndexBuffer);
bgfx::dbgTextPrintf(10, 10, 0x0F, "TEST");
bgfx::submit(0, Shader);
bgfx::frame();
}
void GameRendering::Shutdown()
{
bgfx::shutdown();
}
}

View File

@@ -1,102 +1,101 @@
#pragma once
#include "bimg/bimg.h"
#include <bgfx/bgfx.h>
#include <cstdio>
#include <bx/string.h>
#include <cstdio>
namespace Game
{
struct VertexData
{
float X;
float Y;
float Z;
uint32_t VertCol;
};
struct VertexData
{
float X;
float Y;
float Z;
uint32_t VertCol;
};
struct BgfxCallback : public bgfx::CallbackI
{
virtual ~BgfxCallback()
{
}
struct BgfxCallback : public bgfx::CallbackI
{
virtual ~BgfxCallback()
{
}
virtual void fatal(const char* _filePath, uint16_t _line, bgfx::Fatal::Enum _code, const char* _str) override
{
// Something unexpected happened, inform user and bail out.
printf("Fatal error: 0x%08x: %s", _code, _str);
virtual void fatal(const char* _filePath, uint16_t _line, bgfx::Fatal::Enum _code, const char* _str) override
{
// Something unexpected happened, inform user and bail out.
printf("Fatal error: 0x%08x: %s", _code, _str);
// Must terminate, continuing will cause crash anyway.
abort();
}
// Must terminate, continuing will cause crash anyway.
abort();
}
virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) override
{
printf("%s (%d): ", _filePath, _line);
vprintf(_format, _argList);
}
virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) override
{
printf("%s (%d): ", _filePath, _line);
vprintf(_format, _argList);
}
virtual void profilerBegin(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override
{
}
virtual void profilerBegin(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override
{
}
virtual void profilerBeginLiteral(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override
{
}
virtual void profilerBeginLiteral(const char* /*_name*/, uint32_t /*_abgr*/, const char* /*_filePath*/, uint16_t /*_line*/) override
{
}
virtual void profilerEnd() override
{
}
virtual void profilerEnd() override
{
}
virtual uint32_t cacheReadSize(uint64_t _id) override
{
return 0;
}
virtual uint32_t cacheReadSize(uint64_t _id) override
{
return 0;
}
virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) override
{
return false;
}
virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) override
{
return false;
}
virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) override
{
}
virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) override
{
}
virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t /*_size*/, bool _yflip) override
{
}
virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t /*_size*/, bool _yflip) override
{
}
virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t /*_pitch*/, bgfx::TextureFormat::Enum /*_format*/, bool _yflip) override
{
}
virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t /*_pitch*/, bgfx::TextureFormat::Enum /*_format*/, bool _yflip) override
{
}
virtual void captureEnd() override
{
}
virtual void captureEnd() override
{
}
virtual void captureFrame(const void* _data, uint32_t /*_size*/) override
{
}
};
virtual void captureFrame(const void* _data, uint32_t /*_size*/) override
{
}
};
struct RenderState
{
int64_t StartTime = 0;
uint32_t WindowWidth = 1920;
uint32_t WindowHeight = 1080;
};
class GameRendering
{
private:
bgfx::VertexLayout VertLayout;
bgfx::VertexBufferHandle VertexBuffer;
bgfx::IndexBufferHandle IndexBuffer;
bgfx::ProgramHandle Shader;
BgfxCallback Callback;
RenderState State;
public:
void Setup(void* window);
void Update();
void Shutdown();
};
struct RenderState
{
int64_t StartTime = 0;
uint32_t WindowWidth = 1920;
uint32_t WindowHeight = 1080;
};
class GameRendering
{
private:
bgfx::VertexLayout VertLayout;
bgfx::VertexBufferHandle VertexBuffer;
bgfx::IndexBufferHandle IndexBuffer;
bgfx::ProgramHandle Shader;
BgfxCallback Callback;
RenderState State;
public:
void Setup(void* window);
void Update();
void Shutdown();
};
}