internal static void OnDeviceReset() { if (m_buffer != StructuredBufferId.NULL) { MyHwBuffers.Destroy(m_buffer); m_buffer = StructuredBufferId.NULL; } Init(); }
internal void Release() { if (m_VB_positions != StructuredBufferId.NULL) { MyHwBuffers.Destroy(m_VB_positions); MyHwBuffers.Destroy(m_VB_rest); MyHwBuffers.Destroy(m_IB); } m_VB_positions = StructuredBufferId.NULL; m_VB_rest = StructuredBufferId.NULL; m_IB = StructuredBufferId.NULL; }
internal unsafe void MoveToGPU() { if(m_dirty) { Release(); fixed(void* ptr = m_vertexStream0) { m_VB_positions = MyHwBuffers.CreateStructuredBuffer(m_vertices, Stride0, false, new IntPtr(ptr), "MyMergeInstancing positions"); } fixed (void* ptr = m_vertexStream1) { m_VB_rest = MyHwBuffers.CreateStructuredBuffer(m_vertices, Stride1, false, new IntPtr(ptr), "MyMergeInstancing rest"); } fixed (void* ptr = m_indexStream) { m_IB = MyHwBuffers.CreateStructuredBuffer(m_indices, IndexStride, false, new IntPtr(ptr), "MyMergeInstancing"); } m_dirty = false; } }
internal unsafe static void Init() { m_buffer = MyHwBuffers.CreateStructuredBuffer(4096, sizeof(MyPerMaterialData), true, null, "MySceneMaterials"); }
internal static void ResizeAndUpdateStaticStructuredBuffer(ref StructuredBufferId id, int capacity, int stride, IntPtr data, string debugName, DeviceContext context = null) { if (id == StructuredBufferId.NULL) { id = CreateStructuredBuffer(capacity, stride, false, data, debugName); } else { Debug.Assert(stride == id.Stride); Debug.Assert(false == id.Dynamic); if (id.Capacity < capacity) { SBuffersData[id.Index].Buffer.Dispose(); SBuffers.Data[id.Index].Description.SizeInBytes = stride * capacity; InitStructuredBuffer(id, data); } else { if (context == null) context = MyRender11.DeviceContext; context.UpdateSubresource(new DataBox(data, stride * capacity, 0), id.Buffer); } } }
private static void InitDevice() { m_particleBuffer = new MyRWStructuredBuffer(MyGPUEmitters.MAX_PARTICLES, PARTICLE_STRIDE, MyRWStructuredBuffer.UavType.Default, true, "MyGPUParticleRenderer::particleBuffer"); m_deadListBuffer = new MyRWStructuredBuffer(MyGPUEmitters.MAX_PARTICLES, sizeof(uint), MyRWStructuredBuffer.UavType.Append, false, "MyGPUParticleRenderer::deadListBuffer"); m_skippedParticleCountBuffer = new MyRWStructuredBuffer(1, sizeof(uint), MyRWStructuredBuffer.UavType.Counter, true, "MyGPUParticleRenderer::skippedParticleCountBuffer"); // Create a staging buffer that is used to read GPU atomic counter into that can then be mapped for reading // back to the CPU for debugging purposes m_debugCounterBuffers[0] = new MyReadStructuredBuffer(1, sizeof(uint), "MyGPUParticleRenderer::debugCounterBuffers[0]"); m_debugCounterBuffers[1] = new MyReadStructuredBuffer(1, sizeof(uint), "MyGPUParticleRenderer::debugCounterBuffers[1]"); var description = new SharpDX.Direct3D11.BufferDescription(4 * sizeof(uint), SharpDX.Direct3D11.ResourceUsage.Default, SharpDX.Direct3D11.BindFlags.ConstantBuffer, SharpDX.Direct3D11.CpuAccessFlags.None, SharpDX.Direct3D11.ResourceOptionFlags.None, sizeof(uint)); m_activeListConstantBuffer = MyHwBuffers.CreateConstantsBuffer(description, "MyGPUParticleRenderer::activeListConstantBuffer"); m_emitterConstantBuffer = MyHwBuffers.CreateConstantsBuffer(EMITTERCONSTANTBUFFER_SIZE, "MyGPUParticleRenderer::emitterConstantBuffer"); m_emitterStructuredBuffer = MyHwBuffers.CreateStructuredBuffer(MyGPUEmitters.MAX_LIVE_EMITTERS, EMITTERDATA_SIZE, true, null, "MyGPUParticleRenderer::emitterStructuredBuffer"); m_aliveIndexBuffer = new MyRWStructuredBuffer(MyGPUEmitters.MAX_PARTICLES, sizeof(float), MyRWStructuredBuffer.UavType.Counter, true, "MyGPUParticleRenderer::aliveIndexBuffer"); m_indirectDrawArgsBuffer = new MyIndirectArgsBuffer(5, sizeof(uint), "MyGPUParticleRenderer::indirectDrawArgsBuffer"); unsafe { uint[] indices = new uint[MyGPUEmitters.MAX_PARTICLES * 6]; for (uint i = 0, index = 0, vertex = 0; i < MyGPUEmitters.MAX_PARTICLES; i++) { indices[index + 0] = vertex + 0; indices[index + 1] = vertex + 1; indices[index + 2] = vertex + 2; indices[index + 3] = vertex + 2; indices[index + 4] = vertex + 1; indices[index + 5] = vertex + 3; vertex += 4; index += 6; } fixed (uint* ptr = indices) { m_ib = MyHwBuffers.CreateIndexBuffer(MyGPUEmitters.MAX_PARTICLES * 6, SharpDX.DXGI.Format.R32_UInt, SharpDX.Direct3D11.BindFlags.IndexBuffer, SharpDX.Direct3D11.ResourceUsage.Immutable, new IntPtr(ptr), "MyGPUParticleRenderer::indexBuffer"); } } //MyRender11.BlendAlphaPremult }
internal static ShaderResourceView GetView(StructuredBufferId id) { return id != StructuredBufferId.NULL ? SBuffersData[id.Index].Srv : null; }
internal static void InitStructuredBuffer(StructuredBufferId id, IntPtr data) { SBuffersData[id.Index].Buffer = new Buffer(MyRender11.Device, data, SBuffers.Data[id.Index].Description); SBuffersData[id.Index].Srv = new ShaderResourceView(MyRender11.Device, SBuffersData[id.Index].Buffer); if (SBuffers.Data[id.Index].DebugName != null) { SBuffersData[id.Index].Buffer.DebugName = SBuffers.Data[id.Index].DebugName; SBuffersData[id.Index].Srv.DebugName = SBuffers.Data[id.Index].DebugName; } }
internal static Buffer GetBuffer(StructuredBufferId id) { return SBuffersData[id.Index].Buffer; }
internal static BufferDescription GetBufferDesc(StructuredBufferId id) { return SBuffers.Data[id.Index].Description; }
internal static void Destroy(StructuredBufferId id) { SbIndices.Remove(id); if (SBuffersData[id.Index].Buffer != null) { SBuffersData[id.Index].Buffer.Dispose(); SBuffersData[id.Index].Buffer = null; } if (SBuffersData[id.Index].Srv != null) { SBuffersData[id.Index].Srv.Dispose(); SBuffersData[id.Index].Srv = null; } SBuffers.Free(id.Index); }
internal static void Destroy(ref StructuredBufferId id) { if (id != StructuredBufferId.NULL) { Destroy(id); id = StructuredBufferId.NULL; } }
internal static StructuredBufferId CreateStructuredBuffer(BufferDescription description, IntPtr? data, string debugName) { var id = new StructuredBufferId { Index = SBuffers.Allocate() }; MyArrayHelpers.Reserve(ref SBuffersData, id.Index + 1); SBuffers.Data[id.Index] = new MyHwBufferDesc { Description = description, DebugName = debugName }; SBuffersData[id.Index] = new MyStructuredBufferData { }; SbIndices.Add(id); if (!data.HasValue) { InitStructuredBuffer(id); } else { InitStructuredBuffer(id, data.Value); } return id; }
internal unsafe void MoveToGPU() { var context = MyImmediateRC.RC; if (m_tableDirty) { var array = m_instancingTable.Data; fixed (void* ptr = array) { var intPtr = new IntPtr(ptr); MyHwBuffers.ResizeAndUpdateStaticStructuredBuffer(ref m_indirectionBuffer, array.Length, sizeof(MyInstancingTableEntry), intPtr, "MyMergeInstancing/Tables", context); m_srvs[0] = m_indirectionBuffer.Srv; } m_tableDirty = false; } if (m_instancesDataDirty) { var array = m_perInstance.Data; fixed (void* ptr = array) { var intPtr = new IntPtr(ptr); if (m_instanceBuffer != StructuredBufferId.NULL && m_instanceBuffer.Capacity < array.Length) { MyHwBuffers.Destroy(m_instanceBuffer); m_instanceBuffer = StructuredBufferId.NULL; m_srvs[1] = null; } if (m_instanceBuffer == StructuredBufferId.NULL) { m_instanceBuffer = MyHwBuffers.CreateStructuredBuffer(array.Length, sizeof(MyPerInstanceData), true, intPtr, "MyMergeInstancing instances"); m_srvs[1] = m_instanceBuffer.Srv; } else { var mapping = MyMapping.MapDiscard(MyImmediateRC.RC, m_instanceBuffer.Buffer); mapping.WriteAndPosition(array, 0, array.Length); mapping.Unmap(); } } m_instancesDataDirty = false; } }
internal void OnDeviceReset() { if (m_indirectionBuffer != StructuredBufferId.NULL) { MyHwBuffers.Destroy(m_indirectionBuffer); MyHwBuffers.Destroy(m_instanceBuffer); } m_indirectionBuffer = StructuredBufferId.NULL; m_instanceBuffer = StructuredBufferId.NULL; Array.Clear(m_srvs, 0, m_srvs.Length); m_tableDirty = true; m_instancesDataDirty = true; }