mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
First working effects with new pipeline
This commit is contained in:
@@ -35,6 +35,11 @@ class CompiledShader
|
||||
private Dictionary<StringView, ReflectedConstantBuffer> _buffers = new .() ~ DeleteDictionaryAndValues!(_);
|
||||
private Dictionary<StringView, ReflectedTexture> _textures = new .() ~ DeleteDictionaryAndValues!(_);
|
||||
|
||||
public Dictionary<StringView, ReflectedConstantBuffer> ConstantBuffers => _buffers;
|
||||
public Dictionary<StringView, ReflectedTexture> Textures => _textures;
|
||||
|
||||
public extern Span<uint8> Blob {get;}
|
||||
|
||||
public void AddConstantBuffer(ReflectedConstantBuffer buffer)
|
||||
{
|
||||
_buffers.Add(buffer.Name, buffer);
|
||||
@@ -49,17 +54,28 @@ class CompiledShader
|
||||
class ReflectedConstantBuffer
|
||||
{
|
||||
private String _name ~ delete _;
|
||||
private String _engineBufferName ~ delete _;
|
||||
|
||||
public int Size;
|
||||
|
||||
public int BindPoint;
|
||||
|
||||
public StringView Name
|
||||
{
|
||||
get => _name;
|
||||
set => String.NewOrSet!(_name, value);
|
||||
}
|
||||
|
||||
public StringView EngineBufferName
|
||||
{
|
||||
get => _engineBufferName;
|
||||
set => String.NewOrSet!(_engineBufferName, value);
|
||||
}
|
||||
|
||||
private Dictionary<StringView, ReflectedConstantBufferVariable> _variables = new .() ~ DeleteDictionaryAndValues!(_);
|
||||
|
||||
public Dictionary<StringView, ReflectedConstantBufferVariable> Variables => _variables;
|
||||
|
||||
public void AddVariable(ReflectedConstantBufferVariable vaiable)
|
||||
{
|
||||
_variables.Add(vaiable.Name, vaiable);
|
||||
@@ -115,6 +131,8 @@ class ReflectedConstantBufferVariable
|
||||
extension CompiledShader
|
||||
{
|
||||
internal ID3DBlob* _shaderBlob;
|
||||
|
||||
public override Span<uint8> Blob => Span<uint8>((uint8*)_shaderBlob.GetBufferPointer(), (int)_shaderBlob.GetBufferSize());
|
||||
}
|
||||
|
||||
class ShaderCompiler
|
||||
@@ -226,6 +244,7 @@ extension ShaderCompiler
|
||||
switch(bindDesc.Type)
|
||||
{
|
||||
case .ConstantBuffer:
|
||||
//var bufferReflection = reflection.GetConstantBufferByName(bindDesc.Name);
|
||||
var bufferReflection = reflection.GetConstantBufferByName(bindDesc.Name);
|
||||
|
||||
bufferReflection.GetDescription(let bufferDesc);
|
||||
@@ -233,7 +252,7 @@ extension ShaderCompiler
|
||||
// ConstantBuffer
|
||||
if(bufferDesc.Type == .D3D11_CT_CBUFFER)
|
||||
{
|
||||
ReflectedConstantBuffer cbuffer = Try!(ReflectConstantBuffer(bufferReflection));
|
||||
ReflectedConstantBuffer cbuffer = Try!(ReflectConstantBuffer(bindDesc, bufferReflection));
|
||||
shader.AddConstantBuffer(cbuffer);
|
||||
}
|
||||
case .Texture:
|
||||
@@ -261,8 +280,7 @@ extension ShaderCompiler
|
||||
|
||||
shader.AddTexture(new ReflectedTexture(StringView(bindDesc.Name), bindDesc.BindPoint, textureDimension));
|
||||
case .Sampler:
|
||||
// TODO: do we have to do something for samplers?
|
||||
// i.e. can we get default values?
|
||||
// There is nothing to do for samplers
|
||||
default:
|
||||
Log.EngineLogger.Warning($"Unhandled shader resource type: \"{bindDesc.Type}\"");
|
||||
}
|
||||
@@ -271,7 +289,7 @@ extension ShaderCompiler
|
||||
return .Ok;
|
||||
}
|
||||
|
||||
private static Result<ReflectedConstantBuffer> ReflectConstantBuffer(ID3D11ShaderReflectionConstantBuffer* bufferReflection)
|
||||
private static Result<ReflectedConstantBuffer> ReflectConstantBuffer(ShaderInputBindDescription bindDesc, ID3D11ShaderReflectionConstantBuffer* bufferReflection)
|
||||
{
|
||||
Debug.Profiler.ProfileResourceFunction!();
|
||||
|
||||
@@ -284,6 +302,7 @@ extension ShaderCompiler
|
||||
|
||||
buffer.Name = StringView(bufferDescription.Name);
|
||||
buffer.Size = bufferDescription.Size;
|
||||
buffer.BindPoint = bindDesc.BindPoint;
|
||||
buffer.RawData = new uint8[buffer.Size];
|
||||
|
||||
for(uint32 v = 0; v < bufferDescription.Variables; v++)
|
||||
|
||||
@@ -10,15 +10,60 @@ namespace GlitchyEditor.Assets.Processors;
|
||||
|
||||
class ProcessedShader : ProcessedResource
|
||||
{
|
||||
public struct TextureEntry
|
||||
{
|
||||
public StringView Name;
|
||||
public TextureDimension TextureDimension;
|
||||
public int32 VertexShaderBindPoint;
|
||||
public int32 PixelShaderBindPoint;
|
||||
|
||||
public static readonly TextureEntry Default = .() {
|
||||
Name = null,
|
||||
TextureDimension = .Unknown,
|
||||
VertexShaderBindPoint = -1,
|
||||
PixelShaderBindPoint = -1
|
||||
};
|
||||
}
|
||||
|
||||
public struct ConstantBufferEntry
|
||||
{
|
||||
public ReflectedConstantBuffer ConstantBuffer;
|
||||
public int32 VertexShaderBindPoint;
|
||||
public int32 PixelShaderBindPoint;
|
||||
|
||||
public static readonly Self Default = .() {
|
||||
ConstantBuffer = null,
|
||||
VertexShaderBindPoint = -1,
|
||||
PixelShaderBindPoint = -1,
|
||||
};
|
||||
}
|
||||
|
||||
public override AssetType AssetType => .Shader;
|
||||
|
||||
public CompiledShader VertexShader ~ delete _;
|
||||
public CompiledShader PixelShader ~ delete _;
|
||||
|
||||
Dictionary<StringView, ConstantBufferEntry> _constantBuffers = new .() ~ delete _; // Only delete container, Buffers come from shaders
|
||||
|
||||
Dictionary<StringView, TextureEntry> _textures = new .() ~ delete _;
|
||||
|
||||
public Dictionary<StringView, ConstantBufferEntry> ConstantBuffers => _constantBuffers;
|
||||
public Dictionary<StringView, TextureEntry> Textures => _textures;
|
||||
|
||||
public this(AssetIdentifier ownAssetIdentifier, AssetHandle assetHandle) : base(ownAssetIdentifier, assetHandle)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void AddConstantBuffer(ConstantBufferEntry buffer)
|
||||
{
|
||||
_constantBuffers.Add(buffer.ConstantBuffer.Name, buffer);
|
||||
}
|
||||
|
||||
public void AddTextureEntry(TextureEntry textureEntry)
|
||||
{
|
||||
_textures.Add(textureEntry.Name, textureEntry);
|
||||
}
|
||||
}
|
||||
|
||||
class ShaderVariable
|
||||
@@ -72,23 +117,20 @@ class ShaderProcessor : IAssetProcessor
|
||||
String psName = scope String();
|
||||
|
||||
Dictionary<StringView, ShaderVariable> variables = scope .();
|
||||
Dictionary<String, String> engineBuffers = scope .();
|
||||
List<String> bufferNames = scope .();
|
||||
// Name in Shader -> Name in Engine
|
||||
Dictionary<StringView, StringView> engineBuffers = scope .();
|
||||
|
||||
defer
|
||||
{
|
||||
ClearDictionaryAndDeleteValues!(variables);
|
||||
|
||||
for (let (key, value) in engineBuffers)
|
||||
{
|
||||
delete key;
|
||||
delete value;
|
||||
}
|
||||
ClearAndDeleteItems!(bufferNames);
|
||||
}
|
||||
|
||||
String code = new String(importedShader.HlslCode);
|
||||
defer { delete code; }
|
||||
|
||||
Try!(ProcessFileContent(code, vsName, psName, variables, engineBuffers));
|
||||
Try!(ProcessFileContent(code, vsName, psName, variables, bufferNames, engineBuffers));
|
||||
|
||||
if (String.IsNullOrWhiteSpace(vsName) && String.IsNullOrWhiteSpace(psName))
|
||||
{
|
||||
@@ -100,16 +142,108 @@ class ShaderProcessor : IAssetProcessor
|
||||
|
||||
Try!(CompileAndReflect(vsName, psName, importedShader, code, processedShader));
|
||||
|
||||
Try!(MergeResources(processedShader));
|
||||
Try!(MergeResources(processedShader, variables, engineBuffers));
|
||||
|
||||
outProcessedResources.Add(processedShader);
|
||||
|
||||
return .Ok;
|
||||
}
|
||||
|
||||
private static Result<void> MergeResources(ProcessedShader processedShader)
|
||||
private static Result<void> MergeResources(ProcessedShader processedShader,
|
||||
Dictionary<StringView, ShaderVariable> variables,
|
||||
Dictionary<StringView, StringView> engineBuffers)
|
||||
{
|
||||
Try!(MergeConstantBuffers(processedShader, variables, engineBuffers));
|
||||
Try!(MergeTextures(processedShader));
|
||||
|
||||
return .Ok;
|
||||
}
|
||||
|
||||
private static Result<void> MergeConstantBuffers(ProcessedShader processedShader,
|
||||
Dictionary<StringView, ShaderVariable> variables,
|
||||
Dictionary<StringView, StringView> engineBuffers)
|
||||
{
|
||||
HashSet<StringView> bufferNames = scope .();
|
||||
|
||||
void AddBufferNames(CompiledShader shader)
|
||||
{
|
||||
for (StringView name in shader.ConstantBuffers.Keys)
|
||||
{
|
||||
bufferNames.Add(name);
|
||||
}
|
||||
}
|
||||
|
||||
AddBufferNames(processedShader.VertexShader);
|
||||
AddBufferNames(processedShader.PixelShader);
|
||||
|
||||
for (StringView bufferName in bufferNames)
|
||||
{
|
||||
Result<ReflectedConstantBuffer> vsBufferResult = processedShader.VertexShader.ConstantBuffers.GetValue(bufferName);
|
||||
Result<ReflectedConstantBuffer> psBufferResult = processedShader.PixelShader.ConstantBuffers.GetValue(bufferName);
|
||||
|
||||
ProcessedShader.ConstantBufferEntry constantBufferEntry = .Default;
|
||||
|
||||
if (vsBufferResult case .Ok(let vsBuffer) && psBufferResult case .Ok(let psBuffer))
|
||||
{
|
||||
// Choose larger buffer
|
||||
constantBufferEntry.ConstantBuffer = (vsBuffer.Size >= psBuffer.Size) ? vsBuffer : psBuffer;
|
||||
constantBufferEntry.VertexShaderBindPoint = (.)vsBuffer.BindPoint;
|
||||
constantBufferEntry.PixelShaderBindPoint = (.)psBuffer.BindPoint;
|
||||
}
|
||||
else if (vsBufferResult case .Ok(let vsBuffer))
|
||||
{
|
||||
constantBufferEntry.ConstantBuffer = vsBuffer;
|
||||
constantBufferEntry.VertexShaderBindPoint = (.)vsBuffer.BindPoint;
|
||||
}
|
||||
else if (psBufferResult case .Ok(let psBuffer))
|
||||
{
|
||||
constantBufferEntry.ConstantBuffer = psBuffer;
|
||||
constantBufferEntry.PixelShaderBindPoint = (.)psBuffer.BindPoint;
|
||||
}
|
||||
|
||||
Log.EngineLogger.Assert(constantBufferEntry.ConstantBuffer != null);
|
||||
Log.EngineLogger.Assert(constantBufferEntry.VertexShaderBindPoint > -1 || constantBufferEntry.PixelShaderBindPoint > -1);
|
||||
|
||||
if (engineBuffers.TryGetValue(constantBufferEntry.ConstantBuffer.Name, let engineBufferName))
|
||||
{
|
||||
constantBufferEntry.ConstantBuffer.EngineBufferName = engineBufferName;
|
||||
}
|
||||
|
||||
processedShader.AddConstantBuffer(constantBufferEntry);
|
||||
}
|
||||
|
||||
return .Ok;
|
||||
}
|
||||
|
||||
static ref ProcessedShader.TextureEntry AddOrGetTextureEntry(ProcessedShader processedShader, ReflectedTexture reflectedTexture)
|
||||
{
|
||||
if (!processedShader.Textures.ContainsKey(reflectedTexture.Name))
|
||||
{
|
||||
ProcessedShader.TextureEntry textureEntry = .Default;
|
||||
textureEntry.Name = reflectedTexture.Name;
|
||||
textureEntry.TextureDimension = reflectedTexture.TextureDimension;
|
||||
|
||||
processedShader.Textures.Add(textureEntry.Name, textureEntry);
|
||||
}
|
||||
|
||||
ref ProcessedShader.TextureEntry entry = ref processedShader.Textures[reflectedTexture.Name];
|
||||
|
||||
return ref entry;
|
||||
}
|
||||
|
||||
private static Result<void> MergeTextures(ProcessedShader processedShader)
|
||||
{
|
||||
for (let (textureName, reflectedTexture) in processedShader.VertexShader.Textures)
|
||||
{
|
||||
ref ProcessedShader.TextureEntry textureEntry = ref AddOrGetTextureEntry(processedShader, reflectedTexture);
|
||||
textureEntry.VertexShaderBindPoint = (int32)reflectedTexture.BindPoint;
|
||||
}
|
||||
|
||||
for (let (textureName, reflectedTexture) in processedShader.PixelShader.Textures)
|
||||
{
|
||||
ref ProcessedShader.TextureEntry textureEntry = ref AddOrGetTextureEntry(processedShader, reflectedTexture);
|
||||
textureEntry.PixelShaderBindPoint = (int32)reflectedTexture.BindPoint;
|
||||
}
|
||||
|
||||
return .Ok;
|
||||
}
|
||||
@@ -143,7 +277,9 @@ class ShaderProcessor : IAssetProcessor
|
||||
return ShaderCompiler.CompileAndReflectShader(code, importedShader.AssetIdentifier, vsName, "ps_5_0", .());
|
||||
}
|
||||
|
||||
private static Result<void> ProcessFileContent(String fileContent, String outVsName, String outPsName, Dictionary<StringView, ShaderVariable> outVarDescs, Dictionary<String, String> outEngineBuffers)
|
||||
private static Result<void> ProcessFileContent(String fileContent, String outVsName, String outPsName,
|
||||
Dictionary<StringView, ShaderVariable> outVarDescs,
|
||||
List<String> outBufferNames, Dictionary<StringView, StringView> outEngineBuffers)
|
||||
{
|
||||
Debug.Profiler.ProfileResourceFunction!();
|
||||
|
||||
@@ -176,7 +312,7 @@ class ShaderProcessor : IAssetProcessor
|
||||
case "EditorVariable":
|
||||
Try!(ProcessEditorVariables(arguments, outVarDescs));
|
||||
case "EngineBuffer":
|
||||
Try!(ProcessEngineBuffer(arguments, outEngineBuffers));
|
||||
Try!(ProcessEngineBuffer(arguments, outBufferNames, outEngineBuffers));
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
@@ -273,7 +409,8 @@ class ShaderProcessor : IAssetProcessor
|
||||
return .Ok((startOfLine, endOfLine));
|
||||
}
|
||||
|
||||
private static Result<void> ProcessEngineBuffer(Dictionary<StringView, StringView> arguments, Dictionary<String, String> outEngineBuffers)
|
||||
private static Result<void> ProcessEngineBuffer(Dictionary<StringView, StringView> arguments,
|
||||
List<String> outBufferNames, Dictionary<StringView, StringView> outEngineBuffers)
|
||||
{
|
||||
String nameInEngine = null;
|
||||
String nameInShader = null;
|
||||
@@ -306,7 +443,9 @@ class ShaderProcessor : IAssetProcessor
|
||||
return .Err;
|
||||
}
|
||||
|
||||
outEngineBuffers.Add(nameInEngine, nameInShader);
|
||||
outBufferNames.Add(nameInShader);
|
||||
outBufferNames.Add(nameInEngine);
|
||||
outEngineBuffers.Add(nameInShader, nameInEngine);
|
||||
|
||||
return .Ok;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user