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); } }
/// <summary>Setup premultiplied alpha blending on the first render target</summary> public static void premultipliedAlphaBlending(ref this PipelineStateDesc psoDesc) { RenderTargetBlendDesc blendDesc = new RenderTargetBlendDesc(false) { BlendEnable = true, SrcBlend = BlendFactor.One, DestBlend = BlendFactor.InvSrcAlpha, }; psoDesc.GraphicsPipeline.BlendDesc.setRenderTarget(blendDesc); }
public RenderTargetBlendDesc_NativeInterop(ref RenderTargetBlendDesc desc) { blendingEnabled = (byte)(desc.blendingEnabled ? 1 : 0); logicOperationEnabled = (byte)(desc.logicOperationEnabled ? 1 : 0); alphaBlendingSeparated = (byte)(desc.alphaBlendingSeparated ? 1 : 0); sourceFactor = desc.sourceFactor; destinationFactor = desc.destinationFactor; operation = desc.operation; sourceAlphaFactor = desc.sourceAlphaFactor; destinationAlphaFactor = desc.destinationAlphaFactor; alphaOperation = desc.alphaOperation; logicalOperation = desc.logicalOperation; writeMask = desc.writeMask; }
public MonoCursor(Context context, IRenderDevice device, iPipelineStateFactory stateFactory, iShaderFactory shaderFactory, iStorageFolder assets, IShader vs) { constantBuffer = device.CreateDynamicUniformBuffer <Vector4>("Cursor CB"); PipelineStateDesc desc = createStateDesc(context); // === First pass, setup that weird blending to invert colors === RenderTargetBlendDesc blendDesc = new RenderTargetBlendDesc(false) { BlendEnable = true, SrcBlend = BlendFactor.InvDestColor, DestBlend = BlendFactor.Zero, }; desc.GraphicsPipeline.BlendDesc.setRenderTarget(blendDesc); initState(stateFactory, shaderFactory, assets, vs, "CursorMaskPS.hlsl", "Invert bits"); stateFactory.apply(ref desc); psoInvert = device.CreatePipelineState(ref desc); psoInvert.GetStaticVariableByName(ShaderType.Vertex, "CursorCB").Set(constantBuffer); bindingsInvert = psoInvert.CreateShaderResourceBinding(true); textureVariableInvert = bindingsInvert.GetVariableByName(ShaderType.Pixel, "g_Texture"); // === Second pass, normal alpha blending === desc.premultipliedAlphaBlending(); initState(stateFactory, shaderFactory, assets, vs, "CursorColorPS.hlsl", "Monochrome cursor"); stateFactory.apply(ref desc); psoRgb = device.CreatePipelineState(ref desc); psoRgb.GetStaticVariableByName(ShaderType.Vertex, "CursorCB").Set(constantBuffer); bindingsRgb = psoRgb.CreateShaderResourceBinding(true); textureVariableRgb = bindingsRgb.GetVariableByName(ShaderType.Pixel, "g_Texture"); position = default; }
protected override unsafe ID3D12PipelineState *CreatePipelineState() { using ComPtr <ID3D10Blob> pixelShader = null; using ComPtr <ID3D10Blob> vertexShader = null; var compileFlags = 0u; #if DEBUG // Enable better shader debugging with the graphics debugging tools. compileFlags |= (1 << 0) | (1 << 2); #endif fixed(char *fileName = GetAssetFullPath(@"HelloTriangle.hlsl")) { var entryPoint = 0x00006E69614D5356; // VSMain var target = 0x0000305F355F7376; // vs_5_0 ID3D10Blob *errorMsgs; SilkMarshal.ThrowHResult ( _d3d.CompileFromFile ( (char *)fileName, pDefines: null, pInclude: null, (byte *)&entryPoint, (byte *)&target, compileFlags, Flags2: 0, vertexShader.GetAddressOf(), ppErrorMsgs: &errorMsgs ) ); entryPoint = 0x00006E69614D5350; // PSMain target = 0x0000305F355F7370; // ps_5_0 SilkMarshal.ThrowHResult ( _d3d.CompileFromFile ( (char *)fileName, pDefines: null, pInclude: null, (byte *)&entryPoint, (byte *)&target, compileFlags, Flags2: 0, pixelShader.GetAddressOf(), ppErrorMsgs: &errorMsgs ) ); } // Define the vertex input layout. const int InputElementDescsCount = 2; var semanticName0 = stackalloc ulong[2] { 0x4E4F495449534F50, // POSITION 0x0000000000000000, }; var semanticName1 = stackalloc ulong[1] { 0x000000524F4C4F43, // COLOR }; var inputElementDescs = stackalloc InputElementDesc[InputElementDescsCount] { new InputElementDesc { SemanticName = (byte *)semanticName0, Format = Format.FormatR32G32B32Float, InputSlotClass = InputClassification.InputClassificationPerVertexData, }, new InputElementDesc { SemanticName = (byte *)semanticName1, Format = Format.FormatR32G32B32A32Float, AlignedByteOffset = 12, InputSlotClass = InputClassification.InputClassificationPerVertexData, }, }; var defaultRenderTargetBlend = new RenderTargetBlendDesc() { BlendEnable = 0, LogicOpEnable = 0, SrcBlend = Blend.BlendOne, DestBlend = Blend.BlendZero, BlendOp = BlendOp.BlendOpAdd, SrcBlendAlpha = Blend.BlendOne, DestBlendAlpha = Blend.BlendZero, BlendOpAlpha = BlendOp.BlendOpAdd, LogicOp = LogicOp.LogicOpNoop, RenderTargetWriteMask = (byte)ColorWriteEnable.ColorWriteEnableAll }; var defaultStencilOp = new DepthStencilopDesc { StencilFailOp = StencilOp.StencilOpKeep, StencilDepthFailOp = StencilOp.StencilOpKeep, StencilPassOp = StencilOp.StencilOpKeep, StencilFunc = ComparisonFunc.ComparisonFuncAlways }; // Describe and create the graphics pipeline state object (PSO). var psoDesc = new GraphicsPipelineStateDesc { InputLayout = new InputLayoutDesc { PInputElementDescs = inputElementDescs, NumElements = InputElementDescsCount, }, PRootSignature = RootSignature, VS = new ShaderBytecode(vertexShader.Get().GetBufferPointer(), vertexShader.Get().GetBufferSize()), PS = new ShaderBytecode(pixelShader.Get().GetBufferPointer(), pixelShader.Get().GetBufferSize()), RasterizerState = new RasterizerDesc { FillMode = FillMode.FillModeSolid, CullMode = CullMode.CullModeBack, FrontCounterClockwise = 0, DepthBias = D3D12.DefaultDepthBias, DepthBiasClamp = 0, SlopeScaledDepthBias = 0, DepthClipEnable = 1, MultisampleEnable = 0, AntialiasedLineEnable = 0, ForcedSampleCount = 0, ConservativeRaster = ConservativeRasterizationMode.ConservativeRasterizationModeOff, }, BlendState = new BlendDesc { AlphaToCoverageEnable = 0, IndependentBlendEnable = 0, RenderTarget = new BlendDesc.RenderTargetBuffer() { [0] = defaultRenderTargetBlend, [1] = defaultRenderTargetBlend, [2] = defaultRenderTargetBlend, [3] = defaultRenderTargetBlend, [4] = defaultRenderTargetBlend, [5] = defaultRenderTargetBlend, [6] = defaultRenderTargetBlend, [7] = defaultRenderTargetBlend } }, DepthStencilState = new DepthStencilDesc { DepthEnable = 1, DepthWriteMask = DepthWriteMask.DepthWriteMaskAll, DepthFunc = ComparisonFunc.ComparisonFuncLess, StencilEnable = 0, StencilReadMask = D3D12.DefaultStencilReadMask, StencilWriteMask = D3D12.DefaultStencilWriteMask, FrontFace = defaultStencilOp, BackFace = defaultStencilOp }, SampleMask = uint.MaxValue, PrimitiveTopologyType = PrimitiveTopologyType.PrimitiveTopologyTypeTriangle, NumRenderTargets = 1, SampleDesc = new SampleDesc(count: 1, quality: 0), }; psoDesc.DepthStencilState.DepthEnable = 0; psoDesc.RTVFormats[0] = Format.FormatR8G8B8A8Unorm; ID3D12PipelineState *pipelineState; var iid = ID3D12PipelineState.Guid; SilkMarshal.ThrowHResult(D3DDevice->CreateGraphicsPipelineState(&psoDesc, &iid, (void **)&pipelineState)); return(pipelineState); }