Exemplo n.º 1
0
 /// <summary>
 /// Appends the discovered methods for the current method.
 /// </summary>
 /// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
 /// <param name="mapping">The mapping of already discovered type names.</param>
 public void AppendMethods(ref ArrayPoolStringBuilder builder, HashSet <string> mapping)
 {
     foreach (KeyValuePair <string, string> method in this.methods)
     {
         if (mapping.Remove(method.Key))
         {
             builder.Append('\n');
             builder.Append(method.Value);
             builder.Append('\n');
         }
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Appends the discovered forward declarations for the current method.
 /// </summary>
 /// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
 /// <param name="mapping">The mapping of already discovered type names.</param>
 public void AppendForwardDeclarations(ref ArrayPoolStringBuilder builder, HashSet <string> mapping)
 {
     foreach (KeyValuePair <string, string> method in this.methods)
     {
         if (mapping.Add(method.Key))
         {
             builder.Append('\n');
             builder.Append(method.Key);
             builder.Append('\n');
         }
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Appends the discovered types for the current method.
 /// </summary>
 /// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
 /// <param name="mapping">The mapping of already discovered type names.</param>
 public void AppendTypes(ref ArrayPoolStringBuilder builder, HashSet <string> mapping)
 {
     foreach (KeyValuePair <string, string> type in this.types)
     {
         if (mapping.Add(type.Key))
         {
             builder.Append('\n');
             builder.Append(type.Value);
             builder.Append('\n');
         }
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Appends the mapped source code for the current method.
 /// </summary>
 /// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
 /// <param name="mapping">The mapping of already discovered constant names.</param>
 public void AppendConstants(ref ArrayPoolStringBuilder builder, HashSet <string> mapping)
 {
     foreach (KeyValuePair <string, string> constant in this.constants)
     {
         if (mapping.Add(constant.Key))
         {
             builder.Append("#define ");
             builder.Append(constant.Key);
             builder.Append(' ');
             builder.Append(constant.Value);
             builder.Append('\n');
         }
     }
 }
Exemplo n.º 5
0
 Unsafe.AsRef(in shader).BuildHlslString(out ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ);
Exemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DebugView"/> class with the specified parameters.
 /// </summary>
 /// <param name="builder">The input <see cref="ArrayPoolStringBuilder"/> instance to display.</param>
 public DebugView(ArrayPoolStringBuilder builder)
 {
     Text = builder.WrittenSpan.ToString();
 }
Exemplo n.º 7
0
        public static ArrayPoolStringBuilder Render(int threadsX, int threadsY, int threadsZ, IShaderLoader info)
        {
            var builder = ArrayPoolStringBuilder.Create();

            // Header
            builder.AppendLine("// ================================================");
            builder.AppendLine("//                  AUTO GENERATED");
            builder.AppendLine("// ================================================");
            builder.AppendLine("// This shader was created by ComputeSharp.");
            builder.AppendLine("// See: https://github.com/Sergio0694/ComputeSharp.");

            // Group size constants
            builder.AppendLine();
            builder.Append("#define __GroupSize__get_X ");
            builder.AppendLine(threadsX.ToString());
            builder.Append("#define __GroupSize__get_Y ");
            builder.AppendLine(threadsY.ToString());
            builder.Append("#define __GroupSize__get_Z ");
            builder.AppendLine(threadsZ.ToString());

            // Define declarations
            foreach (var define in info.DefinesInfo)
            {
                builder.Append("#define ");
                builder.Append(define.Key);
                builder.Append(' ');
                builder.AppendLine(define.Value);
            }

            // Static fields
            if (info.StaticFields.Count > 0)
            {
                builder.AppendLine();

                foreach (var constant in info.StaticFields)
                {
                    builder.Append(constant.Value.TypeDeclaration);
                    builder.Append(' ');
                    builder.Append(constant.Key);

                    if (constant.Value.Assignment is string assignment)
                    {
                        builder.Append(" = ");
                        builder.Append(assignment);
                    }

                    builder.AppendLine(";");
                }
            }

            // Declared types
            foreach (var type in info.DeclaredTypes)
            {
                builder.AppendLine();
                builder.AppendLine(type);
            }

            // Captured variables
            builder.AppendLine();
            builder.AppendLine("cbuffer _ : register(b0)");
            builder.AppendLine('{');
            builder.AppendLine("    uint __x;");
            builder.AppendLine("    uint __y;");
            builder.AppendLine("    uint __z;");

            foreach (var local in info.FieldsInfo)
            {
                builder.Append("    ");
                builder.Append(local.FieldType);
                builder.Append(' ');
                builder.Append(local.FieldName);
                builder.AppendLine(';');
            }

            builder.AppendLine('}');

            // Buffers
            foreach (var buffer in info.HlslResourceInfo)
            {
                builder.AppendLine();

                switch (buffer)
                {
                // Constant buffer go to cbuffer fields with a dummy local
                case HlslResourceInfo.Constant _:
                    builder.Append("cbuffer _");
                    builder.Append(buffer.FieldName);
                    builder.Append(" : register(b");
                    builder.Append(buffer.BufferIndex.ToString());
                    builder.AppendLine(')');
                    builder.AppendLine('{');
                    builder.Append("    ");
                    builder.Append(buffer.FieldType);
                    builder.Append(' ');
                    builder.Append(buffer.FieldName);
                    builder.AppendLine("[2];");
                    builder.AppendLine('}');
                    break;

                // Structured buffer have the same syntax but different id
                case HlslResourceInfo.ReadOnly _:
                    char registerId = 't';
                    goto StructuredBuffer;

                case HlslResourceInfo.ReadWrite _:
                    registerId = 'u';
StructuredBuffer:
                    builder.Append(buffer.FieldType);
                    builder.Append(' ');
                    builder.Append(buffer.FieldName);
                    builder.Append(" : register(");
                    builder.Append(registerId);
                    builder.Append(buffer.BufferIndex.ToString());
                    builder.AppendLine(");");
                    break;
                }
            }

            // Shared buffers
            foreach (var buffer in info.SharedBuffers)
            {
                builder.AppendLine();
                builder.Append("groupshared ");
                builder.Append(buffer.Value.Type);
                builder.Append(' ');
                builder.Append(buffer.Key);
                builder.Append('[');

                if (buffer.Value.Count is int count)
                {
                    builder.Append(count.ToString());
                }
                else
                {
                    builder.Append((threadsX * threadsY * threadsZ).ToString());
                }

                builder.AppendLine("];");
            }

            // Forward declarations
            foreach (var declaration in info.ForwardDeclarations)
            {
                builder.AppendLine();
                builder.AppendLine(declaration);
            }

            // Captured methods
            foreach (var function in info.MethodsInfo)
            {
                builder.AppendLine();
                builder.AppendLine(function);
            }

            // Entry point
            builder.AppendLine();
            builder.AppendLine("[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]");
            builder.AppendLine(info.EntryPoint);

            return(builder);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Appends the mapped source code for the current method.
 /// </summary>
 /// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
 /// <param name="name">The name to bind the method to.</param>
 public void AppendMappedInvokeMethod(ref ArrayPoolStringBuilder builder, string name)
 {
     builder.Append(this.invokeMethod.Replace(InvokeMethodIdentifier, name));
 }