Add sampler support and refactor resource handling
Enhanced shader and resource systems with `Sampler` support, including updates to `ShaderPropertyType`, HLSL code, and resource management. Refactored `Result` structs for better type safety and added new enums for texture and comparison settings. Improved `MeshRenderPass` to dynamically load textures and samplers. Updated SDL compiler and token lexicon for `Sampler` handling. Embedded debug info in project files and streamlined resource state tracking.
This commit is contained in:
@@ -10,6 +10,8 @@ struct Vertex
|
||||
float4 color;
|
||||
};
|
||||
|
||||
#define SIZEOF_VERTEX 80 // bytes
|
||||
|
||||
// Resource descriptor heap definitions
|
||||
|
||||
#define GLOBAL_TEXTURE2D_HEAP ResourceDescriptorHeap
|
||||
@@ -44,50 +46,52 @@ struct Vertex
|
||||
#define GET_BUFFER(id) GLOBAL_BUFFER_HEAP[id]
|
||||
#define GET_SAMPLER(id) GLOBAL_SAMPLER_HEAP[id]
|
||||
|
||||
#define SAMPLE_TEXTURE2D(texId, sampId, uv) SampleTexture2D(texId, sampId, uv)
|
||||
#define SAMPLE_TEXTURE2D_LEVEL(texId, sampId, uv, level) SampleTexture2DLevel(texId, sampId, uv, level)
|
||||
#define SAMPLE_TEXTURE2D_ARRAY(texId, sampId, uvw) SampleTextureArray(texId, sampId, uvw)
|
||||
|
||||
#define MESH_SHADER_THREADS(x) [NumThreads(x, 1, 1)]
|
||||
|
||||
|
||||
inline float4 SampleTexture2D(uint texId, uint sampId, float2 uv)
|
||||
static inline float4 SampleTexture2D(uint texId, uint sampId, float2 uv)
|
||||
{
|
||||
Texture2D tex = GET_TEXTURE2D(texId);
|
||||
SamplerState samp = GET_SAMPLER(sampId);
|
||||
return tex.Sample(samp, uv);
|
||||
}
|
||||
|
||||
inline float4 SampleTexture2DLevel(uint texId, uint sampId, float2 uv, float level)
|
||||
static inline float4 SampleTexture2DLevel(uint texId, uint sampId, float2 uv, float level)
|
||||
{
|
||||
Texture2D tex = GET_TEXTURE2D(texId);
|
||||
SamplerState samp = GET_SAMPLER(sampId);
|
||||
return tex.SampleLevel(samp, uv, level);
|
||||
}
|
||||
|
||||
inline float4 SampleTextureArray(uint texId, uint sampId, float3 uv)
|
||||
static inline float4 SampleTextureArray(uint texId, uint sampId, float3 uvw)
|
||||
{
|
||||
Texture2DArray tex = GET_TEXTURE2D_ARRAY(texId);
|
||||
SamplerState samp = GET_SAMPLER(sampId);
|
||||
return tex.Sample(samp, uvw.xyz);
|
||||
return tex.Sample(samp, uvw);
|
||||
}
|
||||
|
||||
|
||||
inline Vertex LoadVertexData(uint vertexID, uint groupID, BYTE_ADDRESS_BUFFER vertexBuffer, BYTE_ADDRESS_BUFFER indexBuffer)
|
||||
static inline Vertex LoadVertexData(uint vertexID, uint groupID, BYTE_ADDRESS_BUFFER vertexBuffer, BYTE_ADDRESS_BUFFER indexBuffer)
|
||||
{
|
||||
// Fetch bindless buffers
|
||||
ByteAddressBuffer vertexBuffer = GET_BUFFER(vertexBuffer);
|
||||
ByteAddressBuffer indexBuffer = GET_BUFFER(indexBuffer);
|
||||
ByteAddressBuffer vertices = GET_BUFFER(vertexBuffer);
|
||||
ByteAddressBuffer indices = GET_BUFFER(indexBuffer);
|
||||
|
||||
// Compute the triangle’s vertex indices
|
||||
uint indexOffset = (groupID * 3 + vertexID) * 4; // uint32 index
|
||||
uint vertexIndex = indexBuffer.Load(indexOffset);
|
||||
|
||||
uint vertexIndex = indices.Load(indexOffset);
|
||||
|
||||
// Load vertex attributes
|
||||
uint vertexOffset = vertexIndex * 80; // 80 bytes per vertex
|
||||
uint vertexOffset = vertexIndex * SIZEOF_VERTEX;
|
||||
Vertex v;
|
||||
v.position = asfloat(vertexBuffer.Load4(vertexOffset + 0));
|
||||
v.normal = asfloat(vertexBuffer.Load4(vertexOffset + 16));
|
||||
v.tangent = asfloat(vertexBuffer.Load4(vertexOffset + 32));
|
||||
v.uv = asfloat(vertexBuffer.Load4(vertexOffset + 48));
|
||||
v.color = asfloat(vertexBuffer.Load4(vertexOffset + 64));
|
||||
v.position = asfloat(vertices.Load4(vertexOffset + 0));
|
||||
v.normal = asfloat(vertices.Load4(vertexOffset + 16));
|
||||
v.tangent = asfloat(vertices.Load4(vertexOffset + 32));
|
||||
v.uv = asfloat(vertices.Load4(vertexOffset + 48));
|
||||
v.color = asfloat(vertices.Load4(vertexOffset + 64));
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ struct PerObjectData
|
||||
{
|
||||
float4x4 localToWorld;
|
||||
float3 worldBoundsMin;
|
||||
BYTE_ADDRESS_BUFFER_BINDLESS vertexBuffer;
|
||||
BYTE_ADDRESS_BUFFER vertexBuffer;
|
||||
float3 worldBoundsMax;
|
||||
BYTE_ADDRESS_BUFFER_BINDLESS indexBuffer;
|
||||
BYTE_ADDRESS_BUFFER indexBuffer;
|
||||
};
|
||||
|
||||
cbuffer GlobalConstants : register(b0)
|
||||
|
||||
@@ -182,6 +182,9 @@ internal class PropertiesBlock : IBlockParser<PropertiesSyntax, List<PropertySem
|
||||
TokenLexicon.KnownTypes.TEXTURE2D => ShaderPropertyType.Texture2D,
|
||||
TokenLexicon.KnownTypes.TEXTURE3D => ShaderPropertyType.Texture3D,
|
||||
TokenLexicon.KnownTypes.TEXTURECUBE => ShaderPropertyType.TextureCube,
|
||||
TokenLexicon.KnownTypes.TEXTURECUBE_ARRAY => ShaderPropertyType.TextureCubeArray,
|
||||
TokenLexicon.KnownTypes.TEXTURE2D_ARRAY => ShaderPropertyType.Texture2DArray,
|
||||
TokenLexicon.KnownTypes.SAMPLER => ShaderPropertyType.Sampler,
|
||||
_ => ShaderPropertyType.None,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -257,7 +257,7 @@ internal static class SDLCompiler
|
||||
return Result.Failure("Failed to generate global properties: " + globalPropResult.Message);
|
||||
}
|
||||
|
||||
var generatedResult = GenerateShaderCode(desc, generatedOutputDirectory);
|
||||
var generatedResult = GenerateShaderCode(desc, generatedOutputDirectory, globalPropResult.Value);
|
||||
if (generatedResult.IsFailure)
|
||||
{
|
||||
return Result.Failure("Failed to generate pass files: " + generatedResult.Message);
|
||||
@@ -269,7 +269,7 @@ internal static class SDLCompiler
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Failure("Failed to generate shader files: " + ex.Message);
|
||||
return Result.Failure("Failed to compile shader: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,16 +294,17 @@ internal static class SDLCompiler
|
||||
ShaderPropertyType.Bool3 => "bool3",
|
||||
ShaderPropertyType.Bool4 => "bool4",
|
||||
// NOTE: Textures here are bindless, represented as uint (descriptor index).
|
||||
ShaderPropertyType.Texture2D => "TEXTURE2D_BINDLESS",
|
||||
ShaderPropertyType.Texture3D => "TEXTURE3D_BINDLESS",
|
||||
ShaderPropertyType.TextureCube => "TEXTURECUBE_BINDLESS",
|
||||
ShaderPropertyType.Texture2DArray => "TEXTURE2D_ARRAY_BINDLESS",
|
||||
ShaderPropertyType.TextureCubeArray => "TEXTURECUBE_ARRAY_BINDLESS",
|
||||
ShaderPropertyType.Texture2D => "TEXTURE2D",
|
||||
ShaderPropertyType.Texture3D => "TEXTURE3D",
|
||||
ShaderPropertyType.TextureCube => "TEXTURECUBE",
|
||||
ShaderPropertyType.Texture2DArray => "TEXTURE2D_ARRAY",
|
||||
ShaderPropertyType.TextureCubeArray => "TEXTURECUBE_ARRAY",
|
||||
ShaderPropertyType.Sampler => "SAMPLER",
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(type), $"Unsupported shader property type: {type}")
|
||||
};
|
||||
}
|
||||
|
||||
public static Result<string> GenerateShaderCode(ShaderDescriptor descriptor, string targetDirectory)
|
||||
public static Result<string> GenerateShaderCode(ShaderDescriptor descriptor, string targetDirectory, string globalDataPath)
|
||||
{
|
||||
if (!Directory.Exists(targetDirectory))
|
||||
{
|
||||
@@ -329,7 +330,8 @@ internal static class SDLCompiler
|
||||
#ifndef {fileDefine}
|
||||
#define {fileDefine}
|
||||
|
||||
#include ""F:/csharp/GhostEngine/Ghost.Shader/BuiltIn/Common.hlsl""");
|
||||
#include ""F:/csharp/GhostEngine/Ghost.Shader/BuiltIn/Common.hlsl""
|
||||
#include ""{globalDataPath}""");
|
||||
|
||||
sb.Append(@"
|
||||
struct PerMaterialData
|
||||
|
||||
@@ -176,6 +176,8 @@ internal static class TokenLexicon
|
||||
public const string TEXTURE3D = "tex3d";
|
||||
public const string TEXTURECUBE = "texcube";
|
||||
public const string TEXTURECUBE_ARRAY = "texcube_arr";
|
||||
|
||||
public const string SAMPLER = "sampler";
|
||||
}
|
||||
|
||||
public static class KnownTextureValue
|
||||
@@ -218,6 +220,7 @@ internal static class TokenLexicon
|
||||
KnownTypes.BOOL, KnownTypes.BOOL2, KnownTypes.BOOL3, KnownTypes.BOOL4,
|
||||
KnownTypes.TEXTURE2D, KnownTypes.TEXTURE2D_ARRAY, KnownTypes.TEXTURE3D,
|
||||
KnownTypes.TEXTURECUBE, KnownTypes.TEXTURECUBE_ARRAY,
|
||||
KnownTypes.SAMPLER,
|
||||
};
|
||||
|
||||
private static readonly HashSet<string> s_textureDefaultValues = new()
|
||||
|
||||
Reference in New Issue
Block a user