Beispiel #1
0
 internal VertexFragmentCompilationResult(
     string vertexCode,
     string fragmentCode,
     SpirvReflection reflection)
 {
     VertexShader   = vertexCode;
     FragmentShader = fragmentCode;
     Reflection     = reflection;
 }
        public static void BlobToShader(
            byte[] shaderBytes,
            out SpirvReflection reflection,
            out byte[][] shaderBytecode)
        {
            // FILE STRUCTURE

            // INT HEADER SIZE
            // BYTE[] HEADER => BSON DESERIALIZE INTO VESSEL.SHADERMETADATA ; includes reflection data

            // INT ENTRIES
            // INT [] SIZES

            // STAGE ARRAY {
            //
            //		BYTE[] DATA  =>  a single shader program
            //
            // }

            // Read the data

            // Header length
            int headerLen = ReadInt32(shaderBytes, 0);

            // Isolate the reflection data to a byte[]
            byte[] reflData = new byte[headerLen];
            Array.Copy(shaderBytes, 4, reflData, 0, headerLen);

            // Read the amount of entries in the shader
            int entryCount = ReadInt32(shaderBytes, headerLen + 4);
            int arrPos     = headerLen + 8;

            // Create a buffer for shader bytecode
            shaderBytecode = new byte[entryCount][];

            // Read the SPIRV bytecode
            for (int i = 0; i < entryCount; i++)
            {
                int bytecodeSize = ReadInt32(shaderBytes, arrPos);
                arrPos           += 4;
                shaderBytecode[i] = new byte[bytecodeSize];
                Array.Copy(shaderBytes, arrPos, shaderBytecode[i], 0, bytecodeSize);
                arrPos += bytecodeSize;
            }

            // Read the BSON Reflection data into a SPIRV Reflection object
            using (MemoryStream mem = new MemoryStream(reflData))
            {
                using (BsonReader jtr = new BsonReader(mem))
                {
                    reflection = s_serializer.Value.Deserialize <SpirvReflection>(jtr);
                }
            }
        }
        public void SpirvReflection_DeserializeFromString()
        {
            const string SerializedJson =
                @"{
  ""VertexElements"": [],
  ""ResourceLayouts"": [
    {
      ""Elements"": [
        {
          ""Name"": ""vdspv_0_0"",
          ""Kind"": ""TextureReadOnly"",
          ""Stages"": ""Fragment"",
          ""Options"": ""None""
        },
        {
          ""Name"": ""vdspv_0_1"",
          ""Kind"": ""Sampler"",
          ""Stages"": ""Fragment"",
          ""Options"": ""None""
        }
      ]
    },
    {
      ""Elements"": [
        {
          ""Name"": ""vdspv_1_0"",
          ""Kind"": ""UniformBuffer"",
          ""Stages"": ""Fragment"",
          ""Options"": ""None""
        }
      ]
    }
  ]
}";

            byte[] bytes = Encoding.UTF8.GetBytes(SerializedJson);
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                SpirvReflection refl = SpirvReflection.LoadFromJson(ms);
                Assert.Empty(refl.VertexElements);
                Assert.Equal(2, refl.ResourceLayouts.Length);
                Assert.Equal(2, refl.ResourceLayouts[0].Elements.Length);
                Assert.Single(refl.ResourceLayouts[1].Elements);
            }
        }
Beispiel #4
0
        private void WriteShaderBinary(
            string file,
            byte[][] result,
            SpirvReflection reflectionData)
        {
            // Resolve absolute path
            file = Path.Combine(_outputPath, file);

            // FILE STRUCTURE

            // INT HEADER SIZE
            // BYTE[] HEADER => BSON DESERIALIZE INTO VESSEL.SHADERMETADATA ; includes reflection data

            // INT ENTRIES
            // INT [] SIZES

            // STAGE ARRAY {
            //
            //		BYTE[] DATA  =>  a single shader program
            //
            // }

            int streamPos = 0;

            using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Formatting = Formatting.Indented;
                StringEnumConverter enumConverter = new StringEnumConverter();
                serializer.Converters.Add(enumConverter);
                using (var mem = new MemoryStream())
                {
                    using (BsonWriter jtw = new BsonWriter(mem))
                    {
                        serializer.Serialize(jtw, reflectionData);
                    }

                    var memArr = mem.ToArray();

                    // meta length
                    streamPos += fs.Write((int)memArr.Length);

                    // data
                    //mem.WriteTo(fs);
                    fs.Write(memArr);

                    streamPos += (int)memArr.Length;
                }

                // entries
                streamPos += fs.Write(result.Length);

                // write the compiled shader code to the binary
                for (int i = 0; i < result.Length; i++)
                {
                    // length
                    streamPos += fs.Write(result[i].Length);

                    // bytecode
                    fs.Write(result[i]);
                    streamPos += result[i].Length;
                }

                fs.Flush();
            }
        }
 internal ComputeCompilationResult(string computeCode, SpirvReflection reflection)
 {
     ComputeShader = computeCode;
     Reflection    = reflection;
 }