Skip to content

Commit

Permalink
Initial - slang support, render layers
Browse files Browse the repository at this point in the history
  • Loading branch information
xezno committed Sep 15, 2024
1 parent bc62939 commit 1c8b2eb
Show file tree
Hide file tree
Showing 45 changed files with 14,847 additions and 623 deletions.
511 changes: 249 additions & 262 deletions Content/core/shaders/pbr.mshdr

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions Content/core/shaders/tonemap/agx.mshdr
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Shader
{
Name = "AgX Shader";
Description = "AgX Tonemap Shader";
}

Common
{
// Empty
}

Vertex
{
struct fs_in
{
vec2 vTexCoord;
};

layout (location = 0) in vec3 vPosition;
layout (location = 1) in vec2 vTexCoord;

layout (location = 0) out fs_in vs_out;

void main()
{
vs_out.vTexCoord = vTexCoord;
gl_Position = vec4( vPosition, 1.0 );
}
}

Fragment
{
struct fs_in
{
vec2 vTexCoord;
};

layout (location = 0) in fs_in vs_out;
layout (location = 0) out vec4 outFragColor;

layout (set = 0, binding = 0) uniform sampler2D renderTexture;

vec3 sampleTexture( sampler2D target )
{
return texture( target, vs_out.vTexCoord.xy ).rgb;
}

void main()
{
vec3 fragColor = sampleTexture( renderTexture );
outFragColor = vec4(fragColor, 1.0f);
}
}
2 changes: 2 additions & 0 deletions Samples/mocha-minimal/code/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public override void OnStartup()
// Spawn a player
var player = new Player();
player.Position = new Vector3( 0, 5, 10 );

_ = new PostProcess( "shaders/tonemap/agx.mshdr" );
}
}
10 changes: 10 additions & 0 deletions Source/Mocha.Common/SceneMeshFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Mocha;

public enum SceneMeshFlags
{
WorldLayer = 1 << 1,
UILayer = 1 << 2,

Default = WorldLayer,
PostProcess = UILayer,
};
5 changes: 0 additions & 5 deletions Source/Mocha.Editor/Editor/ImGuiX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ public static bool BeginOverlay( string name )
return b;
}

public static string GetGPUName()
{
return NativeEditor.GetGPUName();
}

public static void RenderViewDropdown()
{
NativeEditor.RenderViewDropdown();
Expand Down
21 changes: 21 additions & 0 deletions Source/Mocha.Engine/Render/Assets/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class Material : Asset

public Glue.Material NativeMaterial { get; private set; }

private Material() { }

/// <summary>
/// Loads a material from an MMAT (compiled) file.
/// </summary>
Expand Down Expand Up @@ -130,4 +132,23 @@ public Material( string shaderPath, VertexAttribute[] vertexAttributes, Texture?

// TODO: File watcher here!
}

public static Material FromShader( string shaderPath, VertexAttribute[] vertexAttributes )
{
Material material = new();

var shaderFileBytes = FileSystem.Mounted.ReadAllBytes( shaderPath );
var shaderFormat = Serializer.Deserialize<MochaFile<ShaderInfo>>( shaderFileBytes );

material.Path = "Procedural Material";

material.NativeMaterial = new(
material.Path,
shaderFormat.Data.VertexShaderData.ToInterop(),
shaderFormat.Data.FragmentShaderData.ToInterop(),
vertexAttributes.ToInterop()
);

return material;
}
}
2 changes: 1 addition & 1 deletion Source/Mocha.Engine/Render/Assets/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public partial class Model<T> : Asset, IModel where T : struct

protected void AddMesh( T[] vertices, Material material )
{
NativeModel.AddMesh( Path, vertices.ToInterop(), new uint[0].ToInterop(), material.NativeMaterial );
NativeModel.AddMesh( Path, vertices.ToInterop(), material.NativeMaterial );
}

protected void AddMesh( T[] vertices, uint[] indices, Material material )
Expand Down
35 changes: 35 additions & 0 deletions Source/Mocha.Engine/Render/PostProcess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Mocha;

public class PostProcess
{
private Glue.SceneMesh Native => NativeEngine.GetSceneGraph().GetMesh( NativeHandle );
private uint NativeHandle;

private Material material;
private Model model;

public PostProcess( string shaderPath )
{
material = Material.FromShader(
shaderPath,
[
new VertexAttribute( "Position", VertexAttributeFormat.Float3 ),
new VertexAttribute( "UV", VertexAttributeFormat.Float2 )
]
);

NativeHandle = NativeEngine.GetSceneGraph().CreateMesh();
Native.SetFlags( SceneMeshFlags.PostProcess );

model = new Model(
[
new Vertex() { Position = new Vector3( -1, -1, 0 ), UV = new Vector2(0, 0)},
new Vertex() { Position = new Vector3( 3, -1, 0 ), UV = new Vector2(2, 0)},
new Vertex() { Position = new Vector3( -1, 3, 0 ), UV = new Vector2(0, 2)},
],
material
);

Native.SetModel( model.NativeModel );
}
}
13 changes: 13 additions & 0 deletions Source/Mocha.Host/Entities/baseentity.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@
class Camera;
class Model;

enum SceneMeshFlags
{
SCENE_MESH_FLAGS_WORLD_LAYER = 1 << 1,
SCENE_MESH_FLAGS_UI_LAYER = 1 << 2,

SCENE_MESH_FLAGS_DEFAULT = SCENE_MESH_FLAGS_WORLD_LAYER,
SCENE_MESH_FLAGS_POSTPROCESS = SCENE_MESH_FLAGS_UI_LAYER,
};

class SceneMesh
{
private:
Model* m_model;
SceneMeshFlags m_flags = SCENE_MESH_FLAGS_DEFAULT;

public:
SceneMesh()
Expand All @@ -34,4 +44,7 @@ class SceneMesh

GENERATE_BINDINGS void SetModel( Model* model ) { m_model = model; }
GENERATE_BINDINGS Model* GetModel() { return m_model; }

GENERATE_BINDINGS void SetFlags( SceneMeshFlags flags ) { m_flags = flags; }
GENERATE_BINDINGS SceneMeshFlags GetFlags() { return m_flags; }
};
5 changes: 0 additions & 5 deletions Source/Mocha.Host/Misc/editormanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ void EditorManager::TextLight( const char* text )
ImGui::PopStyleColor();
}

const char* EditorManager::GetGPUName()
{
return Globals::m_renderManager->GetGPUName();
}

char* EditorManager::InputText( const char* name, char* inputBuf, int inputLength )
{
ImGui::InputText( name, inputBuf, inputLength, ImGuiInputTextFlags_EnterReturnsTrue );
Expand Down
1 change: 0 additions & 1 deletion Source/Mocha.Host/Misc/editormanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class EditorManager : ISubSystem
GENERATE_BINDINGS void TextHeading( const char* text );
GENERATE_BINDINGS void TextMonospace( const char* text );
GENERATE_BINDINGS void TextLight( const char* text );
GENERATE_BINDINGS const char* GetGPUName();
GENERATE_BINDINGS char* InputText( const char* name, char* inputBuf, int inputLength );
GENERATE_BINDINGS void RenderViewDropdown();
GENERATE_BINDINGS void Image( Texture* texture, uint32_t textureWidth, uint32_t textureHeight, int x, int y );
Expand Down
33 changes: 27 additions & 6 deletions Source/Mocha.Host/Mocha.Host.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)..\build</OutDir>
<TargetName>Mocha.Host</TargetName>
<ExternalIncludePath>$(VULKAN_SDK)\Include;$(ProjectDir)ThirdParty\volk;$(ProjectDir)ThirdParty\Renderdoc;$(ProjectDir)ThirdParty\FontAwesome;$(ProjectDir)ThirdParty\vk-bootstrap\src;$(ProjectDir)ThirdParty\imgui;$(ProjectDir)ThirdParty\implot;$(ProjectDir)ThirdParty\JoltPhysics;$(SolutionDir)vcpkg_installed\$(Platform)-windows\include;$(SolutionDir)vcpkg_installed\$(Platform)-windows\include\SDL2;$(ExternalIncludePath)</ExternalIncludePath>
<LibraryPath>$(LibraryPath)</LibraryPath>
<ExternalIncludePath>$(VULKAN_SDK)\Include;$(ProjectDir)ThirdParty\volk;$(ProjectDir)ThirdParty\Renderdoc;$(ProjectDir)ThirdParty\FontAwesome;$(ProjectDir)ThirdParty\vk-bootstrap\src;$(ProjectDir)ThirdParty\imgui;$(ProjectDir)ThirdParty\implot;$(ProjectDir)ThirdParty\JoltPhysics;$(ProjectDir)ThirdParty\slang\include;$(SolutionDir)vcpkg_installed\$(Platform)-windows\include;$(SolutionDir)vcpkg_installed\$(Platform)-windows\include\SDL2;$(ExternalIncludePath)</ExternalIncludePath>
<LibraryPath>$(ProjectDir)ThirdParty\slang\lib;$(LibraryPath)</LibraryPath>
<SourcePath>$(VC_SourcePath)</SourcePath>
<IncludePath>$(ProjectDir);$(IncludePath)</IncludePath>
<LocalDebuggerWorkingDirectory>$(SolutionDir)..\</LocalDebuggerWorkingDirectory>
Expand All @@ -83,8 +83,8 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(SolutionDir)..\build</OutDir>
<TargetName>Mocha.Host</TargetName>
<ExternalIncludePath>$(VULKAN_SDK)\Include;$(ProjectDir)ThirdParty\volk;$(ProjectDir)ThirdParty\Renderdoc;$(ProjectDir)ThirdParty\FontAwesome;$(ProjectDir)ThirdParty\vk-bootstrap\src;$(ProjectDir)ThirdParty\imgui;$(ProjectDir)ThirdParty\implot;$(ProjectDir)ThirdParty\JoltPhysics;$(SolutionDir)vcpkg_installed\$(Platform)-windows\include;$(SolutionDir)vcpkg_installed\$(Platform)-windows\include\SDL2;$(ExternalIncludePath)</ExternalIncludePath>
<LibraryPath>$(LibraryPath)</LibraryPath>
<ExternalIncludePath>$(VULKAN_SDK)\Include;$(ProjectDir)ThirdParty\volk;$(ProjectDir)ThirdParty\Renderdoc;$(ProjectDir)ThirdParty\FontAwesome;$(ProjectDir)ThirdParty\vk-bootstrap\src;$(ProjectDir)ThirdParty\imgui;$(ProjectDir)ThirdParty\implot;$(ProjectDir)ThirdParty\JoltPhysics;$(ProjectDir)ThirdParty\slang\include;$(SolutionDir)vcpkg_installed\$(Platform)-windows\include;$(SolutionDir)vcpkg_installed\$(Platform)-windows\include\SDL2;$(ExternalIncludePath)</ExternalIncludePath>
<LibraryPath>$(ProjectDir)ThirdParty\slang\lib;$(LibraryPath)</LibraryPath>
<SourcePath>$(VC_SourcePath)</SourcePath>
<IncludePath>$(ProjectDir);$(IncludePath)</IncludePath>
<LocalDebuggerWorkingDirectory>$(SolutionDir)..\</LocalDebuggerWorkingDirectory>
Expand Down Expand Up @@ -116,6 +116,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>slang-rt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand Down Expand Up @@ -158,7 +159,7 @@ echo | set /p dummyName=#define GIT_BRANCH &gt;&gt; gitdefs.h
git rev-parse --abbrev-ref HEAD &gt;&gt; gitdefs.h</Command>
</PreBuildEvent>
<Lib>
<AdditionalDependencies>GameNetworkingSockets.lib</AdditionalDependencies>
<AdditionalDependencies>GameNetworkingSockets.lib;slang.lib;slang-rt.lib;</AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down Expand Up @@ -189,7 +190,7 @@ echo | set /p dummyName=#define GIT_BRANCH &gt;&gt; gitdefs.h
git rev-parse --abbrev-ref HEAD &gt;&gt; gitdefs.h</Command>
</PreBuildEvent>
<Lib>
<AdditionalDependencies>GameNetworkingSockets.lib</AdditionalDependencies>
<AdditionalDependencies>GameNetworkingSockets.lib;slang.lib;slang-rt.lib;</AdditionalDependencies>
</Lib>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down Expand Up @@ -656,6 +657,20 @@ git rev-parse --abbrev-ref HEAD &gt;&gt; gitdefs.h</Command>
<ClInclude Include="thirdparty\JoltPhysics\Jolt\TriangleSplitter\TriangleSplitterMean.h" />
<ClInclude Include="thirdparty\JoltPhysics\Jolt\TriangleSplitter\TriangleSplitterMorton.h" />
<ClInclude Include="thirdparty\Renderdoc\renderdoc_app.h" />
<ClInclude Include="Thirdparty\slang\include\slang-com-helper.h" />
<ClInclude Include="Thirdparty\slang\include\slang-com-ptr.h" />
<ClInclude Include="Thirdparty\slang\include\slang-cpp-host-prelude.h" />
<ClInclude Include="Thirdparty\slang\include\slang-cpp-prelude.h" />
<ClInclude Include="Thirdparty\slang\include\slang-cpp-scalar-intrinsics.h" />
<ClInclude Include="Thirdparty\slang\include\slang-cpp-types-core.h" />
<ClInclude Include="Thirdparty\slang\include\slang-cpp-types.h" />
<ClInclude Include="Thirdparty\slang\include\slang-cuda-prelude.h" />
<ClInclude Include="Thirdparty\slang\include\slang-gfx.h" />
<ClInclude Include="Thirdparty\slang\include\slang-hlsl-prelude.h" />
<ClInclude Include="Thirdparty\slang\include\slang-llvm.h" />
<ClInclude Include="Thirdparty\slang\include\slang-tag-version.h" />
<ClInclude Include="Thirdparty\slang\include\slang-torch-prelude.h" />
<ClInclude Include="Thirdparty\slang\include\slang.h" />
<ClInclude Include="thirdparty\vk-bootstrap\src\VkBootstrap.h" />
<ClInclude Include="thirdparty\vk-bootstrap\src\VkBootstrapDispatch.h" />
<ClInclude Include="thirdparty\volk\volk.h" />
Expand All @@ -677,6 +692,12 @@ git rev-parse --abbrev-ref HEAD &gt;&gt; gitdefs.h</Command>
<None Include="thirdparty\JoltPhysics\Jolt\Math\Vec8.inl" />
<None Include="thirdparty\JoltPhysics\Jolt\Physics\Body\Body.inl" />
<None Include="thirdparty\JoltPhysics\Jolt\Physics\Body\MotionProperties.inl" />
<CopyFileToFolders Include="Thirdparty\slang\bin\slang-glslang.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="Thirdparty\slang\bin\slang.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
53 changes: 53 additions & 0 deletions Source/Mocha.Host/Mocha.Host.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
<Filter Include="Rendering\Platform\Null">
<UniqueIdentifier>{a6ab7305-4387-4823-9506-9137221613fd}</UniqueIdentifier>
</Filter>
<Filter Include="Thirdparty\slang">
<UniqueIdentifier>{d58b7182-ebad-42cc-ada1-12a66f828a3d}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="thirdparty\imgui\imgui_tables.cpp">
Expand Down Expand Up @@ -1457,6 +1460,48 @@
</ClInclude>
<ClInclude Include="Managed\managedcallback.h" />
<ClInclude Include="Managed\managedcallbackdispatchinfo.h" />
<ClInclude Include="Thirdparty\slang\include\slang-cpp-prelude.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-cpp-scalar-intrinsics.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-cpp-types.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-cpp-types-core.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-cuda-prelude.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-gfx.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-hlsl-prelude.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-llvm.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-tag-version.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-torch-prelude.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-com-helper.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-com-ptr.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
<ClInclude Include="Thirdparty\slang\include\slang-cpp-host-prelude.h">
<Filter>Thirdparty\slang</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="thirdparty\JoltPhysics\Jolt\Math\DVec3.inl">
Expand Down Expand Up @@ -1502,4 +1547,12 @@
<Filter>Thirdparty\Jolt</Filter>
</None>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="Thirdparty\slang\bin\slang.dll">
<Filter>Thirdparty\slang</Filter>
</CopyFileToFolders>
<CopyFileToFolders Include="Thirdparty\slang\bin\slang-glslang.dll">
<Filter>Thirdparty\slang</Filter>
</CopyFileToFolders>
</ItemGroup>
</Project>
18 changes: 18 additions & 0 deletions Source/Mocha.Host/Rendering/Assets/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ Material::Material( const char* name, UtilArray vertexShaderData, UtilArray frag
m_name = std::string( name );
}

Material::Material( const char* name, UtilArray vertexShaderData, UtilArray fragmentShaderData, UtilArray vertexAttributes )
{
m_vertexShaderData = vertexShaderData.GetData<uint32_t>();
m_fragmentShaderData = fragmentShaderData.GetData<uint32_t>();

m_isDirty.store( true );

auto vertexAttribInfo = vertexAttributes.GetData<InteropVertexAttributeInfo>();
for ( int i = 0; i < vertexAttributes.count; i++ )
{
m_vertexAttribInfo.push_back( vertexAttribInfo[i].ToNative() );
}

m_samplerType = SAMPLER_TYPE_LINEAR;
m_ignoreDepth = true;
m_name = std::string( name );
}

void Material::Reload()
{
m_isDirty.store( true );
Expand Down
Loading

0 comments on commit 1c8b2eb

Please sign in to comment.