public static ShaderChannel5 ToShaderChannel5(this ShaderChannel _this) { switch (_this) { case ShaderChannel.Vertex: return(ShaderChannel5.Vertex); case ShaderChannel.Normal: return(ShaderChannel5.Normal); case ShaderChannel.Color: return(ShaderChannel5.Color); case ShaderChannel.UV0: return(ShaderChannel5.UV0); case ShaderChannel.UV1: return(ShaderChannel5.UV1); case ShaderChannel.UV2: return(ShaderChannel5.UV2); case ShaderChannel.UV3: return(ShaderChannel5.UV3); case ShaderChannel.Tangent: return(ShaderChannel5.Tangent); default: throw new Exception($"Unsupported channel type {_this}"); } }
public static byte GetDimention(this ShaderChannel _this, Version version) { switch (_this) { case ShaderChannel.Vertex: return(3); case ShaderChannel.Normal: return(3); case ShaderChannel.Tangent: return(4); case ShaderChannel.Color: return(ShaderChannel5Relevant(version) ? (byte)4 : (byte)1); case ShaderChannel.UV0: case ShaderChannel.UV1: case ShaderChannel.UV2: case ShaderChannel.UV3: case ShaderChannel.UV4: case ShaderChannel.UV5: case ShaderChannel.UV6: case ShaderChannel.UV7: return(2); case ShaderChannel.SkinWeight: case ShaderChannel.SkinBoneIndex: throw new Exception($"Skin's dimention is varying"); default: throw new Exception($"Unsupported channel type {_this}"); } }
public static byte GetStride(this ShaderChannel _this, Version version) { VertexFormat format = _this.GetVertexFormat(version); int dimention = _this.GetDimention(version); return(format.CalculateStride(version, dimention)); }
public static byte GetStride(this ShaderChannel _this) { // since sizeof(Color) * 1 == sizeof(Byte) * 4, we can omit version VertexFormat format = _this.GetVertexFormat(Version.MinVersion); int dimention = _this.GetDimention(Version.MinVersion); return(format.CalculateStride(dimention)); }
public ChannelInfo GetChannel(Version version, ShaderChannel channelType) { if (HasChannels(version)) { return(Channels[channelType.ToChannel(version)]); } else { return(StreamInfoConverter.GenerateChannelInfo(version, Streams, channelType)); } }
public static ShaderChannel2018 ToShaderChannel2018(this ShaderChannel _this) { switch (_this) { case ShaderChannel.Vertex: return(ShaderChannel2018.Vertex); case ShaderChannel.Normal: return(ShaderChannel2018.Normal); case ShaderChannel.Tangent: return(ShaderChannel2018.Tangent); case ShaderChannel.Color: return(ShaderChannel2018.Color); case ShaderChannel.UV0: return(ShaderChannel2018.UV0); case ShaderChannel.UV1: return(ShaderChannel2018.UV1); case ShaderChannel.UV2: return(ShaderChannel2018.UV2); case ShaderChannel.UV3: return(ShaderChannel2018.UV3); case ShaderChannel.UV4: return(ShaderChannel2018.UV4); case ShaderChannel.UV5: return(ShaderChannel2018.UV5); case ShaderChannel.UV6: return(ShaderChannel2018.UV6); case ShaderChannel.UV7: return(ShaderChannel2018.UV7); case ShaderChannel.SkinWeight: return(ShaderChannel2018.SkinWeight); case ShaderChannel.SkinBoneIndex: return(ShaderChannel2018.SkinBoneIndex); default: throw new Exception($"Unsupported channel type {_this}"); } }
public static int ToChannel(this ShaderChannel _this, Version version) { if (ShaderChannel2018Relevant(version)) { return((int)ToShaderChannel2018(_this)); } else if (ShaderChannel5Relevant(version)) { return((int)ToShaderChannel5(_this)); } else { return((int)ToShaderChannel4(_this)); } }
public static bool HasChannel(this ShaderChannel _this, Version version) { if (ShaderChannel2018Relevant(version)) { return(true); } else if (ShaderChannel5Relevant(version)) { return(_this <= ShaderChannel.UV3); } else { return(_this <= ShaderChannel.UV1); } }
public static int ToChannel(this ShaderChannel _this, Version version) { if (version.IsLess(5)) { return((int)ToShaderChannelV4(_this)); } else if (version.IsLess(2018)) { return((int)ToShaderChannelV5(_this)); } else { return((int)ToShaderChannelV2018(_this)); } }
public static bool HasChannel(this ShaderChannel _this, Version version) { if (version.IsLess(5)) { return(_this <= ShaderChannel.UV1); } else if (version.IsLess(2018)) { return(_this <= ShaderChannel.UV4); } else { return(true); } }
public static VertexFormat GetVertexFormat(this ShaderChannel _this, Version version) { switch (_this) { case ShaderChannel.Vertex: return(VertexFormat.Float); case ShaderChannel.Normal: return(VertexFormat.Float); case ShaderChannel.Tangent: return(VertexFormat.Float); case ShaderChannel.Color: return(VertexFormatExtensions.VertexFormat2019Relevant(version) ? VertexFormat.Byte : VertexFormat.Color); case ShaderChannel.UV0: case ShaderChannel.UV1: case ShaderChannel.UV2: case ShaderChannel.UV3: case ShaderChannel.UV4: case ShaderChannel.UV5: case ShaderChannel.UV6: case ShaderChannel.UV7: return(VertexFormat.Float); case ShaderChannel.SkinWeight: return(VertexFormat.Float); case ShaderChannel.SkinBoneIndex: return(VertexFormat.Int); default: throw new Exception($"Unsupported channel type {_this}"); } }
public void Read(AssetReader reader) { int magic = reader.ReadInt32(); if (magic != GetMagicNumber(reader.Version)) { throw new Exception($"Magic number {magic} doesn't match"); } ProgramType = (ShaderGpuProgramType)reader.ReadInt32(); int unknown1 = reader.ReadInt32(); int unknown2 = reader.ReadInt32(); int unknown3 = reader.ReadInt32(); if (HasUnknown4(reader.Version)) { int unknown4 = reader.ReadInt32(); } GlobalKeywords = reader.ReadStringArray(); if (HasLocalKeywords(reader.Version)) { LocalKeywords = reader.ReadStringArray(); } ProgramData = reader.ReadByteArray(); reader.AlignStream(); int sourceMap = reader.ReadInt32(); int bindCount = reader.ReadInt32(); ShaderBindChannel[] channels = new ShaderBindChannel[bindCount]; for (int i = 0; i < bindCount; i++) { ShaderChannel source = (ShaderChannel)reader.ReadUInt32(); VertexComponent target = (VertexComponent)reader.ReadUInt32(); ShaderBindChannel channel = new ShaderBindChannel(source, target); channels[i] = channel; sourceMap |= 1 << (int)source; } BindChannels = new ParserBindChannels(channels, sourceMap); List <VectorParameter> vectors = new List <VectorParameter>(); List <MatrixParameter> matrices = new List <MatrixParameter>(); List <TextureParameter> textures = new List <TextureParameter>(); List <VectorParameter> structVectors = new List <VectorParameter>(); List <MatrixParameter> structMatrices = new List <MatrixParameter>(); List <BufferBinding> buffers = new List <BufferBinding>(); List <UAVParameter> uavs = HasUAVParameters(reader.Version) ? new List <UAVParameter>() : null; List <SamplerParameter> samplers = HasSamplerParameters(reader.Version) ? new List <SamplerParameter>() : null; List <BufferBinding> constBindings = new List <BufferBinding>(); List <StructParameter> structs = new List <StructParameter>(); int paramGroupCount = reader.ReadInt32(); ConstantBuffers = new ConstantBuffer[paramGroupCount - 1]; for (int i = 0; i < paramGroupCount; i++) { vectors.Clear(); matrices.Clear(); structs.Clear(); string name = reader.ReadString(); int usedSize = reader.ReadInt32(); int paramCount = reader.ReadInt32(); for (int j = 0; j < paramCount; j++) { string paramName = reader.ReadString(); ShaderParamType paramType = (ShaderParamType)reader.ReadInt32(); int rows = reader.ReadInt32(); int columns = reader.ReadInt32(); bool isMatrix = reader.ReadInt32() > 0; int arraySize = reader.ReadInt32(); int index = reader.ReadInt32(); if (isMatrix) { MatrixParameter matrix = IsAllParamArgs(reader.Version) ? new MatrixParameter(paramName, paramType, index, arraySize, rows, columns) : new MatrixParameter(paramName, paramType, index, rows, columns); matrices.Add(matrix); } else { VectorParameter vector = IsAllParamArgs(reader.Version) ? new VectorParameter(paramName, paramType, index, arraySize, columns) : new VectorParameter(paramName, paramType, index, columns); vectors.Add(vector); } } if (HasStructParameters(reader.Version)) { int structCount = reader.ReadInt32(); for (int j = 0; j < structCount; j++) { structVectors.Clear(); structMatrices.Clear(); string structName = reader.ReadString(); int index = reader.ReadInt32(); int arraySize = reader.ReadInt32(); int structSize = reader.ReadInt32(); int strucParamCount = reader.ReadInt32(); for (int k = 0; k < strucParamCount; k++) { string paramName = reader.ReadString(); paramName = $"{structName}.{paramName}"; ShaderParamType paramType = (ShaderParamType)reader.ReadInt32(); int rows = reader.ReadInt32(); int columns = reader.ReadInt32(); bool isMatrix = reader.ReadInt32() > 0; int vectorArraySize = reader.ReadInt32(); int paramIndex = reader.ReadInt32(); if (isMatrix) { MatrixParameter matrix = IsAllParamArgs(reader.Version) ? new MatrixParameter(paramName, paramType, paramIndex, vectorArraySize, rows, columns) : new MatrixParameter(paramName, paramType, paramIndex, rows, columns); structMatrices.Add(matrix); } else { VectorParameter vector = IsAllParamArgs(reader.Version) ? new VectorParameter(paramName, paramType, paramIndex, vectorArraySize, columns) : new VectorParameter(paramName, paramType, paramIndex, columns); structVectors.Add(vector); } } StructParameter @struct = new StructParameter(structName, index, arraySize, structSize, structVectors.ToArray(), structMatrices.ToArray()); structs.Add(@struct); } } if (i == 0) { VectorParameters = vectors.ToArray(); MatrixParameters = matrices.ToArray(); StructParameters = structs.ToArray(); } else { ConstantBuffer constBuffer = new ConstantBuffer(name, matrices.ToArray(), vectors.ToArray(), structs.ToArray(), usedSize); ConstantBuffers[i - 1] = constBuffer; } } int paramGroup2Count = reader.ReadInt32(); for (int i = 0; i < paramGroup2Count; i++) { string name = reader.ReadString(); int type = reader.ReadInt32(); int index = reader.ReadInt32(); int extraValue = reader.ReadInt32(); if (type == 0) { TextureParameter texture; if (HasMultiSampled(reader.Version)) { uint textureExtraValue = reader.ReadUInt32(); bool isMultiSampled = (textureExtraValue & 1) == 1; byte dimension = (byte)(textureExtraValue >> 1); texture = new TextureParameter(name, index, dimension, extraValue, isMultiSampled); } else { byte dimension = unchecked ((byte)extraValue); int samplerIndex = extraValue >> 8; if (samplerIndex == 0xFFFFFF) { samplerIndex = -1; } texture = new TextureParameter(name, index, dimension, samplerIndex); } 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 && HasUAVParameters(reader.Version)) { UAVParameter uav = new UAVParameter(name, index, extraValue); uavs.Add(uav); } else if (type == 4 && HasSamplerParameters(reader.Version)) { SamplerParameter sampler = new SamplerParameter((uint)extraValue, index); samplers.Add(sampler); } else { throw new Exception($"Unupported parameter type {type}"); } } TextureParameters = textures.ToArray(); BufferParameters = buffers.ToArray(); if (HasUAVParameters(reader.Version)) { UAVParameters = uavs.ToArray(); } if (HasSamplerParameters(reader.Version)) { SamplerParameters = samplers.ToArray(); } ConstantBufferBindings = constBindings.ToArray(); if (HasStructParameters(reader.Version)) { StructParameters = structs.ToArray(); } }
public void Read(AssetStream stream) { int magic = stream.ReadInt32(); if (magic != GetMagicNumber(stream.Version)) { 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(stream.Version)) { 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; } 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(stream.Version) ? new List <UAVParameter>() : null; List <SamplerParameter> samplers = IsReadSamplerParameters(stream.Version) ? new List <SamplerParameter>() : null; List <BufferBinding> constBindings = new List <BufferBinding>(); List <StructParameter> structs = IsReadStructParameters(stream.Version) ? 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(stream.Version) ? new MatrixParameter(paramName, paramType, index, arraySize, rows) : new MatrixParameter(paramName, paramType, index, rows); matrices.Add(matrix); } else { VectorParameter vector = IsAllParamArgs(stream.Version) ? 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(name, matrices.ToArray(), vectors.ToArray(), usedSize); m_constantBuffers[i - 1] = constBuffer; } if (IsReadStructParameters(stream.Version)) { 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(stream.Version) ? new MatrixParameter(paramName, paramType, paramIndex, vectorArraySize, rows) : new MatrixParameter(paramName, paramType, paramIndex, rows); matrices.Add(matrix); } else { VectorParameter vector = IsAllParamArgs(stream.Version) ? 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(stream.Version)) { bool isMultiSampled = stream.ReadUInt32() > 0; texture = new TextureParameter(name, index, isMultiSampled, extraValue); } else { texture = new TextureParameter(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(stream.Version)) { UAVParameter uav = new UAVParameter(name, index, extraValue); uavs.Add(uav); } else if (type == 4 && IsReadSamplerParameters(stream.Version)) { 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(stream.Version)) { m_UAVParameters = uavs.ToArray(); } if (IsReadSamplerParameters(stream.Version)) { m_samplerParameters = samplers.ToArray(); } m_constantBufferBindings = constBindings.ToArray(); if (IsReadStructParameters(stream.Version)) { m_structParameters = structs.ToArray(); } }
public static ChannelInfo GenerateChannelInfo(IExportContainer container, StreamInfo[] origin, ShaderChannel channelType) { return(GenerateChannelInfo(container.ExportVersion, origin, channelType)); }
public static ChannelInfo GenerateChannelInfo(Version instanceVersion, StreamInfo[] origin, ShaderChannel channelType) { ChannelInfo instance = new ChannelInfo(); ShaderChannel4 channelv4 = channelType.ToShaderChannel4(); int streamIndex = origin.IndexOf(t => t.IsMatch(channelv4)); if (streamIndex >= 0) { byte offset = 0; ref StreamInfo stream = ref origin[streamIndex]; for (ShaderChannel4 i = 0; i < channelv4; i++) { if (stream.IsMatch(i)) { offset += i.ToShaderChannel().GetStride(instanceVersion); } } instance.Stream = (byte)streamIndex; instance.Offset = offset; instance.Format = channelType.GetVertexFormat(instanceVersion).ToFormat(instanceVersion); instance.RawDimension = channelType.GetDimention(instanceVersion); }
public ShaderBindChannel(ShaderChannel source, VertexComponent target) { Source = source; Target = target; }