public void Update(DeviceContext3 context) { _modelConstantBufferData.model = _transformMatrix; if (!NeedsUpdate) { return; } context.UpdateSubresource(ref _modelConstantBufferData, _modelConstantBuffer); NeedsUpdate = false; }
public void Render(DeviceContext3 context) { context.VertexShader.SetConstantBuffers(0, _modelConstantBuffer); var stride = Utilities.SizeOf <VertexPositionColor>(); var offset = 0; var bufferBinding = new VertexBufferBinding(_vertexBuffer, stride, offset); context.InputAssembler.SetVertexBuffers(0, bufferBinding); context.InputAssembler.SetIndexBuffer( _indexBuffer, Format.R16_UInt, 0); context.DrawIndexedInstanced(_indexCount, 2, 0, 0, 0); }
internal void Render(DeviceContext3 context, InputLayout inputLayout, VertexShader vertexShader, bool usingVprtShaders, GeometryShader geometryShader, PixelShader pixelShader) { //Update the texture context.PixelShader.SetShaderResource(0, textureView); // Each vertex is one instance of the VertexPositionColor struct. int stride = SharpDX.Utilities.SizeOf <VertexPositionTexture>(); int offset = 0; var bufferBinding = new SharpDX.Direct3D11.VertexBufferBinding(this.vertexBuffer, stride, offset); context.InputAssembler.SetVertexBuffers(0, bufferBinding); context.InputAssembler.SetIndexBuffer( this.indexBuffer, SharpDX.DXGI.Format.R16_UInt, // Each index is one 16-bit unsigned integer (short). 0); context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList; context.InputAssembler.InputLayout = inputLayout; // Attach the vertex shader. context.VertexShader.SetShader(vertexShader, null, 0); // Apply the model constant buffer to the vertex shader. context.VertexShader.SetConstantBuffers(0, this.modelConstantBuffer); if (!usingVprtShaders) { // On devices that do not support the D3D11_FEATURE_D3D11_OPTIONS3:: // VPAndRTArrayIndexFromAnyShaderFeedingRasterizer optional feature, // a pass-through geometry shader is used to set the render target // array index. context.GeometryShader.SetShader(geometryShader, null, 0); } // Attach the pixel shader. context.PixelShader.SetShader(pixelShader, null, 0); // Draw the objects. context.DrawIndexedInstanced( indexCount, // Index count per instance. 2, // Instance count. 0, // Start index location. 0, // Base vertex location. 0 // Start instance location. ); }
/// <summary> /// Configures the Direct3D device, and stores handles to it and the device context. /// </summary> private void CreateDeviceResources() { DisposeDeviceAndContext(); // This flag adds support for surfaces with a different color channel ordering // than the API default. It is required for compatibility with Direct2D. DeviceCreationFlags creationFlags = DeviceCreationFlags.BgraSupport; #if DEBUG if (DirectXHelper.SdkLayersAvailable()) { // If the project is in a debug build, enable debugging via SDK Layers with this flag. creationFlags |= DeviceCreationFlags.Debug; } #endif // This array defines the set of DirectX hardware feature levels this app will support. // Note the ordering should be preserved. // Note that HoloLens supports feature level 11.1. The HoloLens emulator is also capable // of running on graphics cards starting with feature level 10.0. FeatureLevel[] featureLevels = { FeatureLevel.Level_12_1, FeatureLevel.Level_12_0, FeatureLevel.Level_11_1, FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0 }; // Create the Direct3D 11 API device object and a corresponding context. try { if (null != dxgiAdapter) { using (var device = new Device(dxgiAdapter, creationFlags, featureLevels)) { // Store pointers to the Direct3D 11.1 API device. d3dDevice = this.ToDispose(device.QueryInterface <Device3>()); } } else { using (var device = new Device(DriverType.Hardware, creationFlags, featureLevels)) { // Store a pointer to the Direct3D device. d3dDevice = this.ToDispose(device.QueryInterface <Device3>()); } } } catch { // If the initialization fails, fall back to the WARP device. // For more information on WARP, see: // http://go.microsoft.com/fwlink/?LinkId=286690 using (var device = new Device(DriverType.Warp, creationFlags, featureLevels)) { d3dDevice = this.ToDispose(device.QueryInterface <Device3>()); } } // Cache the feature level of the device that was created. d3dFeatureLevel = d3dDevice.FeatureLevel; // Store a pointer to the Direct3D immediate context. d3dContext = this.ToDispose(d3dDevice.ImmediateContext3); // Acquire the DXGI interface for the Direct3D device. using (var dxgiDevice = d3dDevice.QueryInterface <SharpDX.DXGI.Device3>()) { // Wrap the native device using a WinRT interop object. IntPtr pUnknown; UInt32 hr = InteropStatics.CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice.NativePointer, out pUnknown); if (hr == 0) { d3dInteropDevice = (IDirect3DDevice)Marshal.GetObjectForIUnknown(pUnknown); Marshal.Release(pUnknown); } // Store a pointer to the DXGI adapter. // This is for the case of no preferred DXGI adapter, or fallback to WARP. dxgiAdapter = this.ToDispose(dxgiDevice.Adapter.QueryInterface <SharpDX.DXGI.Adapter3>()); } // Check for device support for the optional feature that allows setting the render target array index from the vertex shader stage. var options = d3dDevice.CheckD3D113Features3(); if (options.VPAndRTArrayIndexFromAnyShaderFeedingRasterizer) { d3dDeviceSupportsVprt = true; } }