public void Setup(ID3D11Device device, ID3D11DeviceContext context) { if (!m_vertexAttributes.Any()) { return; } if (!m_inputLayout) { var hr = device.CreateVertexShader(m_vs_blob.GetBufferPointer(), m_vs_blob.GetBufferSize(), null, out m_vs); hr.ThrowIfFailed(); hr = device.CreatePixelShader(m_ps_blob.GetBufferPointer(), m_ps_blob.GetBufferSize(), null, out m_ps); hr.ThrowIfFailed(); hr = device.CreateInputLayout(ref m_layout[0], (uint)m_layout.Length, m_vs_blob.GetBufferPointer(), m_vs_blob.GetBufferSize(), out m_inputLayout); hr.ThrowIfFailed(); } // setup shader pipeline Span <IntPtr> classes = stackalloc IntPtr[] { }; context.VSSetShader(m_vs, ref MemoryMarshal.GetReference(classes), (uint)classes.Length); context.PSSetShader(m_ps, ref MemoryMarshal.GetReference(classes), (uint)classes.Length); context.IASetInputLayout(m_inputLayout); }
void SetupRenderState(ImDrawDataPtr drawData, ID3D11DeviceContext ctx) { var viewport = new Viewport { Width = drawData.DisplaySize.X, Height = drawData.DisplaySize.Y, MinDepth = 0.0f, MaxDepth = 1.0f, }; ctx.RSSetViewports(viewport); int stride = sizeof(ImDrawVert); int offset = 0; ctx.IASetInputLayout(inputLayout); ctx.IASetVertexBuffers(0, 1, new[] { vertexBuffer }, new[] { stride }, new[] { offset }); ctx.IASetIndexBuffer(indexBuffer, sizeof(ImDrawIdx) == 2 ? Format.R16_UInt : Format.R32_UInt, 0); ctx.IASetPrimitiveTopology(PrimitiveTopology.TriangleList); ctx.VSSetShader(vertexShader); ctx.VSSetConstantBuffers(0, constantBuffer); ctx.PSSetShader(pixelShader); ctx.PSSetSamplers(0, fontSampler); ctx.GSSetShader(null); ctx.HSSetShader(null); ctx.DSSetShader(null); ctx.CSSetShader(null); ctx.OMSetBlendState(blendState); ctx.OMSetDepthStencilState(depthStencilState); ctx.RSSetState(rasterizerState); }
public virtual void Render(ID3D11DeviceContext context, PrimitiveTopology type, int size, uint offset) { context.IASetInputLayout(Layout); // set shaders only if available if (OurVertexShader != null) { context.VSSetShader(OurVertexShader); } if (OurVertexShader != null) { context.PSSetShader(OurPixelShader); context.PSSetSampler(0, SamplerState); } if (OurVertexShader != null) { context.GSSetShader(OurGeometryShader); } context.DrawIndexed(size, (int)offset, 0); Profiler.NumDrawCallsThisFrame++; }
public static void PSSetShader(this ID3D11DeviceContext context, ID3D11PixelShader pixelShader, ID3D11ClassInstance[] classInstances = null) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (pixelShader == null) { throw new ArgumentNullException(nameof(pixelShader)); } context.PSSetShader(pixelShader, classInstances, (classInstances?.Length).GetValueOrDefault()); }
public override void Render(ID3D11DeviceContext deviceContext, PrimitiveTopology type, int size, uint offset) { deviceContext.IASetInputLayout(Layout); deviceContext.VSSetShader(OurVertexShader); deviceContext.PSSetShader(OurPixelShader); switch (type) { case PrimitiveTopology.LineList: case PrimitiveTopology.TriangleList: deviceContext.DrawIndexed(size, (int)offset, 0); break; case PrimitiveTopology.LineStrip: deviceContext.Draw(size, 0); break; default: break; } Profiler.NumDrawCallsThisFrame++; }
protected override void SetPipelineStateImpl(PipelineState pipelineState) { var pipelineStateD3D11 = (PipelineStateD3D11)pipelineState; if (pipelineState.IsCompute) { var computeShader = pipelineStateD3D11.ComputeShader; D3D11Context.CSSetShader(computeShader); } else { var blendState = pipelineStateD3D11.BlendState; if (_boundBlendState != blendState) { _boundBlendState = blendState; D3D11Context.OMSetBlendState(blendState, _boundBlendColor); } var depthStencilState = pipelineStateD3D11.DepthStencilState; if (_boundDepthStencilState != depthStencilState) { _boundDepthStencilState = depthStencilState; D3D11Context.OMSetDepthStencilState(depthStencilState, _boundStencilReference); } var rasterizerState = pipelineStateD3D11.RasterizerState; if (_boundRasterizerState != rasterizerState) { _boundRasterizerState = rasterizerState; D3D11Context.RSSetState(rasterizerState); } var primitiveTopology = pipelineStateD3D11.PrimitiveTopology; if (_boundPrimitiveTopology != primitiveTopology) { _boundPrimitiveTopology = primitiveTopology; D3D11Context.IASetPrimitiveTopology(primitiveTopology); } var inputLayout = pipelineStateD3D11.InputLayout; if (_boundInputLayout != inputLayout) { _boundInputLayout = inputLayout; D3D11Context.IASetInputLayout(_boundInputLayout); } var vertexShader = pipelineStateD3D11.VertexShader; if (_boundVertexShader != vertexShader) { _boundVertexShader = vertexShader; D3D11Context.VSSetShader(vertexShader); } var geometryShader = pipelineStateD3D11.GeometryShader; if (_boundGeometryShader != geometryShader) { _boundGeometryShader = geometryShader; D3D11Context.GSSetShader(geometryShader); } var hullShader = pipelineStateD3D11.HullShader; if (_boundHullShader != hullShader) { _boundHullShader = hullShader; D3D11Context.HSSetShader(hullShader); } var domainShader = pipelineStateD3D11.DomainShader; if (_boundDomainShader != domainShader) { _boundDomainShader = domainShader; D3D11Context.DSSetShader(domainShader); } var pixelShader = pipelineStateD3D11.PixelShader; if (_boundPixelShader != pixelShader) { _boundPixelShader = pixelShader; D3D11Context.PSSetShader(pixelShader); } } }
static void Main() { ReferenceTracker.TrackReferences = true; Form form = new Form(); IDXGIFactory factory = DXGI.CreateFactory(); IDXGIAdapter adapter = null; factory.EnumAdapters(0, out adapter); DXGI_SWAP_CHAIN_DESC swapChainDescription = new DXGI_SWAP_CHAIN_DESC { BufferCount = 1, BufferDesc = new DXGI_MODE_DESC { Format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM, Height = form.ClientSize.Height, RefreshRate = new DXGI_RATIONAL { Denominator = 1, Numerator = 60 }, Scaling = DXGI_MODE_SCALING.DXGI_MODE_SCALING_UNSPECIFIED, ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER.DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED, Width = form.ClientSize.Width }, BufferUsage = (int)DXGI_USAGE.DXGI_USAGE_RENDER_TARGET_OUTPUT, Flags = 0, OutputWindow = form.Handle, SampleDesc = new DXGI_SAMPLE_DESC { Count = 1, Quality = 0 }, SwapEffect = DXGI_SWAP_EFFECT.DXGI_SWAP_EFFECT_DISCARD, Windowed = true }; ID3D11Device device = SlimDX.Direct3D11.Direct3D11.CreateDevice(adapter); IDXGISwapChain swapChain = null; factory.CreateSwapChain(device, swapChainDescription, out swapChain); ID3D11Texture2D backbuffer = swapChain.GetBuffer <ID3D11Texture2D>(0); ID3D11RenderTargetView view = null; device.CreateRenderTargetView(backbuffer, null, out view); ID3DBlob vertexShaderBytecode = ShaderCompiler.CompileFromString(File.ReadAllText("MiniTri11.fx"), "MiniTri11.fx", "VS", "vs_4_0"); ID3DBlob pixelShaderBytecode = ShaderCompiler.CompileFromString(File.ReadAllText("MiniTri11.fx"), "MiniTri11.fx", "PS", "ps_4_0"); D3D11_INPUT_ELEMENT_DESC[] inputElements = new[] { new D3D11_INPUT_ELEMENT_DESC { SemanticName = "POSITION", AlignedByteOffset = 0, SemanticIndex = 0, Format = DXGI_FORMAT.DXGI_FORMAT_R32G32B32A32_FLOAT, InstanceDataStepRate = 0, InputSlot = 0, InputSlotClass = D3D11_INPUT_CLASSIFICATION.D3D11_INPUT_PER_VERTEX_DATA }, new D3D11_INPUT_ELEMENT_DESC { SemanticName = "COLOR", AlignedByteOffset = 16, SemanticIndex = 0, Format = DXGI_FORMAT.DXGI_FORMAT_R32G32B32A32_FLOAT, InstanceDataStepRate = 0, InputSlot = 0, InputSlotClass = D3D11_INPUT_CLASSIFICATION.D3D11_INPUT_PER_VERTEX_DATA } }; ID3DBlob inputSignature; ShaderCompiler.D3DGetInputSignatureBlob(vertexShaderBytecode.GetBufferPointer(), vertexShaderBytecode.GetBufferSize(), out inputSignature); ID3D11InputLayout inputLayout; device.CreateInputLayout(inputElements, inputElements.Length, inputSignature.GetBufferPointer(), inputSignature.GetBufferSize(), out inputLayout); ByteBuffer vertexData = new ByteBuffer(3 * 32); vertexData.Write(0 * Vector4.SizeInBytes, new Vector4(0.0f, 0.5f, 0.5f, 1.0f)); vertexData.Write(1 * Vector4.SizeInBytes, new Vector4(1.0f, 0.0f, 0.0f, 1.0f)); vertexData.Write(2 * Vector4.SizeInBytes, new Vector4(0.5f, -0.5f, 0.5f, 1.0f)); vertexData.Write(3 * Vector4.SizeInBytes, new Vector4(0.0f, 1.0f, 0.0f, 1.0f)); vertexData.Write(4 * Vector4.SizeInBytes, new Vector4(-0.5f, -0.5f, 0.5f, 1.0f)); vertexData.Write(5 * Vector4.SizeInBytes, new Vector4(0.0f, 0.0f, 1.0f, 1.0f)); D3D11_BUFFER_DESC vertexBufferDescription = new D3D11_BUFFER_DESC { BindFlags = 1, //vertex buffer ByteWidth = 3 * 32, CPUAccessFlags = 0, MiscFlags = 0, Usage = D3D11_USAGE.D3D11_USAGE_DEFAULT, StructureByteStride = 0 }; ID3D11Buffer vertexBuffer; D3D11_SUBRESOURCE_DATA srd = new D3D11_SUBRESOURCE_DATA { pSysMem = vertexData.Pin(), SysMemPitch = 0, SysMemSlicePitch = 0 }; device.CreateBuffer(vertexBufferDescription, srd, out vertexBuffer); vertexData.Unpin(); RenderLoop loop = new RenderLoop(); ID3D11DeviceContext context = null; device.GetImmediateContext(out context); ID3D11VertexShader vertexShader; ID3D11PixelShader pixelShader; device.CreateVertexShader(vertexShaderBytecode.GetBufferPointer(), vertexShaderBytecode.GetBufferSize(), null, out vertexShader); device.CreatePixelShader(pixelShaderBytecode.GetBufferPointer(), pixelShaderBytecode.GetBufferSize(), null, out pixelShader); context.IASetInputLayout(inputLayout); context.VSSetShader(vertexShader, null, 0); context.PSSetShader(pixelShader, null, 0); context.IASetPrimitiveTopology(4); //triangle list context.IASetVertexBuffers(0, 1, new ID3D11Buffer[] { vertexBuffer }, new int[] { 32 }, new int[] { 0 }); context.OMSetRenderTargets(1, new ID3D11RenderTargetView[] { view }, IntPtr.Zero); D3D11_VIEWPORT vp = new D3D11_VIEWPORT { Height = form.ClientSize.Height, Width = form.ClientSize.Width, TopLeftX = 0, TopLeftY = 0, MinDepth = 0.0f, MaxDepth = 1.0f }; context.RSSetViewports(1, new D3D11_VIEWPORT [] { vp }); loop.Run(form, () => { var clearColor = new SlimDX.Color4 { R = 0.0f, G = 0.0f, B = 0.0f, A = 1.0f }; context.ClearRenderTargetView(view, clearColor); context.Draw(3, 0); swapChain.Present(0, 0); }); view.ReleaseReference(); backbuffer.ReleaseReference(); swapChain.ReleaseReference(); device.ReleaseReference(); adapter.ReleaseReference(); factory.ReleaseReference(); }