Exemplo n.º 1
0
        static IShader compilePixelShader(iShaderFactory compiler, iStorageFolder assets)
        {
            // return compiler.compileHlslFile( assets, "Nv12PS.hlsl", ShaderType.Pixel );
            ShaderSourceInfo sourceInfo = new ShaderSourceInfo(ShaderType.Pixel, ShaderSourceLanguage.Glsl);

            sourceInfo.combinedTextureSamplers = true;
            return(compiler.compileFromFile(assets, "Nv12PS.glsl", sourceInfo));
        }
Exemplo n.º 2
0
        protected override IShader compilePixelShader(iShaderFactory compiler, iStorageFolder assets, string uvMin, string uvMax, string colorString)
        {
            ShaderSourceInfo ssi = new ShaderSourceInfo(ShaderType.Pixel, ShaderSourceLanguage.Hlsl)
            {
                combinedTextureSamplers = true
            };

            return(compiler.compileFromFile(assets, "VideoPS.hlsl", ssi, "RenderVideoPS", makeMacros(uvMin, uvMax, colorString)));
        }
Exemplo n.º 3
0
        public Blender(Context context, IRenderDevice device, byte samplesCount)
        {
            PipelineStateDesc desc = new PipelineStateDesc(false);

            desc.setBufferFormats(context);
            desc.premultipliedAlphaBlending();
            desc.GraphicsPipeline.PrimitiveTopology            = PrimitiveTopology.TriangleList;
            desc.GraphicsPipeline.RasterizerDesc.CullMode      = CullMode.None;
            desc.GraphicsPipeline.DepthStencilDesc.DepthEnable = false;

            iShaderFactory shaderFactory = device.GetShaderFactory();
            iStorageFolder assets        = StorageFolder.embeddedResources(Assembly.GetExecutingAssembly(), resourceSubfolder);

            using (iPipelineStateFactory stateFactory = device.CreatePipelineStateFactory())
            {
                stateFactory.layoutVariable(ShaderType.Pixel, ShaderResourceVariableType.Mutable, textureVarName);

                stateFactory.graphicsVertexShader(shaderFactory.compileShader(assets, "Blend", ShaderType.Vertex));

                ShaderSourceInfo ssi;
                string           src;
                if (RuntimeEnvironment.operatingSystem == eOperatingSystem.Windows)
                {
                    ssi = new ShaderSourceInfo(ShaderType.Pixel, ShaderSourceLanguage.Hlsl);
                    src = "BlendPS.hlsl";
                }
                else
                {
                    ssi = new ShaderSourceInfo(ShaderType.Pixel, ShaderSourceLanguage.Glsl);
                    ssi.combinedTextureSamplers = true;
                    src = "BlendPS.glsl";
                }

                string name = $"BlendPS { samplesCount }x";
                var    ps   = shaderFactory.compileFromFile(assets, src, ssi, name, shaderMacros(samplesCount));
                stateFactory.graphicsPixelShader(ps);

                stateFactory.apply(ref desc);
                pipelineState = device.CreatePipelineState(ref desc);
            }
            this.samplesCount = samplesCount;
        }
Exemplo n.º 4
0
        void createPipelineState(IRenderDevice device, iStorageFolder assets)
        {
            PipelineStateDesc PSODesc = new PipelineStateDesc(false);

            PSODesc.setBufferFormats(context);

            // Primitive topology defines what kind of primitives will be rendered by this pipeline state
            PSODesc.GraphicsPipeline.PrimitiveTopology = PrimitiveTopology.TriangleList;
            // Cull back faces
            PSODesc.GraphicsPipeline.RasterizerDesc.CullMode = CullMode.Back;
            // Enable depth testing
            PSODesc.GraphicsPipeline.DepthStencilDesc.DepthEnable = true;

            iShaderFactory shaderFactory = device.GetShaderFactory();

            // We won't be using the factory object after this, `using` to release the COM interface once finished
            using (iPipelineStateFactory stateFactory = device.CreatePipelineStateFactory())
            {
                stateFactory.setName("Cube PSO");

                // Compile the two shaders
                ShaderSourceInfo sourceInfo = new ShaderSourceInfo(ShaderType.Vertex, ShaderSourceLanguage.Hlsl);
                sourceInfo.combinedTextureSamplers = true;                  // This appears to be the requirement of OpenGL backend.

                // In this tutorial, we will load shaders from resources embedded into this .NET DLL.
                var vs = shaderFactory.compileFromFile(assets, "cube.vsh", sourceInfo, "Cube VS");
                stateFactory.graphicsVertexShader(vs);

                // Create dynamic uniform buffer that will store our transformation matrix. Dynamic buffers can be frequently updated by the CPU.
                BufferDesc CBDesc = new BufferDesc(false);
                CBDesc.uiSizeInBytes  = Marshal.SizeOf <Matrix4x4>();
                CBDesc.Usage          = Usage.Dynamic;
                CBDesc.BindFlags      = BindFlags.UniformBuffer;
                CBDesc.CPUAccessFlags = CpuAccessFlags.Write;
                vsConstants           = device.CreateBuffer(CBDesc, "VS constants CB");

                // Create a pixel shader
                sourceInfo.shaderType = ShaderType.Pixel;

                var ps = shaderFactory.compileFromFile(assets, "cube.psh", sourceInfo, "Cube PS");
                stateFactory.graphicsPixelShader(ps);

                // Define vertex shader input layout

                // Attribute 0 - vertex position
                LayoutElement elt = new LayoutElement(false)
                {
                    InputIndex    = 0,
                    BufferSlot    = 0,
                    NumComponents = 3,
                    ValueType     = GpuValueType.Float32,
                    IsNormalized  = false
                };
                stateFactory.graphicsLayoutElement(elt);
                // Attribute 1 - texture coordinates
                elt.InputIndex    = 1;
                elt.NumComponents = 2;
                stateFactory.graphicsLayoutElement(elt);

                // Define variable type that will be used by default
                PSODesc.ResourceLayout.DefaultVariableType = ShaderResourceVariableType.Static;
                // Shader variables should typically be mutable, which means they are expected to change on a per-instance basis
                stateFactory.layoutVariable(ShaderType.Pixel, ShaderResourceVariableType.Mutable, "g_Texture");

                // Define static sampler for g_Texture. Static samplers should be used whenever possible.
                // The default constructor is good enough, it sets FilterType.Linear and TextureAddressMode.Clamp for all 3 coordinates.
                SamplerDesc samplerDesc = new SamplerDesc(false);
                stateFactory.layoutStaticSampler(ShaderType.Pixel, ref samplerDesc, "g_Texture");

                stateFactory.apply(ref PSODesc);
                pipelineState = device.CreatePipelineState(ref PSODesc);
            }

            // Since we did not explicitly specify the type for 'Constants' variable, default
            // type (SHADER_RESOURCE_VARIABLE_TYPE_STATIC) will be used. Static variables never
            // change and are bound directly through the pipeline state object.
            pipelineState.GetStaticVariableByName(ShaderType.Vertex, "Constants").Set(vsConstants);

            // Since we are using mutable variable, we must create a shader resource binding object
            // http://diligentgraphics.com/2016/03/23/resource-binding-model-in-diligent-engine-2-0/
            resourceBinding = pipelineState.CreateShaderResourceBinding(true);
        }
Exemplo n.º 5
0
        public TeapotResources(Context context, IRenderDevice device)
        {
            PipelineStateDesc PSODesc = new PipelineStateDesc(false);

            PSODesc.setBufferFormats(context);

            // Primitive topology defines what kind of primitives will be rendered by this pipeline state
            PSODesc.GraphicsPipeline.PrimitiveTopology = PrimitiveTopology.TriangleList;
            // Cull back faces
            PSODesc.GraphicsPipeline.RasterizerDesc.CullMode = CullMode.Back;
            // Enable depth testing
            PSODesc.GraphicsPipeline.DepthStencilDesc.DepthEnable = true;

            iShaderFactory shaderFactory = device.GetShaderFactory();

            // We won't be using the device object after this, `using` is to release the COM interface once finished
            using (iPipelineStateFactory stateFactory = device.CreatePipelineStateFactory())
            {
                stateFactory.setName("Teapot PSO");

                // Compile the two shaders
                ShaderSourceInfo sourceInfo = new ShaderSourceInfo(ShaderType.Vertex, ShaderSourceLanguage.Hlsl);
                sourceInfo.combinedTextureSamplers = true;                  // This appears to be the requirement of OpenGL backend.

                // In this tutorial, we will load shaders from resources embedded into this .NET DLL.
                iStorageFolder resources = StorageFolder.embeddedResources(Assembly.GetExecutingAssembly(), resourcesFolder);
                var            vs        = shaderFactory.compileFromFile(resources, "TeapotVS.hlsl", sourceInfo, "Teapot VS");
                stateFactory.graphicsVertexShader(vs);

                // Create dynamic uniform buffer that will store our transformation matrix. Dynamic buffers can be frequently updated by the CPU.
                vsConstants = device.CreateDynamicUniformBuffer <VsConstants>("VS constants CB");

                // Create a pixel shader
                sourceInfo.shaderType = ShaderType.Pixel;
                var ps = shaderFactory.compileFromFile(resources, "TeapotPS.hlsl", sourceInfo, "Teapot PS");
                stateFactory.graphicsPixelShader(ps);

                // Define vertex shader input layout

                // Attribute 0 - vertex position
                LayoutElement elt = new LayoutElement(false)
                {
                    InputIndex    = 0,
                    BufferSlot    = 0,
                    NumComponents = 3,
                    ValueType     = GpuValueType.Float32,
                    IsNormalized  = false
                };
                stateFactory.graphicsLayoutElement(elt);

                // Attribute 1 - normals, they are generated by STL loader because we ask for them.
                elt.InputIndex    = 1;
                elt.NumComponents = 3;
                stateFactory.graphicsLayoutElement(elt);

                // Define variable type that will be used by default
                PSODesc.ResourceLayout.DefaultVariableType = ShaderResourceVariableType.Static;

                stateFactory.apply(ref PSODesc);
                pipelineState = device.CreatePipelineState(ref PSODesc);
            }

            // Since we did not explicitly specify the type for 'Constants' variable, default
            // type (SHADER_RESOURCE_VARIABLE_TYPE_STATIC) will be used. Static variables never
            // change and are bound directly through the pipeline state object.
            pipelineState.GetStaticVariableByName(ShaderType.Vertex, "Constants").Set(vsConstants);

            // Create a shader resource binding object and bind all static resources in it
            resourceBinding = pipelineState.CreateShaderResourceBinding(true);
        }