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;
            }
        }
Example #2
0
        /// <summary>	
        /// <p>[This documentation is preliminary and is subject to change.]</p><p>Links the shader and produces a shader blob that the Direct3D runtime can use.</p>	
        /// </summary>	
        /// <param name="module"><dd>  <p>A reference to the <strong><see cref="SharpDX.D3DCompiler.ModuleInstance"/></strong> interface for the shader module instance to link from.</p> </dd></param>	
        /// <param name="entryPointName"><dd>  <p>The name of the shader module instance to link from.</p> </dd></param>	
        /// <param name="targetName"><dd>  <p>The name for the shader blob that is produced.</p> </dd></param>	
        /// <param name="flags"><dd>  <p>Reserved</p> </dd></param>	
        /// <returns><p>Returns the compiled <see cref="ShaderBytecode"/>.</p></returns>	
        /// <msdn-id>dn280560</msdn-id>	
        /// <unmanaged>HRESULT ID3D11Linker::Link([In] ID3D11ModuleInstance* pEntry,[In] const char* pEntryName,[In] const char* pTargetName,[In] unsigned int uFlags,[Out] ID3D10Blob** ppShaderBlob,[Out] ID3D10Blob** ppErrorBuffer)</unmanaged>	
        /// <unmanaged-short>ID3D11Linker::Link</unmanaged-short>	
        /// <exception cref="CompilationException">Is thrown when linking fails and the error text is available.</exception>
        /// <exception cref="SharpDXException">Is thrown when linking fails and the error text is <b>not</b> available.</exception>
        public ShaderBytecode Link(ModuleInstance module, string entryPointName, string targetName, int flags)
        {
            Blob shaderBlob;
            Blob errorBlob;

            var resultCode = Link(module, entryPointName, targetName, flags, out shaderBlob, out errorBlob);

            if (resultCode.Failure)
            {
                if (errorBlob != null)
                    throw new CompilationException(resultCode, Utilities.BlobToString(errorBlob));

                throw new SharpDXException(resultCode);
            }

            return new ShaderBytecode(shaderBlob);
        }
Example #3
0
        /// <summary>
        /// <p>[This documentation is preliminary and is subject to change.]</p><p>Links the shader and produces a shader blob that the Direct3D runtime can use.</p>
        /// </summary>
        /// <param name="module"><dd>  <p>A reference to the <strong><see cref="SharpDX.D3DCompiler.ModuleInstance"/></strong> interface for the shader module instance to link from.</p> </dd></param>
        /// <param name="entryPointName"><dd>  <p>The name of the shader module instance to link from.</p> </dd></param>
        /// <param name="targetName"><dd>  <p>The name for the shader blob that is produced.</p> </dd></param>
        /// <param name="flags"><dd>  <p>Reserved</p> </dd></param>
        /// <returns><p>Returns the compiled <see cref="ShaderBytecode"/>.</p></returns>
        /// <msdn-id>dn280560</msdn-id>
        /// <unmanaged>HRESULT ID3D11Linker::Link([In] ID3D11ModuleInstance* pEntry,[In] const char* pEntryName,[In] const char* pTargetName,[In] unsigned int uFlags,[Out] ID3D10Blob** ppShaderBlob,[Out] ID3D10Blob** ppErrorBuffer)</unmanaged>
        /// <unmanaged-short>ID3D11Linker::Link</unmanaged-short>
        /// <exception cref="CompilationException">Is thrown when linking fails and the error text is available.</exception>
        /// <exception cref="SharpDXException">Is thrown when linking fails and the error text is <b>not</b> available.</exception>
        public ShaderBytecode Link(ModuleInstance module, string entryPointName, string targetName, int flags)
        {
            Blob shaderBlob;
            Blob errorBlob;

            var resultCode = Link(module, entryPointName, targetName, flags, out shaderBlob, out errorBlob);

            if (resultCode.Failure)
            {
                if (errorBlob != null)
                {
                    throw new CompilationException(resultCode, Utilities.BlobToString(errorBlob));
                }

                throw new SharpDXException(resultCode);
            }

            return(new ShaderBytecode(shaderBlob));
        }
        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);
        }