internal unsafe IntPtr CreateBlenderForDevice(LightDevice device) { if (!_alpha) { return(IntPtr.Zero); } BlendDescription d = new BlendDescription(); d.RenderTarget0.BlendEnable = 1; //true d.RenderTarget0.SrcBlend = 5; //D3D11_BLEND_SRC_ALPHA d.RenderTarget0.DestBlend = 6; //D3D11_BLEND_INV_SRC_ALPHA d.RenderTarget0.BlendOp = 1; //D3D11_BLEND_OP_ADD d.RenderTarget0.SrcBlendAlpha = 2; //D3D11_BLEND_ONE d.RenderTarget0.DestBlendAlpha = 1; //D3D11_BLEND_ZERO d.RenderTarget0.BlendOpAlpha = 1; //D3D11_BLEND_OP_ADD d.RenderTarget0.RenderTargetWriteMask = 15; //D3D11_COLOR_WRITE_ENABLE_ALL Device.CreateBlendState(device.DevicePtr, new IntPtr(&d), out var ret).Check(); return(ret); }
private static GraphicsPipelineStateDescription CreateGraphicsPipelineStateDescription(GraphicsDevice device, RootSignature rootSignature, InputElementDescription[] inputElements, byte[] vertexShader, byte[] pixelShader, byte[]?geometryShader, byte[]?hullShader, byte[]?domainShader) { RasterizerDescription rasterizerDescription = RasterizerDescription.CullNone; rasterizerDescription.FrontCounterClockwise = true; BlendDescription blendDescription = BlendDescription.AlphaBlend; InputLayoutDescription inputLayoutDescription = new InputLayoutDescription(inputElements.Select(i => Unsafe.As <InputElementDescription, Vortice.Direct3D12.InputElementDescription>(ref i)).ToArray()); GraphicsPipelineStateDescription pipelineStateDescription = new GraphicsPipelineStateDescription { InputLayout = inputLayoutDescription, RootSignature = rootSignature.NativeRootSignature, VertexShader = vertexShader, PixelShader = pixelShader, GeometryShader = geometryShader, HullShader = hullShader, DomainShader = domainShader, RasterizerState = rasterizerDescription, BlendState = blendDescription, PrimitiveTopologyType = PrimitiveTopologyType.Triangle, StreamOutput = new StreamOutputDescription() }; DepthStencilView?depthStencilBuffer = device.CommandList.DepthStencilBuffer; if (depthStencilBuffer != null) { pipelineStateDescription.DepthStencilFormat = (Format)depthStencilBuffer.Resource.Description.Format; } Format[] renderTargetFormats = new Format[device.CommandList.RenderTargets.Length]; for (int i = 0; i < renderTargetFormats.Length; i++) { renderTargetFormats[i] = (Format)device.CommandList.RenderTargets[i].Resource.Description.Format; } pipelineStateDescription.RenderTargetFormats = renderTargetFormats; return(pipelineStateDescription); }
private static GraphicsPipelineStateDescription CreateGraphicsPipelineStateDescription(GraphicsDevice device, InputElementDescription[] inputElements, ID3D12RootSignature rootSignature, ShaderBytecode vertexShader, ShaderBytecode pixelShader, ShaderBytecode geometryShader, ShaderBytecode hullShader, ShaderBytecode domainShader) { RasterizerDescription rasterizerDescription = RasterizerDescription.CullNone; rasterizerDescription.FrontCounterClockwise = true; BlendDescription blendDescription = BlendDescription.AlphaBlend; GraphicsPipelineStateDescription pipelineStateDescription = new GraphicsPipelineStateDescription { InputLayout = new InputLayoutDescription(inputElements), RootSignature = rootSignature, VertexShader = vertexShader, PixelShader = pixelShader, GeometryShader = geometryShader, HullShader = hullShader, DomainShader = domainShader, RasterizerState = rasterizerDescription, BlendState = blendDescription, PrimitiveTopologyType = PrimitiveTopologyType.Triangle, StreamOutput = new StreamOutputDescription() }; Texture?depthStencilBuffer = device.CommandList.DepthStencilBuffer; if (depthStencilBuffer != null) { pipelineStateDescription.DepthStencilFormat = (Format)depthStencilBuffer.Description.Format; } Format[] renderTargetFormats = new Format[device.CommandList.RenderTargets.Length]; for (int i = 0; i < renderTargetFormats.Length; i++) { renderTargetFormats[i] = (Format)((Texture)device.CommandList.RenderTargets[i]).Description.Format; } pipelineStateDescription.RenderTargetFormats = renderTargetFormats; return(pipelineStateDescription); }
BlendDescription blendStateAlpha() { BlendDescription blendDescription = new BlendDescription(Blend.SourceAlpha, Blend.InverseSourceAlpha, Blend.One, Blend.InverseSourceAlpha); return(blendDescription); }
GraphicsPipelineState makeTriangle(ResourceCreateContext ctx) { var triangleVertex = UniqueCreator.Graphics.Gpu.Shaders.triangle_vertex.Factory.Create(); var trianglePixel = UniqueCreator.Graphics.Gpu.Shaders.triangle_pixel.Factory.Create(); var description2 = new GraphicsPipelineStateDescription(); var rasterizerState = new RasterizerDescription(); var samples = new SampleDescription { Count = 1, Quality = 0 }; var depthStencil = new DepthStencilDescription { DepthEnable = true, StencilEnable = false, DepthWriteMask = DepthWriteMask.All, DepthFunc = ComparisonFunction.Less }; depthStencil.BackFace.StencilFailOperation = StencilOperation.Keep; depthStencil.BackFace.StencilPassOperation = StencilOperation.Keep; depthStencil.BackFace.StencilFunction = ComparisonFunction.Always; depthStencil.BackFace.StencilDepthFailOperation = StencilOperation.Keep; depthStencil.FrontFace = depthStencil.BackFace; depthStencil.StencilReadMask = 0xff; depthStencil.StencilWriteMask = 0xff; rasterizerState.CullMode = CullMode.Back; rasterizerState.FillMode = FillMode.Solid; rasterizerState.FrontCounterClockwise = true; rasterizerState.AntialiasedLineEnable = false; rasterizerState.ConservativeRaster = ConservativeRasterizationMode.Off; rasterizerState.DepthBias = 0; rasterizerState.DepthBiasClamp = 0.0f; rasterizerState.SlopeScaledDepthBias = 0.0f; rasterizerState.DepthClipEnable = true; rasterizerState.ForcedSampleCount = 0; rasterizerState.MultisampleEnable = false; var blendState = new BlendDescription { AlphaToCoverageEnable = false, IndependentBlendEnable = false }; var blend = new RenderTargetBlendDescription { BlendEnable = false, LogicOperationEnable = false, BlendOperation = BlendOperation.Add, BlendOperationAlpha = BlendOperation.Add, LogicOperation = LogicOperation.Clear, DestinationBlend = Blend.DestinationColor, DestinationBlendAlpha = Blend.DestinationAlpha, SourceBlend = Blend.SourceColor, SourceBlendAlpha = Blend.SourceAlpha, RenderTargetWriteMask = 0xF }; blendState.RenderTargets = new RenderTargetBlendDescription[] { blend }; description2.VS = triangleVertex; description2.PS = trianglePixel; description2.SampleMask = 0xFFFFFFFF; description2.RasterizerState = rasterizerState; description2.PrimitiveTopology = PrimitiveTopologyType.Triangle; description2.Samples = samples; description2.DepthStencilState = depthStencil; description2.DsvFormat = GraphicsFormat.D32_Single; description2.IbStripCutValue = IndexBufferStripCut.ValueDisabled; description2.BlendState = blendState; description2.RtvFormats.Add(GraphicsFormat.R8G8B8A8_UNORM); return(new GraphicsPipelineState(ctx, description2)); }
void CreateDeviceObjects() { var vertexShaderCode = @" cbuffer vertexBuffer : register(b0) { float4x4 ProjectionMatrix; }; struct VS_INPUT { float2 pos : POSITION; float4 col : COLOR0; float2 uv : TEXCOORD0; }; struct PS_INPUT { float4 pos : SV_POSITION; float4 col : COLOR0; float2 uv : TEXCOORD0; }; PS_INPUT main(VS_INPUT input) { PS_INPUT output; output.pos = mul(ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f)); output.col = input.col; output.uv = input.uv; return output; }"; Compiler.Compile(vertexShaderCode, "main", "vs", "vs_4_0", out vertexShaderBlob, out var errorBlob); if (vertexShaderBlob == null) { throw new Exception("error compiling vertex shader"); } vertexShader = device.CreateVertexShader(vertexShaderBlob.GetBytes()); var inputElements = new[] { new InputElementDescription("POSITION", 0, Format.R32G32_Float, 0, 0, InputClassification.PerVertexData, 0), new InputElementDescription("TEXCOORD", 0, Format.R32G32_Float, 8, 0, InputClassification.PerVertexData, 0), new InputElementDescription("COLOR", 0, Format.R8G8B8A8_UNorm, 16, 0, InputClassification.PerVertexData, 0), }; inputLayout = device.CreateInputLayout(inputElements, vertexShaderBlob); var constBufferDesc = new BufferDescription { SizeInBytes = VertexConstantBufferSize, Usage = Vortice.Direct3D11.Usage.Dynamic, BindFlags = BindFlags.ConstantBuffer, CpuAccessFlags = CpuAccessFlags.Write }; constantBuffer = device.CreateBuffer(constBufferDesc); var pixelShaderCode = @"struct PS_INPUT { float4 pos : SV_POSITION; float4 col : COLOR0; float2 uv : TEXCOORD0; }; sampler sampler0; Texture2D texture0; float4 main(PS_INPUT input) : SV_Target { float4 out_col = input.col * texture0.Sample(sampler0, input.uv); return out_col; }"; Compiler.Compile(pixelShaderCode, "main", "ps", "ps_4_0", out pixelShaderBlob, out errorBlob); if (pixelShaderBlob == null) { throw new Exception("error compiling pixel shader"); } pixelShader = device.CreatePixelShader(pixelShaderBlob.GetBytes()); var blendDesc = new BlendDescription { AlphaToCoverageEnable = false }; blendDesc.RenderTarget[0] = new RenderTargetBlendDescription { IsBlendEnabled = true, SourceBlend = Blend.SourceAlpha, DestinationBlend = Blend.InverseSourceAlpha, BlendOperation = BlendOperation.Add, SourceBlendAlpha = Blend.InverseSourceAlpha, DestinationBlendAlpha = Blend.Zero, BlendOperationAlpha = BlendOperation.Add, RenderTargetWriteMask = ColorWriteEnable.All }; blendState = device.CreateBlendState(blendDesc); var rasterDesc = new RasterizerDescription { FillMode = FillMode.Solid, CullMode = CullMode.None, ScissorEnable = true, DepthClipEnable = true }; rasterizerState = device.CreateRasterizerState(rasterDesc); var stencilOpDesc = new DepthStencilOperationDescription(StencilOperation.Keep, StencilOperation.Keep, StencilOperation.Keep, ComparisonFunction.Always); var depthDesc = new DepthStencilDescription { DepthEnable = false, DepthWriteMask = DepthWriteMask.All, DepthFunc = ComparisonFunction.Always, StencilEnable = false, FrontFace = stencilOpDesc, BackFace = stencilOpDesc }; depthStencilState = device.CreateDepthStencilState(depthDesc); CreateFontsTexture(); }
public bool Init(IntPtr WindowHandle) { var factory = DXGI.CreateDXGIFactory1 <IDXGIFactory1>(); var adapter = factory.GetAdapter1(0); var monitor = adapter.GetOutput(0); var modes = monitor.GetDisplayModeList(Format.R8G8B8A8_UNorm, DisplayModeEnumerationFlags.Interlaced); var rational = new Rational(0, 1); var adapterDescription = adapter.Description; //VideoCardMemory = adapterDescription.DedicatedVideoMemory >> 10 >> 10; //VideoCardDescription = adapterDescription.Description.Trim('\0'); monitor.Dispose(); adapter.Dispose(); var swapChainDesc = new SwapChainDescription() { BufferCount = 2, BufferDescription = new ModeDescription(1920, 1080, rational, Format.R8G8B8A8_UNorm), Usage = Usage.RenderTargetOutput, OutputWindow = WindowHandle, SampleDescription = new SampleDescription(1, 0), IsWindowed = true, Flags = SwapChainFlags.None, SwapEffect = SwapEffect.Discard }; // Create Device and DeviceContext ID3D11Device TempDevice = null; ID3D11DeviceContext TempDeviceContext = null; D3D11.D3D11CreateDevice(adapter, DriverType.Hardware, DeviceCreationFlags.None, null, out TempDevice, out TempDeviceContext); Device = TempDevice.QueryInterface <ID3D11Device1>(); DeviceContext = TempDeviceContext.QueryInterface <ID3D11DeviceContext1>(); TempDevice.Dispose(); TempDeviceContext.Dispose(); // Create SwapChain SwapChain = factory.CreateSwapChain(Device, swapChainDesc); factory.MakeWindowAssociation(WindowHandle, WindowAssociationFlags.IgnoreAltEnter); var backBuffer = SwapChain.GetBuffer <ID3D11Texture2D>(0); m_RenderTargetView = Device.CreateRenderTargetView(backBuffer); backBuffer.Dispose(); // Create blend state BlendDescription bsd = new BlendDescription() { AlphaToCoverageEnable = false,//true, IndependentBlendEnable = false, }; bsd.RenderTarget[0].BlendOperationAlpha = BlendOperation.Add; bsd.RenderTarget[0].BlendOperation = BlendOperation.Add; bsd.RenderTarget[0].DestinationBlendAlpha = Blend.One; bsd.RenderTarget[0].DestinationBlend = Blend.InverseSourceAlpha; bsd.RenderTarget[0].IsBlendEnabled = true; bsd.RenderTarget[0].RenderTargetWriteMask = ColorWriteEnable.All; bsd.RenderTarget[0].SourceBlendAlpha = Blend.Zero; bsd.RenderTarget[0].SourceBlend = Blend.SourceAlpha; bsd.AlphaToCoverageEnable = true; ID3D11BlendState bsAlpha = Device.CreateBlendState(bsd); // Set Blend State DeviceContext.OMSetBlendState(bsAlpha); BuildDepthStencilView(1920, 1080); // Create rasterizers m_RSDesc = new RasterizerDescription() { AntialiasedLineEnable = false, CullMode = CullMode.Back, DepthBias = 0, DepthBiasClamp = .0f, DepthClipEnable = false, FillMode = FillMode.Solid, FrontCounterClockwise = true, MultisampleEnable = true, ScissorEnable = false, SlopeScaledDepthBias = .0f }; m_RSCullSolid = Device.CreateRasterizerState(m_RSDesc); m_RSDesc.CullMode = CullMode.None; m_RSSolid = Device.CreateRasterizerState(m_RSDesc); m_RSDesc.FillMode = FillMode.Wireframe; m_RSWireFrame = Device.CreateRasterizerState(m_RSDesc); m_RSDesc.CullMode = CullMode.Back; m_RSCullWireFrame = Device.CreateRasterizerState(m_RSDesc); UpdateRasterizer(); return(true); }