rj-action-library/Runtime/Rendering/RenderGraph/RDShaderProcessor.cs

114 lines
3.1 KiB
C#
Raw Normal View History

2025-04-23 12:00:43 +00:00
using Godot;
using System.Collections.Generic;
namespace Rokojori
{
2025-04-26 20:04:11 +00:00
public class RDShaderProcessor:RGGraphProcessor
2025-04-23 12:00:43 +00:00
{
protected string _shaderPath;
2025-04-26 20:04:11 +00:00
public RDShaderProcessor( RDGraph graph, string shaderPath ):base( graph )
2025-04-23 12:00:43 +00:00
{
this._shaderPath = shaderPath;
}
protected RDShader _shader;
protected RDPipeline _pipeline;
2025-04-25 08:13:22 +00:00
protected RDPushConstants _constants = new RDPushConstants();
2025-04-24 13:39:00 +00:00
public RDPushConstants constants => _constants;
protected Vector3I _groupSize = new Vector3I( 8, 8, 0 );
protected List<CompositorEffectGraphTextureSlot> _textureSlots = new List<CompositorEffectGraphTextureSlot>();
2025-04-23 12:00:43 +00:00
protected override void OnInitialize()
{
2025-04-30 20:18:22 +00:00
graph.context.Verbose( GetType().Name, "Trying to load shader: ", _shaderPath );
2025-04-23 12:00:43 +00:00
if ( _shaderPath == null )
{
2025-04-30 20:18:22 +00:00
graph.context.Error( GetType().Name, "_shaderPath == null" );
2025-04-23 12:00:43 +00:00
return;
}
var glslFile = GD.Load<RDShaderFile>( _shaderPath );
if ( glslFile == null )
{
2025-04-30 20:18:22 +00:00
graph.context.Error( GetType().Name, "Couldn't load shader at path:", _shaderPath );
2025-04-23 12:00:43 +00:00
return;
}
2025-04-26 20:04:11 +00:00
_shader = RDShader.CreateFromSpirV( graph.context, glslFile.GetSpirV() );
2025-04-23 12:00:43 +00:00
if ( _shader == null )
{
2025-04-30 20:18:22 +00:00
graph.context.Error( GetType().Name, "Couldn't create shader from code, path:", _shaderPath );
2025-04-23 12:00:43 +00:00
return;
}
2025-04-26 20:04:11 +00:00
_pipeline = RDPipeline.Create( graph.context, _shader );
2025-04-23 12:00:43 +00:00
if ( _shader == null )
{
2025-04-30 20:18:22 +00:00
graph.context.Error( GetType().Name, "Couldn't create pipeline from compiled shader, path:", _shaderPath );
2025-04-23 12:00:43 +00:00
return;
}
2025-04-30 20:18:22 +00:00
graph.context.Verbose( GetType().Name, "Created shader at path: ", _shaderPath );
2025-04-23 12:00:43 +00:00
}
public override void Process()
{
var context = graph.context;
context.Clear();
2025-04-29 19:46:45 +00:00
context.CalculateSceneComputeGroups( _groupSize );
2025-04-23 12:00:43 +00:00
context.SetShaderAndPipeline( _shader, _pipeline );
2025-04-30 20:18:22 +00:00
context.Verbose( GetType().Name, "Setting texture uniforms", _textureSlots.Count );
int index = 0;
2025-04-24 13:39:00 +00:00
_textureSlots.ForEach( t =>
{
2025-04-30 20:18:22 +00:00
index++;
2025-04-24 13:39:00 +00:00
t.ResolveTexture();
2025-04-30 20:18:22 +00:00
var texture = t.GetTexture();
if ( texture == null || ! texture.rid.IsValid )
{
if ( texture == null )
{
context.Error( index, "Texture is null" );
}
else
{
context.Error( index, "Texture is not valid:", texture.rid );
}
graph.context.AssignMissingTexture( t.sampler );
return;
}
graph.context.AssignTexture( texture, t.sampler );
2025-04-24 13:39:00 +00:00
}
2025-04-25 08:13:22 +00:00
);
2025-04-30 20:18:22 +00:00
2025-04-23 12:00:43 +00:00
2025-04-25 08:13:22 +00:00
if ( _constants.size > 0 )
2025-04-30 20:18:22 +00:00
{
context.Verbose( GetType().Name, "Setting constants", _constants.size );
2025-04-25 08:13:22 +00:00
context.pushConstants = _constants;
2025-04-30 20:18:22 +00:00
}
else
{
context.Verbose( GetType().Name, "No constants" );
}
2025-04-23 12:00:43 +00:00
2025-04-30 20:18:22 +00:00
context.Verbose( GetType().Name, "Process Compute Program", _constants.size );
2025-04-26 20:04:11 +00:00
context.ProcessComputeProgram();
2025-04-23 12:00:43 +00:00
}
}
}