diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea57110..8e7f99b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,8 +22,9 @@ file(GLOB_RECURSE sources_game game/*.cpp game/*.h) add_library(PuzGame SHARED ${sources_game}) set_property(TARGET PuzGame PROPERTY CXX_STANDARD 17) -SET(BGFX_BUILD_TOOLS OFF) +SET(BGFX_BUILD_TOOLS ON) SET(BGFX_BUILD_EXAMPLES OFF) +SET(BGFX_CONFIG_MULTITHREADED OFF) add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/bgfx.cmake") target_link_libraries(PuzGame bx bimg bgfx) diff --git a/src/build.ps1 b/src/build.ps1 index 5ba655e..ce880a2 100644 --- a/src/build.ps1 +++ b/src/build.ps1 @@ -1 +1 @@ -cmake --build cmake-build && start ./cmake-build/PuzGameEngine.exe +cmake --build cmake-build diff --git a/src/engine/main.cpp b/src/engine/main.cpp index 4377f62..01770da 100644 --- a/src/engine/main.cpp +++ b/src/engine/main.cpp @@ -11,6 +11,7 @@ typedef void (*Startup)(void*); typedef void (*Update)(); +typedef void (*Shutdown)(); constexpr UINT WM_CUSTOM_DLL_CHANGE = WM_USER + 1; @@ -38,6 +39,7 @@ namespace DevelopmentData DevData; Startup StartupFunc; Update UpdateFunc; + Shutdown ShutdownFunc; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) @@ -163,8 +165,20 @@ bool ReloadDLL() return false; } +#ifdef VISUAL_STUDIO + Shutdown ShutdownReloaded = (Shutdown)GetProcAddress(DevData.GameLib, "_ZN4Game8ShutdownEv"); +#else + Shutdown ShutdownReloaded = (Shutdown)GetProcAddress(DevData.GameLib, "_ZN4Game8ShutdownEv"); +#endif + if (ShutdownReloaded == NULL) + { + printf("Failed to load shutdown function from game DLL\n"); + return false; + } + StartupFunc = StartupReloaded; UpdateFunc = UpdateReloaded; + ShutdownFunc = ShutdownReloaded; printf("Loaded Game DLL successfully!\n"); return true; @@ -177,19 +191,10 @@ int main() if (!ReloadDLL()) return 1; - bgfx::Init init; - init.platformData.nwh = (void*)window; - init.platformData.ndt = nullptr; - init.platformData.type = bgfx::NativeWindowHandleType::Default; - init.resolution.width = 1920; - init.resolution.height = 1080; - init.resolution.reset = BGFX_RESET_VSYNC; - bgfx::init(init); - - StartupFunc(window); DWORD fileWatcherThreadId = 0; CreateThread(NULL, 0, FileWatcherThread, NULL, 0, &fileWatcherThreadId); + StartupFunc(window); bool isRunning = true; while (isRunning) { @@ -198,7 +203,7 @@ int main() { if (msg.message == WM_QUIT) { - + isRunning = false; } TranslateMessage(&msg); @@ -208,9 +213,11 @@ int main() if (DevData.FileWatcher.Change) { DevData.FileWatcher.Change = false; + ShutdownFunc(); ReloadDLL(); + StartupFunc(window); } - + UpdateFunc(); } return 0; diff --git a/src/game/Setup.cpp b/src/game/Setup.cpp index d4f2841..5b99e22 100644 --- a/src/game/Setup.cpp +++ b/src/game/Setup.cpp @@ -1,22 +1,42 @@ #include "Setup.h" #include "Log.h" -#include +#include "rendering/Rendering.h" +#include namespace Game { - int32_t FrameCounter = 0; + class GameSetup + { + public: + GameRendering Rendering; + int32_t FrameCounter = 0; + }; + + namespace + { + GameSetup Instance; + } void Setup(void* window) { Log("Game Setup Start!"); + Instance.Rendering.Setup(window); } void Update() { - ++FrameCounter; - if (FrameCounter % 1000000 == 0) + ++Instance.FrameCounter; + if (Instance.FrameCounter % 100 == 0) { Log("Frame!"); } + + Instance.Rendering.Update(); + } + + void Shutdown() + { + Log("Shutdown"); + Instance.Rendering.Shutdown(); } } diff --git a/src/game/Setup.h b/src/game/Setup.h index 420ccd6..a4ad5c2 100644 --- a/src/game/Setup.h +++ b/src/game/Setup.h @@ -6,4 +6,5 @@ namespace Game { DLLEXPORT void Setup(void* window); DLLEXPORT void Update(); + DLLEXPORT void Shutdown(); } diff --git a/src/game/compiled-shaders/dx11/frag.bin b/src/game/compiled-shaders/dx11/frag.bin new file mode 100644 index 0000000..0008d70 Binary files /dev/null and b/src/game/compiled-shaders/dx11/frag.bin differ diff --git a/src/game/compiled-shaders/dx11/vert.bin b/src/game/compiled-shaders/dx11/vert.bin new file mode 100644 index 0000000..9538439 Binary files /dev/null and b/src/game/compiled-shaders/dx11/vert.bin differ diff --git a/src/game/compiled-shaders/glsl/frag.bin b/src/game/compiled-shaders/glsl/frag.bin new file mode 100644 index 0000000..79d5098 Binary files /dev/null and b/src/game/compiled-shaders/glsl/frag.bin differ diff --git a/src/game/compiled-shaders/glsl/vert.bin b/src/game/compiled-shaders/glsl/vert.bin new file mode 100644 index 0000000..9eed011 Binary files /dev/null and b/src/game/compiled-shaders/glsl/vert.bin differ diff --git a/src/game/compiled-shaders/spirv/frag.bin b/src/game/compiled-shaders/spirv/frag.bin new file mode 100644 index 0000000..bc6fc76 Binary files /dev/null and b/src/game/compiled-shaders/spirv/frag.bin differ diff --git a/src/game/compiled-shaders/spirv/vert.bin b/src/game/compiled-shaders/spirv/vert.bin new file mode 100644 index 0000000..a3d0932 Binary files /dev/null and b/src/game/compiled-shaders/spirv/vert.bin differ diff --git a/src/game/rendering/Rendering.cpp b/src/game/rendering/Rendering.cpp new file mode 100644 index 0000000..8e201f8 --- /dev/null +++ b/src/game/rendering/Rendering.cpp @@ -0,0 +1,187 @@ +#include "Rendering.h" +#include "../Log.h" +#include "bgfx/defines.h" +#include "bx/timer.h" +#include +#include +#include +#include + +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; + } + + 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; + + case bgfx::RendererType::Count: + BX_ASSERT(false, "You should not be here!"); + break; + } + + char fileName[512]{0}; + bx::strCopy(fileName, sizeof(fileName), name); + bx::strCat(fileName, sizeof(fileName), ".bin"); + filePath.join(fileName); + + bx::FileReader fileReader; + bgfx::ShaderHandle result = bgfx::createShader(loadMem(&fileReader, filePath.getCPtr())); + 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 = 1920; + init.resolution.height = 1080; + 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(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); + // Submit primitive for rendering to view 0. + bgfx::submit(0, Shader); + } + bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %f", time); + bgfx::frame(); + } + + void GameRendering::Shutdown() + { + bgfx::shutdown(); + } +} diff --git a/src/game/rendering/Rendering.h b/src/game/rendering/Rendering.h new file mode 100644 index 0000000..2261a1e --- /dev/null +++ b/src/game/rendering/Rendering.h @@ -0,0 +1,102 @@ +#pragma once +#include "bimg/bimg.h" +#include +#include +#include + +namespace Game +{ + struct VertexData + { + float X; + float Y; + float Z; + uint32_t VertCol; + }; + + 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); + + // 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); + printf(_format, _argList); + } + + 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 profilerEnd() override + { + } + + 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 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 captureBegin(uint32_t _width, uint32_t _height, uint32_t /*_pitch*/, bgfx::TextureFormat::Enum /*_format*/, bool _yflip) override + { + } + + virtual void captureEnd() 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(); + }; +} diff --git a/src/game/shaders/common.sh b/src/game/shaders/common.sh new file mode 100644 index 0000000..ac5e51a --- /dev/null +++ b/src/game/shaders/common.sh @@ -0,0 +1,7 @@ +/* + * Copyright 2011-2025 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE + */ + +#include +#include "shaderlib.sh" diff --git a/src/game/shaders/frag.sc b/src/game/shaders/frag.sc new file mode 100644 index 0000000..8035e62 --- /dev/null +++ b/src/game/shaders/frag.sc @@ -0,0 +1,7 @@ +$input v_color0 +#include "common.sh" + +void main() +{ + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); +} diff --git a/src/game/shaders/shaderlib.sh b/src/game/shaders/shaderlib.sh new file mode 100644 index 0000000..c0c17de --- /dev/null +++ b/src/game/shaders/shaderlib.sh @@ -0,0 +1,431 @@ +/* + * Copyright 2011-2025 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE + */ + +#ifndef __SHADERLIB_SH__ +#define __SHADERLIB_SH__ + +vec4 encodeRE8(float _r) +{ + float exponent = ceil(log2(_r) ); + return vec4(_r / exp2(exponent) + , 0.0 + , 0.0 + , (exponent + 128.0) / 255.0 + ); +} + +float decodeRE8(vec4 _re8) +{ + float exponent = _re8.w * 255.0 - 128.0; + return _re8.x * exp2(exponent); +} + +vec4 encodeRGBE8(vec3 _rgb) +{ + vec4 rgbe8; + float maxComponent = max(max(_rgb.x, _rgb.y), _rgb.z); + float exponent = ceil(log2(maxComponent) ); + rgbe8.xyz = _rgb / exp2(exponent); + rgbe8.w = (exponent + 128.0) / 255.0; + return rgbe8; +} + +vec3 decodeRGBE8(vec4 _rgbe8) +{ + float exponent = _rgbe8.w * 255.0 - 128.0; + vec3 rgb = _rgbe8.xyz * exp2(exponent); + return rgb; +} + +vec3 encodeNormalUint(vec3 _normal) +{ + return _normal * 0.5 + 0.5; +} + +vec3 decodeNormalUint(vec3 _encodedNormal) +{ + return _encodedNormal * 2.0 - 1.0; +} + +vec2 encodeNormalSphereMap(vec3 _normal) +{ + return normalize(_normal.xy) * sqrt(_normal.z * 0.5 + 0.5); +} + +vec3 decodeNormalSphereMap(vec2 _encodedNormal) +{ + float zz = dot(_encodedNormal, _encodedNormal) * 2.0 - 1.0; + return vec3(normalize(_encodedNormal.xy) * sqrt(1.0 - zz*zz), zz); +} + +vec2 octahedronWrap(vec2 _val) +{ + // Reference(s): + // - Octahedron normal vector encoding + // https://web.archive.org/web/20191027010600/https://knarkowicz.wordpress.com/2014/04/16/octahedron-normal-vector-encoding/comment-page-1/ + return (1.0 - abs(_val.yx) ) + * mix(vec2_splat(-1.0), vec2_splat(1.0), vec2(greaterThanEqual(_val.xy, vec2_splat(0.0) ) ) ); +} + +vec2 encodeNormalOctahedron(vec3 _normal) +{ + _normal /= abs(_normal.x) + abs(_normal.y) + abs(_normal.z); + _normal.xy = _normal.z >= 0.0 ? _normal.xy : octahedronWrap(_normal.xy); + _normal.xy = _normal.xy * 0.5 + 0.5; + return _normal.xy; +} + +vec3 decodeNormalOctahedron(vec2 _encodedNormal) +{ + _encodedNormal = _encodedNormal * 2.0 - 1.0; + + vec3 normal; + normal.z = 1.0 - abs(_encodedNormal.x) - abs(_encodedNormal.y); + normal.xy = normal.z >= 0.0 ? _encodedNormal.xy : octahedronWrap(_encodedNormal.xy); + return normalize(normal); +} + +vec3 convertRGB2XYZ(vec3 _rgb) +{ + // Reference(s): + // - RGB/XYZ Matrices + // https://web.archive.org/web/20191027010220/http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html + vec3 xyz; + xyz.x = dot(vec3(0.4124564, 0.3575761, 0.1804375), _rgb); + xyz.y = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb); + xyz.z = dot(vec3(0.0193339, 0.1191920, 0.9503041), _rgb); + return xyz; +} + +vec3 convertXYZ2RGB(vec3 _xyz) +{ + vec3 rgb; + rgb.x = dot(vec3( 3.2404542, -1.5371385, -0.4985314), _xyz); + rgb.y = dot(vec3(-0.9692660, 1.8760108, 0.0415560), _xyz); + rgb.z = dot(vec3( 0.0556434, -0.2040259, 1.0572252), _xyz); + return rgb; +} + +vec3 convertXYZ2Yxy(vec3 _xyz) +{ + // Reference(s): + // - XYZ to xyY + // https://web.archive.org/web/20191027010144/http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_xyY.html + float inv = 1.0/dot(_xyz, vec3(1.0, 1.0, 1.0) ); + return vec3(_xyz.y, _xyz.x*inv, _xyz.y*inv); +} + +vec3 convertYxy2XYZ(vec3 _Yxy) +{ + // Reference(s): + // - xyY to XYZ + // https://web.archive.org/web/20191027010036/http://www.brucelindbloom.com/index.html?Eqn_xyY_to_XYZ.html + vec3 xyz; + xyz.x = _Yxy.x*_Yxy.y/_Yxy.z; + xyz.y = _Yxy.x; + xyz.z = _Yxy.x*(1.0 - _Yxy.y - _Yxy.z)/_Yxy.z; + return xyz; +} + +vec3 convertRGB2Yxy(vec3 _rgb) +{ + return convertXYZ2Yxy(convertRGB2XYZ(_rgb) ); +} + +vec3 convertYxy2RGB(vec3 _Yxy) +{ + return convertXYZ2RGB(convertYxy2XYZ(_Yxy) ); +} + +vec3 convertRGB2Yuv(vec3 _rgb) +{ + vec3 yuv; + yuv.x = dot(_rgb, vec3(0.299, 0.587, 0.114) ); + yuv.y = (_rgb.x - yuv.x)*0.713 + 0.5; + yuv.z = (_rgb.z - yuv.x)*0.564 + 0.5; + return yuv; +} + +vec3 convertYuv2RGB(vec3 _yuv) +{ + vec3 rgb; + rgb.x = _yuv.x + 1.403*(_yuv.y-0.5); + rgb.y = _yuv.x - 0.344*(_yuv.y-0.5) - 0.714*(_yuv.z-0.5); + rgb.z = _yuv.x + 1.773*(_yuv.z-0.5); + return rgb; +} + +vec3 convertRGB2YIQ(vec3 _rgb) +{ + vec3 yiq; + yiq.x = dot(vec3(0.299, 0.587, 0.114 ), _rgb); + yiq.y = dot(vec3(0.595716, -0.274453, -0.321263), _rgb); + yiq.z = dot(vec3(0.211456, -0.522591, 0.311135), _rgb); + return yiq; +} + +vec3 convertYIQ2RGB(vec3 _yiq) +{ + vec3 rgb; + rgb.x = dot(vec3(1.0, 0.9563, 0.6210), _yiq); + rgb.y = dot(vec3(1.0, -0.2721, -0.6474), _yiq); + rgb.z = dot(vec3(1.0, -1.1070, 1.7046), _yiq); + return rgb; +} + +vec3 toLinear(vec3 _rgb) +{ + return pow(abs(_rgb), vec3_splat(2.2) ); +} + +vec4 toLinear(vec4 _rgba) +{ + return vec4(toLinear(_rgba.xyz), _rgba.w); +} + +vec3 toLinearAccurate(vec3 _rgb) +{ + vec3 lo = _rgb / 12.92; + vec3 hi = pow( (_rgb + 0.055) / 1.055, vec3_splat(2.4) ); + vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.04045) ) ) ); + return rgb; +} + +vec4 toLinearAccurate(vec4 _rgba) +{ + return vec4(toLinearAccurate(_rgba.xyz), _rgba.w); +} + +float toGamma(float _r) +{ + return pow(abs(_r), 1.0/2.2); +} + +vec3 toGamma(vec3 _rgb) +{ + return pow(abs(_rgb), vec3_splat(1.0/2.2) ); +} + +vec4 toGamma(vec4 _rgba) +{ + return vec4(toGamma(_rgba.xyz), _rgba.w); +} + +vec3 toGammaAccurate(vec3 _rgb) +{ + vec3 lo = _rgb * 12.92; + vec3 hi = pow(abs(_rgb), vec3_splat(1.0/2.4) ) * 1.055 - 0.055; + vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.0031308) ) ) ); + return rgb; +} + +vec4 toGammaAccurate(vec4 _rgba) +{ + return vec4(toGammaAccurate(_rgba.xyz), _rgba.w); +} + +vec3 toReinhard(vec3 _rgb) +{ + return toGamma(_rgb/(_rgb+vec3_splat(1.0) ) ); +} + +vec4 toReinhard(vec4 _rgba) +{ + return vec4(toReinhard(_rgba.xyz), _rgba.w); +} + +vec3 toFilmic(vec3 _rgb) +{ + _rgb = max(vec3_splat(0.0), _rgb - 0.004); + _rgb = (_rgb*(6.2*_rgb + 0.5) ) / (_rgb*(6.2*_rgb + 1.7) + 0.06); + return _rgb; +} + +vec4 toFilmic(vec4 _rgba) +{ + return vec4(toFilmic(_rgba.xyz), _rgba.w); +} + +vec3 toAcesFilmic(vec3 _rgb) +{ + // Reference(s): + // - ACES Filmic Tone Mapping Curve + // https://web.archive.org/web/20191027010704/https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/ + float aa = 2.51f; + float bb = 0.03f; + float cc = 2.43f; + float dd = 0.59f; + float ee = 0.14f; + return saturate( (_rgb*(aa*_rgb + bb) )/(_rgb*(cc*_rgb + dd) + ee) ); +} + +vec4 toAcesFilmic(vec4 _rgba) +{ + return vec4(toAcesFilmic(_rgba.xyz), _rgba.w); +} + +vec3 luma(vec3 _rgb) +{ + float yy = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb); + return vec3_splat(yy); +} + +vec4 luma(vec4 _rgba) +{ + return vec4(luma(_rgba.xyz), _rgba.w); +} + +vec3 conSatBri(vec3 _rgb, vec3 _csb) +{ + vec3 rgb = _rgb * _csb.z; + rgb = mix(luma(rgb), rgb, _csb.y); + rgb = mix(vec3_splat(0.5), rgb, _csb.x); + return rgb; +} + +vec4 conSatBri(vec4 _rgba, vec3 _csb) +{ + return vec4(conSatBri(_rgba.xyz, _csb), _rgba.w); +} + +vec3 posterize(vec3 _rgb, float _numColors) +{ + return floor(_rgb*_numColors) / _numColors; +} + +vec4 posterize(vec4 _rgba, float _numColors) +{ + return vec4(posterize(_rgba.xyz, _numColors), _rgba.w); +} + +vec3 sepia(vec3 _rgb) +{ + vec3 color; + color.x = dot(_rgb, vec3(0.393, 0.769, 0.189) ); + color.y = dot(_rgb, vec3(0.349, 0.686, 0.168) ); + color.z = dot(_rgb, vec3(0.272, 0.534, 0.131) ); + return color; +} + +vec4 sepia(vec4 _rgba) +{ + return vec4(sepia(_rgba.xyz), _rgba.w); +} + +vec3 blendOverlay(vec3 _base, vec3 _blend) +{ + vec3 lt = 2.0 * _base * _blend; + vec3 gte = 1.0 - 2.0 * (1.0 - _base) * (1.0 - _blend); + return mix(lt, gte, step(vec3_splat(0.5), _base) ); +} + +vec4 blendOverlay(vec4 _base, vec4 _blend) +{ + return vec4(blendOverlay(_base.xyz, _blend.xyz), _base.w); +} + +vec3 adjustHue(vec3 _rgb, float _hue) +{ + vec3 yiq = convertRGB2YIQ(_rgb); + float angle = _hue + atan2(yiq.z, yiq.y); + float len = length(yiq.yz); + return convertYIQ2RGB(vec3(yiq.x, len*cos(angle), len*sin(angle) ) ); +} + +vec4 packFloatToRgba(float _value) +{ + const vec4 shift = vec4(256 * 256 * 256, 256 * 256, 256, 1.0); + const vec4 mask = vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); + vec4 comp = fract(_value * shift); + comp -= comp.xxyz * mask; + return comp; +} + +float unpackRgbaToFloat(vec4 _rgba) +{ + const vec4 shift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0); + return dot(_rgba, shift); +} + +vec2 packHalfFloat(float _value) +{ + const vec2 shift = vec2(256, 1.0); + const vec2 mask = vec2(0, 1.0 / 256.0); + vec2 comp = fract(_value * shift); + comp -= comp.xx * mask; + return comp; +} + +float unpackHalfFloat(vec2 _rg) +{ + const vec2 shift = vec2(1.0 / 256.0, 1.0); + return dot(_rg, shift); +} + +float random(vec2 _uv) +{ + return fract(sin(dot(_uv.xy, vec2(12.9898, 78.233) ) ) * 43758.5453); +} + +vec3 fixCubeLookup(vec3 _v, float _lod, float _topLevelCubeSize) +{ + // Reference(s): + // - Seamless cube-map filtering + // https://web.archive.org/web/20190411181934/http://the-witness.net/news/2012/02/seamless-cube-map-filtering/ + float ax = abs(_v.x); + float ay = abs(_v.y); + float az = abs(_v.z); + float vmax = max(max(ax, ay), az); + float scale = 1.0 - exp2(_lod) / _topLevelCubeSize; + if (ax != vmax) { _v.x *= scale; } + if (ay != vmax) { _v.y *= scale; } + if (az != vmax) { _v.z *= scale; } + return _v; +} + +vec2 texture2DBc5(sampler2D _sampler, vec2 _uv) +{ +#if BGFX_SHADER_LANGUAGE_HLSL && BGFX_SHADER_LANGUAGE_HLSL <= 300 + return texture2D(_sampler, _uv).yx; +#else + return texture2D(_sampler, _uv).xy; +#endif +} + +mat3 cofactor(mat4 _m) +{ + // Reference: + // Cofactor of matrix. Use to transform normals. The code assumes the last column of _m is [0,0,0,1]. + // https://www.shadertoy.com/view/3s33zj + // https://github.com/graphitemaster/normals_revisited + return mat3( + _m[1][1]*_m[2][2]-_m[1][2]*_m[2][1], + _m[1][2]*_m[2][0]-_m[1][0]*_m[2][2], + _m[1][0]*_m[2][1]-_m[1][1]*_m[2][0], + _m[0][2]*_m[2][1]-_m[0][1]*_m[2][2], + _m[0][0]*_m[2][2]-_m[0][2]*_m[2][0], + _m[0][1]*_m[2][0]-_m[0][0]*_m[2][1], + _m[0][1]*_m[1][2]-_m[0][2]*_m[1][1], + _m[0][2]*_m[1][0]-_m[0][0]*_m[1][2], + _m[0][0]*_m[1][1]-_m[0][1]*_m[1][0] + ); +} + +float toClipSpaceDepth(float _depthTextureZ) +{ +#if BGFX_SHADER_LANGUAGE_GLSL + return _depthTextureZ * 2.0 - 1.0; +#else + return _depthTextureZ; +#endif // BGFX_SHADER_LANGUAGE_GLSL +} + +vec3 clipToWorld(mat4 _invViewProj, vec3 _clipPos) +{ + vec4 wpos = mul(_invViewProj, vec4(_clipPos, 1.0) ); + return wpos.xyz / wpos.w; +} + +#endif // __SHADERLIB_SH__ diff --git a/src/game/shaders/varying.def.sc b/src/game/shaders/varying.def.sc new file mode 100644 index 0000000..9d9d83e --- /dev/null +++ b/src/game/shaders/varying.def.sc @@ -0,0 +1,4 @@ +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); + +vec3 a_position : POSITION; +vec4 a_color0 : COLOR0; diff --git a/src/game/shaders/vert.sc b/src/game/shaders/vert.sc new file mode 100644 index 0000000..c5b7667 --- /dev/null +++ b/src/game/shaders/vert.sc @@ -0,0 +1,10 @@ +$input a_position, a_color0 +$output v_color0 + +#include "common.sh" + +void main() +{ + gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); + v_color0 = a_color0; +} diff --git a/src/shadercompile.bat b/src/shadercompile.bat new file mode 100644 index 0000000..e8791bc --- /dev/null +++ b/src/shadercompile.bat @@ -0,0 +1,6 @@ +.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\vert.sc -o .\game\compiled-shaders\dx11\vert.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type v --platform windows --profile s_5_0 +.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\frag.sc -o .\game\compiled-shaders\dx11\frag.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type f --platform windows --profile s_5_0 +.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\vert.sc -o .\game\compiled-shaders\glsl\vert.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type v --platform windows --profile 430 +.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\frag.sc -o .\game\compiled-shaders\glsl\frag.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type f --platform windows --profile 430 +.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\vert.sc -o .\game\compiled-shaders\spirv\vert.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type v --platform windows --profile spirv16-13 +.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\frag.sc -o .\game\compiled-shaders\spirv\frag.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type f --platform windows --profile spirv16-13