public override void Render(ID3D11Device device, ID3D11DeviceContext deviceContext, Camera camera) { if (!DoRender) { return; } //if (!camera.CheckBBoxFrustum(Transform.TranslationVector, BoundingBox)) // return; VertexBufferView VertexBufferView = new VertexBufferView(vertexBuffer, Unsafe.SizeOf <VertexLayouts.NormalLayout.Vertex>(), 0); deviceContext.IASetVertexBuffers(0, VertexBufferView); deviceContext.IASetIndexBuffer(indexBuffer, Vortice.DXGI.Format.R32_UInt, 0); deviceContext.IASetPrimitiveTopology(PrimitiveTopology.TriangleList); deviceContext.PSSetShaderResource(2, AOTexture); for (int i = 0; i != LODs[0].ModelParts.Length; i++) { ModelPart Segment = LODs[0].ModelParts[i]; Segment.Shader.SetShaderParameters(device, deviceContext, new MaterialParameters(Segment.Material, SelectionColour.Normalize())); Segment.Shader.SetSceneVariables(deviceContext, Transform, camera); Segment.Shader.Render(deviceContext, PrimitiveTopology.TriangleList, (int)(Segment.NumFaces * 3), Segment.StartIndex); } }
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 void Draw(ID3D11Device device, ID3D11DeviceContext context, Span <D3D11_INPUT_ELEMENT_DESC> _layout) { if (!m_vertexBuffer) { using (var pin = PinPtr.Create(m_vertices)) { var desc = new D3D11_BUFFER_DESC { ByteWidth = (uint)m_vertices.Length, Usage = D3D11_USAGE._DEFAULT, BindFlags = (uint)D3D11_BIND_FLAG._VERTEX_BUFFER, }; var data = new D3D11_SUBRESOURCE_DATA { pSysMem = pin.Ptr }; device.CreateBuffer(ref desc, ref data, out m_vertexBuffer).ThrowIfFailed(); } } Span <IntPtr> pBufferTbl = stackalloc IntPtr[] { m_vertexBuffer.Ptr }; Span <uint> SizeTbl = stackalloc uint[] { (uint)m_vertexSize }; Span <uint> OffsetTbl = stackalloc uint[] { 0 }; context.IASetVertexBuffers(0, 1, ref MemoryMarshal.GetReference(pBufferTbl), ref MemoryMarshal.GetReference(SizeTbl), ref MemoryMarshal.GetReference(OffsetTbl)); if (!m_indexBuffer) { using (var pin = PinPtr.Create(m_indices)) { var desc = new D3D11_BUFFER_DESC { ByteWidth = (uint)m_indices.Length, Usage = D3D11_USAGE._DEFAULT, BindFlags = (uint)D3D11_BIND_FLAG._INDEX_BUFFER, }; var data = new D3D11_SUBRESOURCE_DATA { pSysMem = pin.Ptr }; device.CreateBuffer(ref desc, ref data, out m_indexBuffer).ThrowIfFailed(); } } context.IASetIndexBuffer(m_indexBuffer, m_indexFormat, 0); context.IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY._TRIANGLELIST); context.DrawIndexed((uint)m_indexCount, 0, 0); } } }
private void RenderLines(ID3D11DeviceContext InDeviceContext, Camera InCamera) { VertexBufferView BufferView = new VertexBufferView(VertexBuffer, Unsafe.SizeOf <VertexLayouts.BasicLayout.Vertex>(), 0); InDeviceContext.IASetVertexBuffers(0, BufferView); InDeviceContext.IASetIndexBuffer(IndexBuffer, Vortice.DXGI.Format.R16_UInt, 0); InDeviceContext.IASetPrimitiveTopology(PrimitiveTopology.LineStrip); BaseShader Shader = RenderStorageSingleton.Instance.ShaderManager.shaders[1]; Shader.SetSceneVariables(InDeviceContext, Matrix4x4.Identity, InCamera); Shader.Render(InDeviceContext, PrimitiveTopology.LineList, SizeToRender, 0); }
public override void Render(ID3D11Device device, ID3D11DeviceContext deviceContext, Camera camera) { if (!DoRender) { return; } VertexBufferView VertexBufferView = new VertexBufferView(vertexBuffer, Unsafe.SizeOf <VertexLayouts.BasicLayout.Vertex>(), 0); deviceContext.IASetVertexBuffers(0, VertexBufferView); deviceContext.IASetPrimitiveTopology(PrimitiveTopology.LineStrip); shader.SetSceneVariables(deviceContext, Transform, camera); shader.Render(deviceContext, PrimitiveTopology.LineStrip, vertices.Length, 0); }
public override void Render(ID3D11Device device, ID3D11DeviceContext deviceContext, Camera camera) { if (!DoRender) { return; } VertexBufferView VertexBufferView = new VertexBufferView(vertexBuffer, Unsafe.SizeOf <VertexLayouts.BasicLayout.Vertex>(), 0); deviceContext.IASetVertexBuffers(0, VertexBufferView); deviceContext.IASetIndexBuffer(indexBuffer, Vortice.DXGI.Format.R32_UInt, 0); deviceContext.IASetPrimitiveTopology(PrimitiveTopology.LineList); shader.SetSceneVariables(deviceContext, Transform, camera); shader.Render(deviceContext, PrimitiveTopology.LineList, ReadOnlyIndices.Length, 0); }
public override void Render(ID3D11Device device, ID3D11DeviceContext deviceContext, Camera camera) { if (!DoRender) { return; } VertexBufferView VertexBufferView = new VertexBufferView(vertexBuffer, Unsafe.SizeOf <VertexLayouts.CollisionLayout.Vertex>(), 0); deviceContext.IASetVertexBuffers(0, VertexBufferView); deviceContext.IASetIndexBuffer(indexBuffer, Vortice.DXGI.Format.R32_UInt, 0); deviceContext.IASetPrimitiveTopology(PrimitiveTopology.TriangleList); Shader.SetSceneVariables(deviceContext, Transform, camera); Shader.SetShaderParameters(device, deviceContext, new BaseShader.MaterialParameters(null, SelectionColour.Normalize())); Shader.Render(deviceContext, PrimitiveTopology.TriangleList, Indices.Length, 0); }
public static void IASetVertexBuffers(this ID3D11DeviceContext context, int startSlot, ID3D11Buffer[] vertexBuffers, int[] strides, int[] offsets) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (vertexBuffers == null) { throw new ArgumentNullException(nameof(vertexBuffers)); } if (strides == null) { throw new ArgumentNullException(nameof(strides)); } if (offsets == null) { throw new ArgumentNullException(nameof(offsets)); } if (vertexBuffers.Length == 0) { throw new ArgumentException(null, nameof(vertexBuffers)); } if (vertexBuffers.Length != strides.Length) { throw new ArgumentException(null, nameof(strides)); } if (vertexBuffers.Length != offsets.Length) { throw new ArgumentException(null, nameof(offsets)); } context.IASetVertexBuffers((uint)startSlot, vertexBuffers.Length, vertexBuffers, strides.Select(s => (uint)s).ToArray(), offsets.Select(s => (uint)s).ToArray()); }
public void Draw(ID3D11Device device, ID3D11DeviceContext context, Span <VertexAttribute> layout) { Span <IntPtr> buffers = stackalloc IntPtr[layout.Length]; Span <uint> strides = stackalloc uint[layout.Length]; Span <uint> offsets = stackalloc uint[layout.Length]; for (int i = 0; i < layout.Length; ++i) { var va = layout[i]; if (m_vertexBufferMap.TryGetValue(va.Semantic, out VertexBuffer vb)) { buffers[i] = vb.GetPtr(device); strides[i] = (uint)vb.Stride; } } context.IASetVertexBuffers(0, (uint)layout.Length, ref MemoryMarshal.GetReference(buffers), ref MemoryMarshal.GetReference(strides), ref MemoryMarshal.GetReference(offsets)); context.IASetIndexBuffer(m_indexBuffer.Buffer, m_indexFormat, 0); context.IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY._TRIANGLELIST); context.DrawIndexed((uint)m_indexCount, 0, 0); }
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(); }