Example #1
0
 public ConstantBuffer(IAssetsFile assetsFile, string name, MatrixParameter[] matrices, VectorParameter[] vectors, int usedSize) :
     this(assetsFile)
 {
     Name           = name;
     NameIndex      = -1;
     m_matrixParams = matrices;
     m_vectorParams = vectors;
     if (IsReadStructParams)
     {
         m_structParams = new StructParameter[0];
     }
     Size = usedSize;
 }
Example #2
0
        public void Read(EndianStream stream)
        {
            int magic = stream.ReadInt32();

            if (magic != MagicNumber)
            {
                throw new Exception($"Magic number {magic} doesn't match");
            }

            ProgramType = (ShaderGpuProgramType)stream.ReadInt32();
            int unknown1 = stream.ReadInt32();
            int unknown2 = stream.ReadInt32();
            int unknown3 = stream.ReadInt32();

            if (IsReadUnknown4)
            {
                int unknown4 = stream.ReadInt32();
            }

            m_keywords    = stream.ReadStringArray();
            m_programData = stream.ReadByteArray();
            stream.AlignStream(AlignType.Align4);

            int sourceMap = stream.ReadInt32();
            int bindCount = stream.ReadInt32();

            ShaderBindChannel[] channels = new ShaderBindChannel[bindCount];
            for (int i = 0; i < bindCount; i++)
            {
                ShaderChannel     source  = (ShaderChannel)stream.ReadUInt32();
                VertexComponent   target  = (VertexComponent)stream.ReadUInt32();
                ShaderBindChannel channel = new ShaderBindChannel(source, target);
                channels[i] = channel;
                sourceMap  |= 1 << (int)source;
            }
            m_bindChannels = new ParserBindChannels(channels, sourceMap);

            List <VectorParameter>  vectors       = new List <VectorParameter>();
            List <MatrixParameter>  matrices      = new List <MatrixParameter>();
            List <TextureParameter> textures      = new List <TextureParameter>();
            List <BufferBinding>    buffers       = new List <BufferBinding>();
            List <UAVParameter>     uavs          = IsReadUAVParameters ? new List <UAVParameter>() : null;
            List <SamplerParameter> samplers      = IsReadSamplerParameters ? new List <SamplerParameter>() : null;
            List <BufferBinding>    constBindings = new List <BufferBinding>();
            List <StructParameter>  structs       = IsReadStructParameters ? new List <StructParameter>() : null;

            int paramGroupCount = stream.ReadInt32();

            m_constantBuffers = new ConstantBuffer[paramGroupCount - 1];
            for (int i = 0; i < paramGroupCount; i++)
            {
                vectors.Clear();
                matrices.Clear();

                string name       = stream.ReadStringAligned();
                int    usedSize   = stream.ReadInt32();
                int    paramCount = stream.ReadInt32();
                for (int j = 0; j < paramCount; j++)
                {
                    string          paramName = stream.ReadStringAligned();
                    ShaderParamType paramType = (ShaderParamType)stream.ReadInt32();
                    int             rows      = stream.ReadInt32();
                    int             dimension = stream.ReadInt32();
                    bool            isMatrix  = stream.ReadInt32() > 0;
                    int             arraySize = stream.ReadInt32();
                    int             index     = stream.ReadInt32();

                    if (isMatrix)
                    {
                        MatrixParameter matrix = IsAllParamArgs ?
                                                 new MatrixParameter(paramName, paramType, index, arraySize, rows) :
                                                 new MatrixParameter(paramName, paramType, index, rows);
                        matrices.Add(matrix);
                    }
                    else
                    {
                        VectorParameter vector = IsAllParamArgs ?
                                                 new VectorParameter(paramName, paramType, index, arraySize, dimension) :
                                                 new VectorParameter(paramName, paramType, index, dimension);
                        vectors.Add(vector);
                    }
                }

                if (i == 0)
                {
                    m_vectorParameters = vectors.ToArray();
                    m_matrixParameters = matrices.ToArray();
                }
                else
                {
                    ConstantBuffer constBuffer = new ConstantBuffer(m_assetsFile, name, matrices.ToArray(), vectors.ToArray(), usedSize);
                    m_constantBuffers[i - 1] = constBuffer;
                }

                if (IsReadStructParameters)
                {
                    int structCount = stream.ReadInt32();
                    for (int j = 0; j < structCount; j++)
                    {
                        vectors.Clear();
                        matrices.Clear();

                        string structName = stream.ReadStringAligned();
                        int    index      = stream.ReadInt32();
                        int    arraySize  = stream.ReadInt32();
                        int    structSize = stream.ReadInt32();

                        int strucParamCount = stream.ReadInt32();
                        for (int k = 0; k < strucParamCount; k++)
                        {
                            string paramName = stream.ReadStringAligned();
                            paramName = $"{structName}.{paramName}";
                            ShaderParamType paramType       = (ShaderParamType)stream.ReadInt32();
                            int             rows            = stream.ReadInt32();
                            int             dimension       = stream.ReadInt32();
                            bool            isMatrix        = stream.ReadInt32() > 0;
                            int             vectorArraySize = stream.ReadInt32();
                            int             paramIndex      = stream.ReadInt32();

                            if (isMatrix)
                            {
                                MatrixParameter matrix = IsAllParamArgs ?
                                                         new MatrixParameter(paramName, paramType, paramIndex, vectorArraySize, rows) :
                                                         new MatrixParameter(paramName, paramType, paramIndex, rows);
                                matrices.Add(matrix);
                            }
                            else
                            {
                                VectorParameter vector = IsAllParamArgs ?
                                                         new VectorParameter(paramName, paramType, paramIndex, vectorArraySize, dimension) :
                                                         new VectorParameter(paramName, paramType, paramIndex, dimension);
                                vectors.Add(vector);
                            }
                        }

                        StructParameter @struct = new StructParameter(structName, index, arraySize, structSize, vectors.ToArray(), matrices.ToArray());
                        structs.Add(@struct);
                    }
                }
            }

            int paramGroup2Count = stream.ReadInt32();

            for (int i = 0; i < paramGroup2Count; i++)
            {
                string name       = stream.ReadStringAligned();
                int    type       = stream.ReadInt32();
                int    index      = stream.ReadInt32();
                int    extraValue = stream.ReadInt32();

                if (type == 0)
                {
                    TextureParameter texture;
                    if (IsReadMultiSampled)
                    {
                        bool isMultiSampled = stream.ReadUInt32() > 0;
                        texture = new TextureParameter(m_assetsFile, name, index, isMultiSampled, extraValue);
                    }
                    else
                    {
                        texture = new TextureParameter(m_assetsFile, name, index, extraValue);
                    }
                    textures.Add(texture);
                }
                else if (type == 1)
                {
                    BufferBinding binding = new BufferBinding(name, index);
                    constBindings.Add(binding);
                }
                else if (type == 2)
                {
                    BufferBinding buffer = new BufferBinding(name, index);
                    buffers.Add(buffer);
                }
                else if (type == 3 && IsReadUAVParameters)
                {
                    UAVParameter uav = new UAVParameter(name, index, extraValue);
                    uavs.Add(uav);
                }
                else if (type == 4 && IsReadSamplerParameters)
                {
                    SamplerParameter sampler = new SamplerParameter((uint)extraValue, index);
                    samplers.Add(sampler);
                }
                else
                {
                    throw new Exception($"Unupported parameter type {type}");
                }
            }
            m_textureParameters = textures.ToArray();
            m_bufferParameters  = buffers.ToArray();
            if (IsReadUAVParameters)
            {
                m_UAVParameters = uavs.ToArray();
            }
            if (IsReadSamplerParameters)
            {
                m_samplerParameters = samplers.ToArray();
            }
            m_constantBufferBindings = constBindings.ToArray();
            if (IsReadStructParameters)
            {
                m_structParameters = structs.ToArray();
            }
        }