Esempio n. 1
0
        void preparePipelines()
        {
            var inputAssemblyState = VkPipelineInputAssemblyStateCreateInfo.Alloc();

            inputAssemblyState[0].topology = VkPrimitiveTopology.TriangleList;
            inputAssemblyState[0].primitiveRestartEnable = false;

            var rasterizationState = VkPipelineRasterizationStateCreateInfo.Alloc();

            rasterizationState[0].polygonMode      = VkPolygonMode.Fill;
            rasterizationState[0].cullMode         = VkCullModeFlagBits.None;
            rasterizationState[0].frontFace        = VkFrontFace.CounterClockwise;
            rasterizationState[0].depthClampEnable = false;
            rasterizationState[0].lineWidth        = 1.0f;

            var blendAttachmentState = VkPipelineColorBlendAttachmentState.Alloc();

            blendAttachmentState[0].colorWriteMask = (VkColorComponentFlagBits)0xf;
            blendAttachmentState[0].blendEnable    = false;

            var colorBlendState = VkPipelineColorBlendStateCreateInfo.Alloc();

            colorBlendState[0].attachments.count   = 1;
            colorBlendState[0].attachments.pointer = blendAttachmentState;

            var depthStencilState = VkPipelineDepthStencilStateCreateInfo.Alloc();

            depthStencilState[0].depthTestEnable  = true;
            depthStencilState[0].depthWriteEnable = true;
            depthStencilState[0].depthCompareOp   = VkCompareOp.LessOrEqual;
            depthStencilState[0].front            = depthStencilState[0].back;
            depthStencilState[0].back.compareOp   = VkCompareOp.Always;

            var viewportState = VkPipelineViewportStateCreateInfo.Alloc();

            viewportState[0].viewports.count = 1;
            viewportState[0].scissors.count  = 1;

            var multisampleState = VkPipelineMultisampleStateCreateInfo.Alloc();

            multisampleState[0].rasterizationSamples = VkSampleCountFlagBits._1;

            var dynamicStateEnables = new VkDynamicState[] {
                VkDynamicState.Viewport,
                VkDynamicState.Scissor
            };
            var dynamicState = VkPipelineDynamicStateCreateInfo.Alloc();

            dynamicState[0].dynamicStates = dynamicStateEnables;

            // Load shaders
            var shaderStages = VkPipelineShaderStageCreateInfo.Alloc(2);

            shaderStages[0] = loadShader(getAssetPath() + "shaders/texture/texture.vert.spv", VkShaderStageFlagBits.Vertex);
            shaderStages[1] = loadShader(getAssetPath() + "shaders/texture/texture.frag.spv", VkShaderStageFlagBits.Fragment);

            var pipelineCreateInfo = VkGraphicsPipelineCreateInfo.Alloc();

            pipelineCreateInfo[0].layout            = pipelineLayout;
            pipelineCreateInfo[0].renderPass        = renderPass;
            pipelineCreateInfo[0].basePipelineIndex = -1;

            var vertexInputState = vDescription.inputState;

            pipelineCreateInfo[0].pVertexInputState   = vertexInputState;
            pipelineCreateInfo[0].pInputAssemblyState = inputAssemblyState;
            pipelineCreateInfo[0].pRasterizationState = rasterizationState;
            pipelineCreateInfo[0].pColorBlendState    = colorBlendState;
            pipelineCreateInfo[0].pMultisampleState   = multisampleState;
            pipelineCreateInfo[0].pViewportState      = viewportState;
            pipelineCreateInfo[0].pDepthStencilState  = depthStencilState;
            pipelineCreateInfo[0].pDynamicState       = dynamicState;
            pipelineCreateInfo[0].stages.count        = 2;
            pipelineCreateInfo[0].stages.pointer      = shaderStages;
            {
                VkPipeline pipeline;
                vkCreateGraphicsPipelines(device, pipelineCache, 1, pipelineCreateInfo, null, &pipeline);
                this.pipeline = pipeline;
            }
        }
Esempio n. 2
0
        void preparePipelines()
        {
            var inputAssemblyState = VkPipelineInputAssemblyStateCreateInfo.Alloc();

            inputAssemblyState->topology = VkPrimitiveTopology.TriangleList;
            inputAssemblyState->primitiveRestartEnable = false;

            var rasterizationState = VkPipelineRasterizationStateCreateInfo.Alloc();

            rasterizationState->polygonMode = VkPolygonMode.Fill;
            rasterizationState->cullMode    = VkCullModeFlagBits.None;
            rasterizationState->frontFace   = VkFrontFace.CounterClockwise;
            rasterizationState->lineWidth   = 1.0f;

            var blendAttachmentState = VkPipelineColorBlendAttachmentState.Alloc();

            blendAttachmentState->colorWriteMask = (VkColorComponentFlagBits)0xf;
            blendAttachmentState->blendEnable    = false;

            var colorBlendState = VkPipelineColorBlendStateCreateInfo.Alloc();

            colorBlendState->attachments.count   = 1;
            colorBlendState->attachments.pointer = blendAttachmentState;

            var depthStencilState = VkPipelineDepthStencilStateCreateInfo.Alloc();

            depthStencilState->depthTestEnable  = true;
            depthStencilState->depthWriteEnable = true;
            depthStencilState->depthCompareOp   = VkCompareOp.LessOrEqual;
            depthStencilState->front            = depthStencilState->back;
            depthStencilState->back.compareOp   = VkCompareOp.Always;

            var viewportState = VkPipelineViewportStateCreateInfo.Alloc();

            viewportState[0].viewports.count = 1;
            viewportState[0].scissors.count  = 1;

            var multisampleState = VkPipelineMultisampleStateCreateInfo.Alloc();

            multisampleState->rasterizationSamples = VkSampleCountFlagBits._1;

            var dynamicStateEnables = new VkDynamicState[] { VkDynamicState.Viewport, VkDynamicState.Scissor };
            var dynamicState        = VkPipelineDynamicStateCreateInfo.Alloc();

            dynamicState[0].dynamicStates = dynamicStateEnables;

            var shaderStages = VkPipelineShaderStageCreateInfo.Alloc(2);

            var pipelineCreateInfo = VkGraphicsPipelineCreateInfo.Alloc();

            pipelineCreateInfo->layout              = pipelineLayoutRadialBlur;
            pipelineCreateInfo->renderPass          = renderPass;
            pipelineCreateInfo->basePipelineIndex   = -1;
            pipelineCreateInfo->pInputAssemblyState = inputAssemblyState;
            pipelineCreateInfo->pRasterizationState = rasterizationState;
            pipelineCreateInfo->pColorBlendState    = colorBlendState;
            pipelineCreateInfo->pMultisampleState   = multisampleState;
            pipelineCreateInfo->pViewportState      = viewportState;
            pipelineCreateInfo->pDepthStencilState  = depthStencilState;
            pipelineCreateInfo->pDynamicState       = dynamicState;
            pipelineCreateInfo->stages.count        = 2;
            pipelineCreateInfo->stages.pointer      = shaderStages;

            // Radial blur pipeline
            shaderStages[0] = loadShader(getAssetPath() + "shaders/radialblur/radialblur.vert.spv", VkShaderStageFlagBits.Vertex);
            shaderStages[1] = loadShader(getAssetPath() + "shaders/radialblur/radialblur.frag.spv", VkShaderStageFlagBits.Fragment);
            // Empty vertex input state
            var emptyInputState = VkPipelineVertexInputStateCreateInfo.Alloc();

            pipelineCreateInfo->pVertexInputState = emptyInputState;
            pipelineCreateInfo->layout            = pipelineLayoutRadialBlur;
            // Additive blending
            blendAttachmentState->colorWriteMask      = (VkColorComponentFlagBits)0xF;
            blendAttachmentState->blendEnable         = true;
            blendAttachmentState->colorBlendOp        = VkBlendOp.Add;
            blendAttachmentState->srcColorBlendFactor = VkBlendFactor.One;
            blendAttachmentState->dstColorBlendFactor = VkBlendFactor.One;
            blendAttachmentState->alphaBlendOp        = VkBlendOp.Add;
            blendAttachmentState->srcAlphaBlendFactor = VkBlendFactor.SrcAlpha;
            blendAttachmentState->dstAlphaBlendFactor = VkBlendFactor.DstAlpha;
            {
                VkPipeline pipeline;
                vkCreateGraphicsPipelines(device, pipelineCache, 1, pipelineCreateInfo, null, &pipeline);
                this.pipelineRadialBlur = pipeline;
            }

            // No blending (for debug display)
            blendAttachmentState->blendEnable = false;
            {
                VkPipeline pipeline;
                vkCreateGraphicsPipelines(device, pipelineCache, 1, pipelineCreateInfo, null, &pipeline);
                this.pipelineOffscreenDisplay = pipeline;
            }

            // Phong pass
            pipelineCreateInfo->layout            = pipelineLayoutScene;
            shaderStages[0]                       = loadShader(getAssetPath() + "shaders/radialblur/phongpass.vert.spv", VkShaderStageFlagBits.Vertex);
            shaderStages[1]                       = loadShader(getAssetPath() + "shaders/radialblur/phongpass.frag.spv", VkShaderStageFlagBits.Fragment);
            pipelineCreateInfo->pVertexInputState = inputInfo;
            blendAttachmentState->blendEnable     = false;
            depthStencilState->depthWriteEnable   = true;
            {
                VkPipeline pipeline;
                vkCreateGraphicsPipelines(device, pipelineCache, 1, pipelineCreateInfo, null, &pipeline);
                this.pipelinePhongPass = pipeline;
            }

            // Color only pass (offscreen blur base)
            shaderStages[0] = loadShader(getAssetPath() + "shaders/radialblur/colorpass.vert.spv", VkShaderStageFlagBits.Vertex);
            shaderStages[1] = loadShader(getAssetPath() + "shaders/radialblur/colorpass.frag.spv", VkShaderStageFlagBits.Fragment);
            pipelineCreateInfo->renderPass = offscreenPass.renderPass;
            {
                VkPipeline pipeline;
                vkCreateGraphicsPipelines(device, pipelineCache, 1, pipelineCreateInfo, null, &pipeline);
                this.pipelineColorPass = pipeline;
            }
        }