static void setupDepthState(ref PipelineStateDesc desc, eShaderMacros macros) { // Enable depth for both opaque and transparent passes.. desc.GraphicsPipeline.DepthStencilDesc.DepthEnable = true; desc.GraphicsPipeline.DepthStencilDesc.DepthFunc = ComparisonFunction.Less; // ..and set it so the depth is written on opaque pass, but readonly on transparent one. // With some luck, this should allow early Z rejection on both passes, saving tremendous amount of pixel shader invocations and fillrate bandwidth. if (macros.HasFlag(eShaderMacros.OpaquePass)) { desc.GraphicsPipeline.DepthStencilDesc.DepthWriteEnable = true; } else { desc.GraphicsPipeline.DepthStencilDesc.DepthWriteEnable = false; // desc.premultipliedAlphaBlending(); RenderTargetBlendDesc blendDesc = new RenderTargetBlendDesc(false) { BlendEnable = true, SrcBlend = BlendFactor.One, DestBlend = BlendFactor.InvSrcAlpha, SrcBlendAlpha = BlendFactor.One, DestBlendAlpha = BlendFactor.One, BlendOpAlpha = BlendOperation.Max, }; desc.GraphicsPipeline.BlendDesc.setRenderTarget(blendDesc); } }
public VrmacStateBase(GpuResources resources, IRenderDevice device, ref PipelineStateDesc desc, iPipelineStateFactory stateFactory, eShaderMacros macros) { this.resources = resources; shaderMacros = macros; layoutUniforms(stateFactory); stateFactory.setName($"2D state { macros }"); stateFactory.layoutVariable(ShaderType.Vertex, ShaderResourceVariableType.Mutable, varPaletteTexture); stateFactory.layoutVariable(ShaderType.Pixel, ShaderResourceVariableType.Mutable, varPaletteTexture); if (macros.HasFlag(eShaderMacros.TextureAtlas)) { stateFactory.layoutVariable(ShaderType.Pixel, ShaderResourceVariableType.Mutable, varSpriteAtlas); SamplerDesc samplerDesc = new SamplerDesc(false); stateFactory.layoutStaticSampler(ShaderType.Pixel, ref samplerDesc, varSpriteAtlas); resources.context.drawDevice.textureAtlas.resized.add(this, dropResourceBindings); } if (macros.HasFlag(eShaderMacros.TextRendering)) { stateFactory.layoutVariable(ShaderType.Pixel, ShaderResourceVariableType.Mutable, varGrayscaleFontAtlas); SamplerDesc samplerDesc = new SamplerDesc(false); stateFactory.layoutStaticSampler(ShaderType.Pixel, ref samplerDesc, varGrayscaleFontAtlas); stateFactory.layoutVariable(ShaderType.Pixel, ShaderResourceVariableType.Mutable, varCleartypeFontAtlas); resources.initTextCBuffer(); } declareResources(stateFactory); stateFactory.apply(ref desc); setupDepthState(ref desc, macros); pipelineState = device.CreatePipelineState(ref desc); resources.context.swapChainResized.add(this, (CSize s, double d) => dropResourceBindings()); resources.paletteTexture.textureResized.add(this, dropResourceBindings); }
void layoutUniforms(iPipelineStateFactory stateFactory) { // See #if preprocessor expression in utils.hlsli around that buffer bool hasConstants = !shaderMacros.HasFlag(eShaderMacros.OpaquePass) || shaderMacros.HasFlag(eShaderMacros.TextRendering); if (hasConstants) { stateFactory.layoutVariable(ShaderType.Vertex, ShaderResourceVariableType.Mutable, varStaticConstantsBuffer); stateFactory.layoutVariable(ShaderType.Pixel, ShaderResourceVariableType.Mutable, varStaticConstantsBuffer); } }
VrmacStateBase compileState(eShaderMacros macros) { using (var dev = resources.context.renderContext.device) using (var stateFactory = dev.CreatePipelineStateFactory()) { PipelineStateDesc desc = createStateDesc(); setupIdLayout(stateFactory); compileShaders(dev, stateFactory, "draw", macros); if (macros.HasFlag(eShaderMacros.FewDrawCalls)) { return(new FewDrawCallsState(resources, dev, ref desc, stateFactory, macros)); } return(new MoreDrawCallsState(resources, dev, ref desc, stateFactory, macros)); } }
static string macroValue(eShaderMacros vals, eShaderMacros bit) { return(vals.HasFlag(bit) ? "1" : "0"); }