public EffectData Rebuild() { // assemble the source text var source = GetSource(); // compile the library var compilationResult = ShaderBytecode.Compile(source, "lib_5_0", ShaderFlags.OptimizationLevel3, EffectFlags.None); // if compilation failed - try to reset to the initial data and rebuild again if (compilationResult.HasErrors) { if (_isRebuildingAfterReset) // something is messed up, we alraedy tried to rebuild once throw new InvalidOperationException(); try { _isRebuildingAfterReset = true; // reset the data _data.Reset(); // rebuild return Rebuild(); } finally { _isRebuildingAfterReset = false; } } var bytecode = compilationResult.Bytecode; // create the shader library module var shaderLibrary = new Module(bytecode); // create the shader library module instance var shaderLibraryInstance = new ModuleInstance(shaderLibrary); // mark the implicit constant buffer (for single parameter) as bindable shaderLibraryInstance.BindConstantBuffer(0, 0, 0); // assemble vertex shader var vertexShaderBytecode = AssembleVertexShader(shaderLibrary, shaderLibraryInstance); // assemble pixel shader var pixelShaderBytecode = AssemblePixelShader(shaderLibrary, shaderLibraryInstance); try { // assemble the effect data from the bytecodes return _effectCompiler.Compile(vertexShaderBytecode, pixelShaderBytecode); } finally { _data.IsDirty = false; } }
private ShaderBytecode AssembleVertexShader(Module shaderLibrary, ModuleInstance shaderLibraryInstance) { var vertexShaderGraph = new FunctionLinkingGraph(); // create input node - parameter name, semantic and size in bytes var vertexShaderInputNode = vertexShaderGraph.SetInputSignature(CreateInParam("inputPos", "SV_POSITION", 4), CreateInParam("inputTex", "COLOR", 4)); // create the function call node var vertexFunctionCallNode = vertexShaderGraph.CallFunction(shaderLibrary, "VertexFunction"); // bind input parameters to the function call vertexShaderGraph.PassValue(vertexShaderInputNode, 0, vertexFunctionCallNode, 0); vertexShaderGraph.PassValue(vertexShaderInputNode, 1, vertexFunctionCallNode, 1); // create the output parameters node var vertexShaderOutputNode = vertexShaderGraph.SetOutputSignature(CreateOutParam("outputTex", "SV_POSITION", 4), CreateOutParam("outputNorm", "COLOR", 4)); // bind function call parameters to the output node vertexShaderGraph.PassValue(vertexFunctionCallNode, 0, vertexShaderOutputNode, 0); vertexShaderGraph.PassValue(vertexFunctionCallNode, 1, vertexShaderOutputNode, 1); // create the library instance for the shader var vertexShaderGraphInstance = vertexShaderGraph.CreateModuleInstance(); var linker = new Linker(); // bind linker to the function library linker.UseLibrary(shaderLibraryInstance); // link the shader return linker.Link(vertexShaderGraphInstance, "main", "vs_5_0", 0); }
private ShaderBytecode AssemblePixelShader(Module shaderLibrary, ModuleInstance shaderLibraryInstance) { var pixelShaderGraph = new FunctionLinkingGraph(); var pixelShaderInputNode = pixelShaderGraph.SetInputSignature(CreateInParam("inputPos", "SV_POSITION", 4, InterpolationMode.Undefined), CreateInParam("inputTex", "COLOR", 4, InterpolationMode.Undefined)); var colorValueNode = pixelShaderGraph.CallFunction(shaderLibrary, "ColorFunction"); pixelShaderGraph.PassValue(pixelShaderInputNode, 0, colorValueNode, 0); pixelShaderGraph.PassValue(pixelShaderInputNode, 1, colorValueNode, 1); // link additional nodes based on configuration if (_data.EnableInvertColor) { var tempNode = pixelShaderGraph.CallFunction(shaderLibrary, "InvertColor"); pixelShaderGraph.PassValue(colorValueNode, tempNode, 0); colorValueNode = tempNode; } if (_data.EnableGrayscale) { var tempNode = pixelShaderGraph.CallFunction(shaderLibrary, "Grayscale"); pixelShaderGraph.PassValue(colorValueNode, tempNode, 0); colorValueNode = tempNode; } var pixelShaderOutputNode = pixelShaderGraph.SetOutputSignature(CreateOutParam("outputColor", "SV_TARGET", 4)); pixelShaderGraph.PassValue(colorValueNode, pixelShaderOutputNode, 0); var pixelShaderGraphInstance = pixelShaderGraph.CreateModuleInstance(); var linker = new Linker(); linker.UseLibrary(shaderLibraryInstance); return linker.Link(pixelShaderGraphInstance, "main", "ps_5_0", 0); }
/// <summary> /// <p>[This documentation is preliminary and is subject to change.]</p><p>Creates a call-function linking node to use in the function-linking-graph.</p> /// </summary> /// <param name="moduleWithFunctionPrototypeRef"><dd> <p>A reference to the <strong><see cref="SharpDX.D3DCompiler.ModuleInstance"/></strong> interface for the library module that contains the function prototype.</p> </dd></param> /// <param name="functionNameRef"><dd> <p>The name of the function.</p> </dd></param> /// <returns><dd> <p>A reference to a variable that receives a reference to the <strong><see cref="SharpDX.D3DCompiler.LinkingNode"/></strong> interface that represents the function in the function-linking-graph.</p> </dd></returns> /// <msdn-id>dn280536</msdn-id> /// <unmanaged>HRESULT ID3D11FunctionLinkingGraph::CallFunction([In, Optional] const char* pModuleInstanceNamespace,[In] ID3D11Module* pModuleWithFunctionPrototype,[In] const char* pFunctionName,[Out] ID3D11LinkingNode** ppCallNode)</unmanaged> /// <unmanaged-short>ID3D11FunctionLinkingGraph::CallFunction</unmanaged-short> public LinkingNode CallFunction(Module moduleWithFunctionPrototypeRef, string functionNameRef) { return CallFunction(string.Empty, moduleWithFunctionPrototypeRef, functionNameRef); }
/// <summary> /// <p>[This documentation is preliminary and is subject to change.]</p><p>Creates a call-function linking node to use in the function-linking-graph.</p> /// </summary> /// <param name="moduleWithFunctionPrototypeRef"><dd> <p>A reference to the <strong><see cref="SharpDX.D3DCompiler.ModuleInstance"/></strong> interface for the library module that contains the function prototype.</p> </dd></param> /// <param name="functionNameRef"><dd> <p>The name of the function.</p> </dd></param> /// <returns><dd> <p>A reference to a variable that receives a reference to the <strong><see cref="SharpDX.D3DCompiler.LinkingNode"/></strong> interface that represents the function in the function-linking-graph.</p> </dd></returns> /// <msdn-id>dn280536</msdn-id> /// <unmanaged>HRESULT ID3D11FunctionLinkingGraph::CallFunction([In, Optional] const char* pModuleInstanceNamespace,[In] ID3D11Module* pModuleWithFunctionPrototype,[In] const char* pFunctionName,[Out] ID3D11LinkingNode** ppCallNode)</unmanaged> /// <unmanaged-short>ID3D11FunctionLinkingGraph::CallFunction</unmanaged-short> public LinkingNode CallFunction(Module moduleWithFunctionPrototypeRef, string functionNameRef) { return(CallFunction(string.Empty, moduleWithFunctionPrototypeRef, functionNameRef)); }
/// <summary> /// Initializes a new instance of the <see cref="ModuleInstance"/> class. /// </summary> /// <param name="module"><p>The address of a reference to an <strong><see cref="SharpDX.D3DCompiler.ModuleInstance"/></strong> interface to initialize.</p></param> public ModuleInstance(Module module) : this(string.Empty, module) { }
/// <summary> /// Initializes a new instance of the <see cref="ModuleInstance"/> class. /// </summary> /// <param name="namespaceRef"><p>The name of a shader module to initialize. This can be <strong><c>null</c></strong> if you don't want to specify a name for the module.</p></param> /// <param name="module"><p>The address of a reference to an <strong><see cref="SharpDX.D3DCompiler.ModuleInstance"/></strong> interface to initialize.</p></param> public ModuleInstance(string namespaceRef, Module module) { if (module == null) throw new ArgumentNullException("module"); module.CreateInstance(namespaceRef, this); }
/// <summary> /// Initializes a new instance of the <see cref="ModuleInstance"/> class. /// </summary> /// <param name="module"><p>The address of a reference to an <strong><see cref="SharpDX.D3DCompiler.ModuleInstance"/></strong> interface to initialize.</p></param> public ModuleInstance(Module module) : this(string.Empty, module) { }