#Creating a computeshader asset with an
1 messages · Page 1 of 1 (latest)
I wrote a c# to hlsl translator for compute shaders, for now I have a string for the compute shader but when I create a .compute file it doesn t get recognized as a compute shader,
How do you create a compute shader asset via code in unity?
This is my current attempt
public class ComputeShaderAssetFactory : MonoBehaviour
{
public string ComputeShaderFolder = "Resources/AutogeneratedScripts";
public void CreateComputeShaderAsset(string Name, string Content)
{
if (!Application.isEditor)
{
Debug.LogError("This function can only be used in the Unity Editor.");
return;
}
string relativeAssetPath = Path.Combine(ComputeShaderFolder, Name + ".compute");
string fullPath = Path.Combine(Application.dataPath, relativeAssetPath);
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
File.WriteAllText(fullPath, Content);
AssetDatabase.Refresh();
Debug.Log("Compute shader asset created at " + relativeAssetPath);
//the file gets created but is not recognized by unity as a compute shader
}
}
heh, wait for my library, I am working on a Syntax API like Roslyn but for HLSL and Shaderlab :P
@rich gate I haven't done this before, but this looks like it should be useful:
https://docs.unity3d.com/ScriptReference/ShaderUtil.CreateComputeShaderAsset.html
tnx
btw I misunderstood your question :D I was thinking about writing shader code... programatically :P not the shader asset
ideally I'd like to combine it with parts of HlslTools by tgjones, because he has whole compiler and whatnot for shaderlab + hlsl along with analyzers etc.
oh yeah, my translator currently parses c# into hlsl, only works for compute shaders for now
what's your approach?
that sounds pretty dope,
my aproach is just
using the attributes to find "HlslContainers"
in which you can define structs/functions which will be translated into hlsl
I make a tree structure of struct and function names which I later replace words in the funcctions, it s pretty damn basic I'd say
possibly similar to Shadergraph's approach
btw as to your question
Try ShaderUtil.CreateShaderAsset
I wrote a bunch of records like FunctionDeclarationSyntax etc, they are created with calls like
new Type.Struct
{
name = SdfResultStructName,
members = new[]
{
new Type.Struct.Member { type = new IntKeyword(), id = SdfIdMemberName },
new Type.Struct.Member
{
type = new VectorToken { type = new FloatKeyword(), arity = 3 },
id = SdfPointMemberName
},
new Type.Struct.Member { type = new FloatKeyword(), id = SdfDistanceMemberName },
}
};
I also wrote a source generator for now that handles creating utility functions in partial records for writing the syntax
the syntax parts represents some AST constructs of the HLSL/Shaderlab syntax
okay so you're building a syntax tree?
yeah I tried roslyn, but it was not that useful for me because the trees are like immutable
but that's good, you just recreate nodes
or use utility functions like ReplaceNode()
the immutability part is actually quite good
yeah I didn t give it such a close look sadly, why arn t you just using roslyn for the syntax tree?
, if you know the syntax so well
roslyn is giving the API for writing C# only
I need the api for Hlsl and Shaderlab
hm so you want to turn hlsl code into shaderlab code or the other way?
no no, I am writing a set of C# scripts that allow to write hlsl and shaderlab code from code, no transpiling and any kind of translation. Just creating program content using a nice API instead of concatenating a bunch of strings with a whole lot of if/elses
take a look at roslyn syntax trees api and you should understand what I mean
oh yeah I see that s a whole lot more complicated