#Creating a computeshader asset with an

1 messages · Page 1 of 1 (latest)

rich gate
#

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
    }
}
fathom ermine
#

heh, wait for my library, I am working on a Syntax API like Roslyn but for HLSL and Shaderlab :P

rich gate
#

thats cool

#

please make it faster than roslyn 🙏

sharp storm
rich gate
#

tnx

fathom ermine
#

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.

rich gate
#

oh yeah, my translator currently parses c# into hlsl, only works for compute shaders for now

fathom ermine
#

what's your approach?

rich gate
#

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

fathom ermine
#

possibly similar to Shadergraph's approach

#

btw as to your question

#

Try ShaderUtil.CreateShaderAsset

rich gate
#

yes tnx that s mentallystable said too

#

what s gonna be your apraoch?

fathom ermine
#

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

rich gate
#

okay so you're building a syntax tree?

fathom ermine
#

yeah

#

similar to how Roslyn has it's SyntaxFactory.SomethingSomething(...)

rich gate
#

yeah I tried roslyn, but it was not that useful for me because the trees are like immutable

fathom ermine
#

but that's good, you just recreate nodes

#

or use utility functions like ReplaceNode()

#

the immutability part is actually quite good

rich gate
#

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

fathom ermine
#

roslyn is giving the API for writing C# only

#

I need the api for Hlsl and Shaderlab

rich gate
#

hm so you want to turn hlsl code into shaderlab code or the other way?

fathom ermine
#

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

rich gate
#

oh yeah I see that s a whole lot more complicated