private void WriteVariableHeader(EndianWriter writer, VariableHeader header)
        {
            //name offset
            writer.Write(header.NameOffset);
            //startOffset
            writer.Write(header.StartOffset);
            //Size
            writer.Write(header.Variable.Length);
            //flags
            writer.Write((uint)ShaderVariableFlags.Used);             //Unity only packs used variables as far as I can tell

            uint typeOffset = m_typeLookup[header.Variable.ShaderType];

            //type offset
            writer.Write(typeOffset);
            //default value offset
            writer.Write((uint)0);             //Not used
            if (majorVersion >= 5)
            {
                //StartTexture
                writer.Write((uint)0);
                //TextureSize
                writer.Write((uint)0);
                //StartSampler
                writer.Write((uint)0);
                //SamplerSize
                writer.Write((uint)0);
            }
        }
        private void BuildVariableHeaders(uint variableOffset)
        {
            const int memberSize    = 12;
            uint      variableSize  = majorVersion >= 5 ? (uint)40 : (uint)24;
            uint      variableCount = (uint)m_variables.Length;
            uint      dataOffset    = variableOffset + variableCount * variableSize;

            foreach (Variable variable in m_variables)
            {
                m_variableNameLookup[variable.Name] = dataOffset;
                VariableHeader header = new VariableHeader();
                header.NameOffset  = dataOffset;
                dataOffset        += (uint)variable.Name.Length + 1;
                header.StartOffset = (uint)variable.Index;
                header.Variable    = variable;

                m_typeLookup[variable.ShaderType] = dataOffset;
                dataOffset += variable.ShaderType.Size();

                variable.ShaderType.MemberOffset = variable.ShaderType.Members.Length > 0 ? dataOffset : 0;
                dataOffset += (uint)variable.ShaderType.Members.Length * memberSize;

                foreach (ShaderTypeMember member in variable.ShaderType.Members)
                {
                    m_variableNameLookup[member.Name] = dataOffset;
                    dataOffset += (uint)member.Name.Length + 1;

                    m_typeLookup[member.ShaderType] = dataOffset;
                    dataOffset += member.ShaderType.Size();
                }

                m_variableHeaders.Add(header);
            }
            Size = dataOffset - variableOffset;
        }
Exemple #3
0
        public VariableChunk(ConstantBuffer constantBuffer, int constantBufferIndex, uint variableOffset, ShaderGpuProgramType programType)
        {
            const int memberSize = 12;

            m_constantBufferIndex = constantBufferIndex;
            m_programType         = programType;
            m_variables           = BuildVariables(constantBuffer);

            majorVersion = programType.GetMajorDXVersion();
            uint variableSize  = majorVersion >= 5 ? (uint)40 : (uint)24;
            uint variableCount = (uint)m_variables.Count;
            uint dataOffset    = variableOffset + variableCount * variableSize;

            foreach (Variable variable in m_variables)
            {
                m_variableNameLookup[variable.Name] = dataOffset;
                VariableHeader header = new VariableHeader();
                header.NameOffset  = dataOffset;
                dataOffset        += (uint)variable.Name.Length + 1;
                header.StartOffset = (uint)variable.Index;
                header.Variable    = variable;

                m_typeLookup[variable.ShaderType] = dataOffset;
                dataOffset += variable.ShaderType.Size();

                variable.ShaderType.MemberOffset = variable.ShaderType.Members.Count > 0 ? dataOffset : 0;
                dataOffset += (uint)variable.ShaderType.Members.Count * memberSize;

                foreach (ShaderTypeMember member in variable.ShaderType.Members)
                {
                    m_variableNameLookup[member.Name] = dataOffset;
                    dataOffset += (uint)member.Name.Length + 1;

                    m_typeLookup[member.ShaderType] = dataOffset;
                    dataOffset += member.ShaderType.Size();
                }

                m_variableHeaders.Add(header);
            }
            Size = dataOffset - variableOffset;
        }