Skip to content

Commit

Permalink
Instanced rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
kraifpatrik committed Jun 28, 2023
1 parent 59ea4ff commit 69d9c94
Show file tree
Hide file tree
Showing 50 changed files with 888 additions and 453 deletions.
6 changes: 4 additions & 2 deletions GMD3D11.resource_order
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"FolderOrderSettings": [
{"name":"ShaderOverride","order":1,"path":"folders/Examples/ShaderOverride.yy",},
{"name":"InstancedRendering","order":1,"path":"folders/Examples/InstancedRendering.yy",},
],
"ResourceOrderSettings": [
{"name":"RmShaderOverride","order":1,"path":"rooms/RmShaderOverride/RmShaderOverride.yy",},
{"name":"RmInstancedRendering","order":1,"path":"rooms/RmInstancedRendering/RmInstancedRendering.yy",},
{"name":"RmVTF","order":1,"path":"rooms/RmVTF/RmVTF.yy",},
{"name":"__d3d11_shader","order":2,"path":"scripts/__d3d11_shader/__d3d11_shader.yy",},
{"name":"SprRubberDuck","order":2,"path":"sprites/SprRubberDuck/SprRubberDuck.yy",},
{"name":"__d3d11_cbuffer","order":1,"path":"scripts/__d3d11_cbuffer/__d3d11_cbuffer.yy",},
{"name":"SprVTF","order":3,"path":"sprites/SprVTF/SprVTF.yy",},
{"name":"yamc","order":1,"path":"scripts/yamc/yamc.yy",},
{"name":"ShVTF","order":2,"path":"shaders/ShVTF/ShVTF.yy",},
],
}
14 changes: 9 additions & 5 deletions GMD3D11.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Compile vertex and pixel shaders from files.
* Hook into `ID3D11DeviceContext::Draw` calls to swap out used vertex and pixel shaders with custom ones.
* Constant buffers.
* Instanced rendering.

## Building the DLL
Requires [CMake](https://cmake.org/) 3.23 at least!
Expand All @@ -27,3 +28,4 @@ Copy-Item -Path ./Release/GMD3D11.dll -Destination ../../datafiles

## Credits
* https://github.com/ParinovYT/cVmtHook-x64 - x64 VMT hook
* https://polyhaven.com/a/rubber_duck_toy - model used in instanced rendering example
8 changes: 6 additions & 2 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ project(GMD3D11)
set(CMAKE_CXX_STANDARD 17)

add_library(GMD3D11 SHARED
src/cbuffer.cpp
src/CBuffer.cpp
src/element.cpp
src/main.cpp
src/shader.cpp
src/PShader.cpp
src/Shader.cpp
src/utils.cpp
src/VShader.cpp
)

target_include_directories(GMD3D11 PRIVATE
Expand Down
41 changes: 41 additions & 0 deletions cpp/include/CBuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <element.hpp>

#include <cstdint>
#include <d3d11.h>
#include <unordered_map>

extern std::unordered_map<size_t, class CBuffer*> gCBuffers;

class CBuffer
{
public:
~CBuffer()
{
if (Buffer)
{
Buffer->Release();
}
}

bool AddElement(ElementType type, uint32_t count)
{
if (count < 1)
{
return false;
}
Size += GetElementSize(type) * count;
return true;
}

size_t GetSize() const
{
return Size;
}

ID3D11Buffer* Buffer = nullptr;

private:
size_t Size = 0;
};
29 changes: 29 additions & 0 deletions cpp/include/PShader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <Shader.hpp>

class PShader : public Shader
{
public:
PShader(ID3DBlob* blob, ID3D11PixelShader* shader)
: Raw(shader)
, Shader(blob)
{
}

~PShader()
{
if (Raw)
{
Raw->Release();
}
}

ID3D11PixelShader* GetShader() const
{
return Raw;
}

private:
ID3D11PixelShader* Raw = nullptr;
};
34 changes: 34 additions & 0 deletions cpp/include/Shader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <d3d11.h>
#include <unordered_map>

extern std::unordered_map<size_t, class Shader*> gShaders;

class Shader
{
public:
Shader(ID3DBlob* blob)
: Blob(blob)
{
}

virtual ~Shader()
{
if (Blob)
{
Blob->Release();
}
}

ID3DBlob* GetBlob() const
{
return Blob;
}

private:
ID3DBlob* Blob = nullptr;
};

// Source: https://learn.microsoft.com/en-us/windows/win32/direct3d11/how-to--compile-a-shader
HRESULT CompileShader(_In_ LPCWSTR srcFile, _In_ LPCSTR entryPoint, _In_ LPCSTR profile, _Outptr_ ID3DBlob** blob);
29 changes: 29 additions & 0 deletions cpp/include/VShader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <Shader.hpp>

class VShader : public Shader
{
public:
VShader(ID3DBlob* blob, ID3D11VertexShader* shader)
: Raw(shader)
, Shader(blob)
{
}

~VShader()
{
if (Raw)
{
Raw->Release();
}
}

ID3D11VertexShader* GetShader() const
{
return Raw;
}

private:
ID3D11VertexShader* Raw = nullptr;
};
11 changes: 11 additions & 0 deletions cpp/include/element.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

enum class ElementType
{
Bool,
Int,
Uint,
Float
};

size_t GetElementSize(ElementType type);
4 changes: 4 additions & 0 deletions cpp/include/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

// Source: https://stackoverflow.com/a/19717944
wchar_t* ConvertCharArrayToLPCWSTR(const char* charArray);
62 changes: 62 additions & 0 deletions cpp/src/PShader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <PShader.hpp>

#include <exports.hpp>
#include <Shader.hpp>
#include <utils.hpp>

#include <iostream>

extern ID3D11Device* gDevice;

extern ID3D11DeviceContext* gContext;

extern char* gErrorString;

extern size_t gShaderID;

ID3D11PixelShader* gOverridePS = nullptr;

static PShader* CompilePS(char* file, char* entryPoint, char* profile)
{
ID3DBlob* blob = nullptr;
LPCWSTR path = ConvertCharArrayToLPCWSTR(file);
HRESULT hr = CompileShader(path, (LPCSTR)entryPoint, (LPCSTR)profile, &blob);
delete path;

if (FAILED(hr))
{
std::cout << "Failed compiling PS " << file << "!" << std::endl;
return nullptr;
}

ID3D11PixelShader* ps = nullptr;
hr = gDevice->CreatePixelShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, &ps);

if (FAILED(hr))
{
std::cout << "Failed creating PS " << file << "!" << std::endl;
return nullptr;
}

std::cout << "Compiled PS " << file << std::endl;

return new PShader(blob, ps);
}

GM_EXPORT double d3d11_shader_compile_ps(char* file, char* entryPoint, char* profile)
{
PShader* shader = CompilePS(file, entryPoint, profile);
if (!shader)
{
return -1.0;
}
size_t index = gShaderID++;
gShaders[index] = shader;
return (double)index;
}

GM_EXPORT double d3d11_shader_override_ps(double ps)
{
gOverridePS = (ps >= 0.0) ? ((PShader*)gShaders[(size_t)ps])->GetShader() : nullptr;
return GM_TRUE;
}
62 changes: 62 additions & 0 deletions cpp/src/VShader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <VShader.hpp>

#include <exports.hpp>
#include <Shader.hpp>
#include <utils.hpp>

#include <iostream>

extern ID3D11Device* gDevice;

extern ID3D11DeviceContext* gContext;

extern char* gErrorString;

extern size_t gShaderID;

ID3D11VertexShader* gOverrideVS = nullptr;

static VShader* CompileVS(char* file, char* entryPoint, char* profile)
{
ID3DBlob* blob = nullptr;
LPCWSTR path = ConvertCharArrayToLPCWSTR(file);
HRESULT hr = CompileShader(path, (LPCSTR)entryPoint, (LPCSTR)profile, &blob);
delete path;

if (FAILED(hr))
{
std::cout << "Failed compiling VS " << file << "!" << std::endl;
return nullptr;
}

ID3D11VertexShader* vs = nullptr;
hr = gDevice->CreateVertexShader(blob->GetBufferPointer(), blob->GetBufferSize(), nullptr, &vs);

if (FAILED(hr))
{
std::cout << "Failed creating VS " << file << "!" << std::endl;
return nullptr;
}

std::cout << "Compiled VS " << file << std::endl;

return new VShader(blob, vs);
}

GM_EXPORT double d3d11_shader_compile_vs(char* file, char* entryPoint, char* profile)
{
VShader* shader = CompileVS(file, entryPoint, profile);
if (!shader)
{
return -1.0;
}
size_t index = gShaderID++;
gShaders[index] = shader;
return (double)index;
}

GM_EXPORT double d3d11_shader_override_vs(double vs)
{
gOverrideVS = (vs >= 0.0) ? ((VShader*)gShaders[(size_t)vs])->GetShader() : nullptr;
return GM_TRUE;
}
Loading

0 comments on commit 69d9c94

Please sign in to comment.