static D3D11UnorderedAccessView CreateBufferUAV(D3D11Device pDevice, D3D11Buffer pBuffer) { var descBuf = pBuffer.Description; D3D11UnorderedAccessViewDesc desc; if (descBuf.MiscOptions.HasFlag(D3D11ResourceMiscOptions.BufferAllowRawViews)) { // This is a Raw Buffer // Format must be DXGI_FORMAT_R32_TYPELESS, when creating Raw Unordered Access View desc = new D3D11UnorderedAccessViewDesc(pBuffer, DxgiFormat.R32Typeless, 0, descBuf.ByteWidth / 4, D3D11BufferUavOptions.Raw); } else if (descBuf.MiscOptions.HasFlag(D3D11ResourceMiscOptions.BufferStructured)) { // This is a Structured Buffer // Format must be must be DXGI_FORMAT_UNKNOWN, when creating a View of a Structured Buffer desc = new D3D11UnorderedAccessViewDesc(pBuffer, DxgiFormat.Unknown, 0, descBuf.ByteWidth / descBuf.StructureByteStride, D3D11BufferUavOptions.None); } else { throw new InvalidOperationException(); } return(pDevice.CreateUnorderedAccessView(pBuffer, desc)); }
static void CreateResources() { var d3dDevice = deviceResources.D3DDevice; // Create the Bitonic Sort Compute Shader g_pComputeShaderBitonic = d3dDevice.CreateComputeShader(File.ReadAllBytes("CSSortBitonic.cso"), null); // Create the Matrix Transpose Compute Shader g_pComputeShaderTranspose = d3dDevice.CreateComputeShader(File.ReadAllBytes("CSSortTranspose.cso"), null); // Create the Const Buffer var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer); g_pCB = d3dDevice.CreateBuffer(constantBufferDesc); // Create the Buffer of Elements // Create 2 buffers for switching between when performing the transpose var bufferDesc = new D3D11BufferDesc( NUM_ELEMENTS * sizeof(uint), D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource, D3D11Usage.Default, D3D11CpuAccessOptions.None, D3D11ResourceMiscOptions.BufferStructured, sizeof(uint)); g_pBuffer1 = d3dDevice.CreateBuffer(bufferDesc); g_pBuffer2 = d3dDevice.CreateBuffer(bufferDesc); // Create the Shader Resource View for the Buffers // This is used for reading the buffer during the transpose var srvbufferDesc = new D3D11ShaderResourceViewDesc(D3D11SrvDimension.Buffer, DxgiFormat.Unknown) { Buffer = new D3D11BufferSrv { ElementWidth = NUM_ELEMENTS } }; g_pBuffer1SRV = d3dDevice.CreateShaderResourceView(g_pBuffer1, srvbufferDesc); g_pBuffer2SRV = d3dDevice.CreateShaderResourceView(g_pBuffer2, srvbufferDesc); // Create the Unordered Access View for the Buffers // This is used for writing the buffer during the sort and transpose var uavbufferDesc = new D3D11UnorderedAccessViewDesc(D3D11UavDimension.Buffer, DxgiFormat.Unknown) { Buffer = new D3D11BufferUav { NumElements = NUM_ELEMENTS } }; g_pBuffer1UAV = d3dDevice.CreateUnorderedAccessView(g_pBuffer1, uavbufferDesc); g_pBuffer2UAV = d3dDevice.CreateUnorderedAccessView(g_pBuffer2, uavbufferDesc); // Create the Readback Buffer // This is used to read the results back to the CPU var readbackBufferDesc = new D3D11BufferDesc( NUM_ELEMENTS * sizeof(uint), D3D11BindOptions.None, D3D11Usage.Staging, D3D11CpuAccessOptions.Read, D3D11ResourceMiscOptions.None, sizeof(uint)); g_pReadBackBuffer = d3dDevice.CreateBuffer(readbackBufferDesc); }
private void CreateParticlePosVeloBuffers() { var d3dDevice = this.deviceResources.D3DDevice; // Initialize the data in the buffers Particle[] pData1 = new Particle[MaxParticles]; Random rand = this.Random ?? new Random(0); if (this.DiskGalaxyFormationType == 0) { // Disk Galaxy Formation float fCenterSpread = Spread * 0.50f; LoadParticles( rand, pData1, 0, new XMFloat3(fCenterSpread, 0, 0), new XMFloat4(0, 0, -20, 1 / 10000.0f / 10000.0f), Spread, pData1.Length / 2); LoadParticles( rand, pData1, pData1.Length / 2, new XMFloat3(-fCenterSpread, 0, 0), new XMFloat4(0, 0, 20, 1 / 10000.0f / 10000.0f), Spread, pData1.Length - pData1.Length / 2); } else { // Disk Galaxy Formation with impacting third cluster LoadParticles( rand, pData1, 0, new XMFloat3(Spread, 0, 0), new XMFloat4(0, 0, -8, 1 / 10000.0f / 10000.0f), Spread, pData1.Length / 3); LoadParticles( rand, pData1, pData1.Length / 3, new XMFloat3(-Spread, 0, 0), new XMFloat4(0, 0, 8, 1 / 10000.0f / 10000.0f), Spread, pData1.Length / 3); LoadParticles( rand, pData1, 2 * pData1.Length / 3, new XMFloat3(0, 0, Spread * 15.0f), new XMFloat4(0, 0, -60, 1 / 10000.0f / 10000.0f), Spread, pData1.Length - 2 * pData1.Length / 3); } D3D11BufferDesc desc = D3D11BufferDesc.From(pData1, D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource); desc.MiscOptions = D3D11ResourceMiscOptions.BufferStructured; desc.StructureByteStride = Particle.Size; this.g_pParticlePosVelo0 = d3dDevice.CreateBuffer(desc, pData1, 0, 0); this.g_pParticlePosVelo1 = d3dDevice.CreateBuffer(desc, pData1, 0, 0); D3D11ShaderResourceViewDesc DescRV = new D3D11ShaderResourceViewDesc( this.g_pParticlePosVelo0, DxgiFormat.Unknown, 0, desc.ByteWidth / desc.StructureByteStride); this.g_pParticlePosVeloRV0 = d3dDevice.CreateShaderResourceView(this.g_pParticlePosVelo0, DescRV); this.g_pParticlePosVeloRV1 = d3dDevice.CreateShaderResourceView(this.g_pParticlePosVelo1, DescRV); D3D11UnorderedAccessViewDesc DescUAV = new D3D11UnorderedAccessViewDesc( this.g_pParticlePosVelo0, DxgiFormat.Unknown, 0, desc.ByteWidth / desc.StructureByteStride); this.g_pParticlePosVeloUAV0 = d3dDevice.CreateUnorderedAccessView(this.g_pParticlePosVelo0, DescUAV); this.g_pParticlePosVeloUAV1 = d3dDevice.CreateUnorderedAccessView(this.g_pParticlePosVelo1, DescUAV); }