CompiledShaderReader(string fn,string shaderType,ConstantBuffer[] cbs , ShaderParameterDescription[] pds,InputBindingDescription[] rbs,ShaderReflection reflec,ShaderBytecode bytec) { cBuffers = cbs; parameterDescriptions = pds; resourceBindings = rbs; filename = fn; typePrefix = shaderType; reflector = reflec; bytecode = bytec; }
private static void CompareResourceBinding(InputBindingDescription expected, ResourceBinding actual) { Assert.AreEqual((uint)expected.BindCount, actual.BindCount); Assert.AreEqual(expected.BindPoint, actual.BindPoint); Assert.AreEqual((int)expected.Dimension, (int)actual.Dimension); Assert.AreEqual((int)expected.Flags, (int)actual.Flags); Assert.AreEqual(expected.Name, actual.Name); if (expected.NumSamples != -1 || actual.NumSamples != uint.MaxValue) { Assert.AreEqual(expected.NumSamples, actual.NumSamples); } Assert.AreEqual((int)expected.ReturnType, (int)actual.ReturnType); Assert.AreEqual((int)expected.Type, (int)actual.Type); }
/// <summary> /// 名前からコンスタントバッファのバインド位置を取得する /// </summary> /// <param name="name"></param> /// <param name="bindIndex"></param> /// <returns></returns> public bool FindConstantBufferByName(string name, out int bindIndex) { // コンスタントバッファの実体をとってきて確実にある状況でGetResourceBindingDescriptionを呼ぶ int cb_num = reflector_.Description.ConstantBuffers; for (int i = 0; i < cb_num; i++) { var cb = reflector_.GetConstantBuffer(i); if (cb.Description.Name == name) { InputBindingDescription desc = reflector_.GetResourceBindingDescription(name); bindIndex = desc.BindPoint; return(true); } } bindIndex = -1; return(false); }
protected void Compile(string name, StreamReader shaderSource, Profile profile) { Logger.LogInfo(this, "Compiling " + name + " with profile " + profile + "."); _shaderByteCode = ShaderBytecode.Compile(shaderSource.ReadToEnd(), "main", profile.ToString(), ShaderFlags.None, EffectFlags.None, null, null, name); using (ShaderReflection sr = new ShaderReflection(_shaderByteCode)) { for (int i = 0; i < sr.Description.BoundResources; i++) { InputBindingDescription desc = sr.GetResourceBindingDescription(i); switch (desc.Type) { case ShaderInputType.ConstantBuffer: { ConstantBufferInfo info = new ConstantBufferInfo(); info.BindingDescription = desc; var buffer = sr.GetConstantBuffer(desc.Name); for (int v = 0; v < buffer.Description.VariableCount; v++) { var variable = buffer.GetVariable(v); info.VariableDescriptions.Add(variable.Description.Name, variable.Description); } _constantBuffersInfos.Add(desc.Name, info); } break; case ShaderInputType.Texture: _shaderResourceInfos.Add(desc.Name, desc); break; case ShaderInputType.Sampler: _samplerStateInfos.Add(desc.Name, desc); break; } } } }
public static void CompileD3D12Shader(H1ShaderCompileInput input, H1ShaderCompileOutput output) { // process shared/single environments String includeContent = ""; List <SharpDX.Direct3D.ShaderMacro> macros = new List <SharpDX.Direct3D.ShaderMacro>(); // 1. shared environment ProcessShaderCompilerEnvironment(input.SharedEnvironment, ref includeContent, macros); // 2. single environment ProcessShaderCompilerEnvironment(input.Environment, ref includeContent, macros); // load shader file content String sourceContent = includeContent + "\n" + LoadShaderFile(input.SourceFileName); // preprocess the shader file sourceContent = ShaderBytecode.Preprocess(sourceContent, macros.ToArray(), new H1SharpDXCompileInclude()); #if DEBUG var shader = ShaderBytecode.Compile(sourceContent, input.EntryPointName, input.Target.ToFormat, SharpDX.D3DCompiler.ShaderFlags.Debug | SharpDX.D3DCompiler.ShaderFlags.SkipOptimization); #else var shader = ShaderBytecode.Compile(sourceContent, input.EntryPointName, input.Target.ToFormat); #endif if (shader.Message != null) // failed to compile the shader { // @TODO - should log the error for failing compiling shader output.IsSucceed = false; return; } // assign the resultant byte code output.Code = shader; // create shader parameter map output.ParameterMap = new H1ShaderParameterMap(); // reflection for the compiled shader ShaderReflection shaderReflect = new ShaderReflection(shader); ShaderDescription shaderDesc = shaderReflect.Description; int bindResCounts = shaderDesc.BoundResources; for (int resIdx = 0; resIdx < bindResCounts; ++resIdx) { InputBindingDescription bindDesc = shaderReflect.GetResourceBindingDescription(resIdx); // for constant buffers if (bindDesc.Type == ShaderInputType.ConstantBuffer) { int cbIndex = bindDesc.BindPoint; ConstantBuffer cb = shaderReflect.GetConstantBuffer(cbIndex); ConstantBufferDescription cbDesc; cbDesc = cb.Description; // track all variables in this constant buffer for (int varIdx = 0; varIdx < cbDesc.VariableCount; varIdx++) { ShaderReflectionVariable variable = cb.GetVariable(varIdx); ShaderVariableDescription variableDesc = variable.Description; output.ParameterMap.ParameterMap.Add(variableDesc.Name, new H1ParameterAllocation(H1ParameterType.Variable, cbIndex, variableDesc.StartOffset, variableDesc.Size)); } // add constant buffer parameter output.ParameterMap.ParameterMap.Add(cbDesc.Name, new H1ParameterAllocation(H1ParameterType.ConstantBuffer, cbIndex, -1, cbDesc.Size)); } // texture, samplers .... other various GDI data } // release shader reflection shaderReflect.Dispose(); output.IsSucceed = true; // successfully compiled }
private static void CompareResourceBinding(InputBindingDescription expected, ResourceBinding actual) { Assert.AreEqual(expected.BindCount, actual.BindCount); Assert.AreEqual(expected.BindPoint, actual.BindPoint); Assert.AreEqual((int) expected.Dimension, (int) actual.Dimension); Assert.AreEqual((int) expected.Flags, (int) actual.Flags); Assert.AreEqual(expected.Name, actual.Name); if (expected.NumSamples != -1 || actual.NumSamples != uint.MaxValue) Assert.AreEqual(expected.NumSamples, actual.NumSamples); Assert.AreEqual((int) expected.ReturnType, (int) actual.ReturnType); Assert.AreEqual((int) expected.Type, (int) actual.Type); }
public static CompiledShaderReader ReadCompiledShader(string path) { if (File.Exists(path) && (Path.GetExtension(path) == ".cso")) { byte[] bytes = File.ReadAllBytes(path); ShaderReflection reflecter = new ShaderReflection(bytes); ShaderBytecode bytecode = new ShaderBytecode(bytes); ShaderProfile profile = bytecode.GetVersion(); ConstantBuffer[] cbuffers = new ConstantBuffer[reflecter.Description.ConstantBuffers]; for (int i = 0; i < reflecter.Description.ConstantBuffers; i++) { cbuffers[i] = reflecter.GetConstantBuffer(i);//this might not work } ShaderParameterDescription[] paramdescriptions = new ShaderParameterDescription[reflecter.Description.InputParameters]; for (int i = 0; i < reflecter.Description.InputParameters; i++) { paramdescriptions[i] = reflecter.GetInputParameterDescription(i); } InputBindingDescription[] bindings = new InputBindingDescription[reflecter.Description.BoundResources]; for (int i = 0; i < reflecter.Description.BoundResources; i++) { bindings[i] = reflecter.GetResourceBindingDescription(i); } ConstantBuffer[] cbuffersCOPY = cbuffers.ToArray(); return new CompiledShaderReader(path, profile.GetTypePrefix(), cbuffersCOPY, paramdescriptions.ToArray(), bindings.ToArray(), reflecter, bytecode); } else throw new Exception("Bad path " + path); }