/// <summary> /// returns a uav views with the specified offset /// </summary> UnorderedAccessView GetView(int offset) { Debug.Assert(offset < ElementCount); if (views == null) { views = new Dictionary <int, UnorderedAccessView>(); } if (views.TryGetValue(offset, out var hashedView)) { return(hashedView); } var newView = new UnorderedAccessView(Device.Get().Handle, Handle, new UnorderedAccessViewDescription { Dimension = UnorderedAccessViewDimension.Buffer, Format = Format.Unknown, Buffer = new UnorderedAccessViewDescription.BufferResource { FirstElement = offset, ElementCount = ElementCount - offset, Flags = UnorderedAccessViewBufferFlags.None } }); views[offset] = newView; return(newView); }
public void BindAsRenderTarget(DeviceContext context, bool depth = false, bool color = true) { RenderTargetView[] renderTargetViews = new RenderTargetView[m_NumRTs]; UnorderedAccessView[] unorderedAccessViews = new UnorderedAccessView[m_UAVsNum]; for (int i = 0; i < m_NumRTs; ++i) { renderTargetViews[i] = m_RenderTargets[i].m_RenderTargetView; } for (int i = 0; i < m_UAVsNum; ++i) { unorderedAccessViews[i] = m_UAVs[i].m_UnorderedAccessView; } if (m_UAVsNum > 0) { context.OutputMerger.SetTargets(depth ? m_DepthStencil.m_DepthStencilView : null, color ? m_NumRTs : 0, color ? unorderedAccessViews : null, color ? renderTargetViews : null); } else { context.OutputMerger.SetTargets(depth ? m_DepthStencil.m_DepthStencilView : null, renderTargetViews); } context.Rasterizer.SetViewports(m_Viewport); context.OutputMerger.DepthStencilState = depth ? m_DepthStencilState : m_DepthStencilStateNoDepth; }
public void SetParameterResource(string p, UnorderedAccessView shaderResView) { if (HasParameter(p)) { effect.Parameters[p].SetResource(shaderResView); } }
private void CopyR32_UInt_UAVToExistingSRV(UnorderedAccessView uav, ShaderResourceView srv) { var device = this.DeviceManager.Direct3DDevice; int width, height; using (var t = uav.ResourceAs <Texture2D>()) { width = t.Description.Width; height = t.Description.Height; if (t.Description.Format != Format.R32_UInt) { throw new ArgumentException("The provided UAV does not use the format R32_Uint", "uav"); } using (var t2 = srv.ResourceAs <Texture2D>()) { if (t2.Description.Format != Format.R8G8B8A8_Typeless) { throw new ArgumentException("Currently only supporting R8G8B8A8_Typeless SRVs", "srv"); } this.DeviceManager.Direct3DDevice.ImmediateContext.CopyResource(t, t2); } } }
public void Render(DX11RenderContext context) { Device device = context.Device; DeviceContext ctx = context.CurrentDeviceContext; //Just in case if (!this.updateddevices.Contains(context)) { this.Update(context); } if (!this.FInLayer.PluginIO.IsConnected) { return; } if (this.rendereddevices.Contains(context)) { return; } if (this.FInEnabled[0]) { if (this.BeginQuery != null) { this.BeginQuery(context); } context.CurrentDeviceContext.OutputMerger.SetTargets(new RenderTargetView[0]); settings.ViewportIndex = 0; settings.ViewportCount = 1; settings.BackBuffer = FOutBuffers[0][context]; settings.CustomSemantics = rsemantics; for (int i = 0; i < FSemantic.SliceCount; i++) { settings.RenderWidth = sizes[i]; settings.RenderHeight = sizes[i]; settings.RenderDepth = sizes[i]; if (FInResetCounter[i]) { settings.ResetCounter = true; int[] resetval = { FInResetCounterValue[i] }; var uavarray = new UnorderedAccessView[1] { FOutBuffers[i][context].UAV }; context.CurrentDeviceContext.ComputeShader.SetUnorderedAccessViews(uavarray, 0, 1, resetval); } else { settings.ResetCounter = false; } } FInLayer[0][context].Render(context, settings); EndQuery?.Invoke(context); } }
public ShaderResourceView GenerateNoiseTexture(Buffer constantBuffer, DirectComputeConstantBuffer container) { Texture3D noiseTexture = new Texture3D(graphicsDevice, new Texture3DDescription() { BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R32_Float, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Default, Width = container.Width, Height = container.Height, Depth = container.Depth }); UnorderedAccessView noiseTextureUAV = new UnorderedAccessView(graphicsDevice, noiseTexture); DataBox data = graphicsDevice.ImmediateContext.MapSubresource(constantBuffer, MapMode.WriteDiscard, MapFlags.None); data.Data.Write <DirectComputeConstantBuffer>(container); graphicsDevice.ImmediateContext.UnmapSubresource(constantBuffer, 0); graphicsDevice.ImmediateContext.ComputeShader.Set(computeFillNoiseTexture); graphicsDevice.ImmediateContext.ComputeShader.SetConstantBuffer(constantBuffer, 0); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(noiseTextureUAV, 0); Vector3 gridDim = new Vector3((float)Math.Ceiling(container.Width / 8.0f), (float)Math.Ceiling(container.Height / 8.0f), (float)Math.Ceiling(container.Depth / 8.0f)); graphicsDevice.ImmediateContext.Dispatch((int)gridDim.X, (int)gridDim.Y, (int)gridDim.Z); noiseTextureUAV.Dispose(); return(new ShaderResourceView(graphicsDevice, noiseTexture)); }
void CreateViews(string name, BindFlags flags, SlimDX.Direct3D11.Resource resource) { if (flags.HasFlag(BindFlags.RenderTarget)) { var view = new RenderTargetView(m_D3dDevice, resource); m_RTVList.Add(name, view); } if (flags.HasFlag(BindFlags.DepthStencil)) { var view = new DepthStencilView(m_D3dDevice, resource); m_DSVList.Add(name, view); } if (flags.HasFlag(BindFlags.ShaderResource)) { var view = new ShaderResourceView(m_D3dDevice, resource); m_SRVList.Add(name, view); } if (flags.HasFlag(BindFlags.UnorderedAccess)) { var view = new UnorderedAccessView(m_D3dDevice, resource); m_UAVList.Add(name, view); } }
public ShaderResourceView GenerateNoiseTexture(Buffer constantBuffer, DirectComputeConstantBuffer container) { Texture3D noiseTexture = new Texture3D(graphicsDevice, new Texture3DDescription() { BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R32_Float, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Default, Width = container.Width, Height = container.Height, Depth = container.Depth }); UnorderedAccessView noiseTextureUAV = new UnorderedAccessView(graphicsDevice, noiseTexture); DataBox data = graphicsDevice.ImmediateContext.MapSubresource(constantBuffer, MapMode.WriteDiscard, MapFlags.None); data.Data.Write<DirectComputeConstantBuffer>(container); graphicsDevice.ImmediateContext.UnmapSubresource(constantBuffer, 0); graphicsDevice.ImmediateContext.ComputeShader.Set(computeFillNoiseTexture); graphicsDevice.ImmediateContext.ComputeShader.SetConstantBuffer(constantBuffer, 0); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(noiseTextureUAV, 0); Vector3 gridDim = new Vector3((float)Math.Ceiling(container.Width / 8.0f), (float)Math.Ceiling(container.Height / 8.0f), (float)Math.Ceiling(container.Depth / 8.0f)); graphicsDevice.ImmediateContext.Dispatch((int)gridDim.X, (int)gridDim.Y, (int)gridDim.Z); noiseTextureUAV.Dispose(); return new ShaderResourceView(graphicsDevice, noiseTexture); }
/// <summary> /// Initializes a new instance of the <see cref="UAVBufferViewProxy"/> class. /// </summary> /// <param name="device">The device.</param> /// <param name="bufferDesc">The buffer desc.</param> /// <param name="uavDesc">The uav desc.</param> /// <param name="srvDesc">The SRV desc.</param> public UAVBufferViewProxy(Device device, ref BufferDescription bufferDesc, ref UnorderedAccessViewDescription uavDesc, ref ShaderResourceViewDescription srvDesc) { buffer = new SDX11.Buffer(device, bufferDesc); srv = new ShaderResourceViewProxy(device, buffer); srv.CreateTextureView(); uav = new UnorderedAccessView(device, buffer, uavDesc); }
/// <summary> /// Generic method to add BufferData to the buffer /// </summary> /// <param name="shaderBufferIndex"></param> /// <param name="bufferData"></param> private void SetBufferData(int shaderBufferIndex, BufferData bufferData) { if (buffers.ContainsKey(shaderBufferIndex)) { Debug.WriteLine("There is already a buffer with index " + shaderBufferIndex); return; } if (bufferData.GetInputDataType() == typeof(Texture2D)) { UnorderedAccessView accessView = CreateUnorderedTextureAccessView((Texture2D)bufferData.GetInputData(), out var computeResource, out var stagingResource); bufferData.SetComputeResource(computeResource); bufferData.SetStagingResource(stagingResource); bufferData.SetUnorderedAccessView(accessView); } else if (bufferData.GetInputDataType() == typeof(ComputeBuffer)) { UnorderedAccessView accessView = CreateUnorderedDataAccessView((ComputeBuffer)bufferData.GetInputData(), out var computeResource, out var stagingResource); bufferData.SetComputeResource(computeResource); bufferData.SetStagingResource(stagingResource); bufferData.SetUnorderedAccessView(accessView); } SendUnorderedAccessView(bufferData); buffers.Add(shaderBufferIndex, bufferData); }
protected override void AfterBufferInit() { if (UavType == MyUavType.Default) { m_uav = new UnorderedAccessView(MyRender11.Device, Resource); } else { var description = new UnorderedAccessViewDescription() { Buffer = new UnorderedAccessViewDescription.BufferResource() { ElementCount = ElementCount, FirstElement = 0, Flags = UavType == MyUavType.Append ? UnorderedAccessViewBufferFlags.Append : UnorderedAccessViewBufferFlags.Counter }, Format = SharpDX.DXGI.Format.Unknown, Dimension = UnorderedAccessViewDimension.Buffer }; m_uav = new UnorderedAccessView(MyRender11.Device, Resource, description); } m_uav.DebugName = Name + "_Uav"; }
public void RunCS(string name, ShaderResourceView source, UnorderedAccessView destination) { int width, height; CheckSRVWidthHeight(source, out width, out height); var context = this.DeviceManager.Direct3DContext; // Update the constant buffer context.UpdateSubresource(ref Constants, perComputeBuffer); //context.ComputeShader.SetSampler(0, linearSampler); context.ComputeShader.SetShaderResource(0, source); context.ComputeShader.SetUnorderedAccessView(0, destination); context.ComputeShader.SetConstantBuffer(0, perComputeBuffer); // Compile the shader if it isn't already if (!computeShaders.ContainsKey(name)) { CompileComputeShader(name); } // Set the shader to run context.ComputeShader.Set(computeShaders[name]); clock.Restart(); context.Dispatch((int)Math.Ceiling(width / (double)ThreadsX), (int)Math.Ceiling(height / (double)ThreadsY), 1); LastDispatchTicks = clock.ElapsedTicks; // Clear the shader and resources context.ComputeShader.SetSampler(0, null); context.ComputeShader.SetShaderResources(0, null, null, null); context.ComputeShader.SetUnorderedAccessViews(0, null, null, null); context.ComputeShader.Set(null); }
internal void BindUAV(int slot, params MyBindableResource[] UAVs) { var buffer = new UnorderedAccessView[UAVs.Length]; for (int i = 0; i < UAVs.Length; i++) { if (UAVs[i] != null) { var ua = UAVs[i] as IUnorderedAccessBindable; Debug.Assert(ua != null); UnbindSRVRead(UAVs[i].GetID()); //UnbindDSVReadOnly(UAVs[i].ResId); necessary? int?currentlyBound = null; foreach (var kv in State.m_bindings) { if (kv.Value.UavSlot == slot + i) { currentlyBound = kv.Key; break; } } if (currentlyBound.HasValue) { State.m_bindings.Remove(currentlyBound.Value); } State.m_bindings[UAVs[i].GetID()] = new MyBinding(MyWriteBindingEnum.UAV, slot + i); buffer[i] = ua.UAV; } } Context.ComputeShader.SetUnorderedAccessViews(slot, buffer); }
public override void Dispose() { PerObjConstantBuffer?.Dispose(); PerObjConstantBuffer = null; PerFrameConstantBuffer?.Dispose(); PerFrameConstantBuffer = null; LightIndexBuffer?.Dispose(); LightIndexBuffer = null; LightCenterAndRadiusBuffer?.Dispose(); LightCenterAndRadiusBuffer = null; LightColorBuffer?.Dispose(); LightColorBuffer = null; LightParamsBuffer?.Dispose(); LightParamsBuffer = null; DirLightBuffer?.Dispose(); DirLightBuffer = null; LightCenterAndRadiusSRV?.Dispose(); LightCenterAndRadiusSRV = null; LightColorSRV?.Dispose(); LightColorSRV = null; LightParamsSRV?.Dispose(); LightParamsSRV = null; LightIndexSRV?.Dispose(); LightIndexSRV = null; LightIndexURV?.Dispose(); LightIndexURV = null; }
internal MyIndirectArgsBuffer(int elements, int stride, string debugName) { m_resolution = new Vector2I(elements, 1); var bufferDesc = new BufferDescription(elements * stride, ResourceUsage.Default, BindFlags.UnorderedAccess, CpuAccessFlags.None, ResourceOptionFlags.DrawIndirectArguments, stride); m_resource = new SharpDX.Direct3D11.Buffer(MyRender11.Device, bufferDesc); m_resource.DebugName = debugName; var description = new UnorderedAccessViewDescription() { Buffer = new UnorderedAccessViewDescription.BufferResource() { ElementCount = elements, FirstElement = 0, Flags = 0 }, Format = Format.R32_UInt, Dimension = UnorderedAccessViewDimension.Buffer }; m_UAV = new UnorderedAccessView(MyRender11.Device, m_resource, description); m_UAV.DebugName = debugName + "UAV"; }
internal MyUnorderedAccessTexture(int width, int height, Format format, int samplesCount = 1, int samplesQuality = 0, string debugName = null) { m_resolution = new Vector2I(width, height); Texture2DDescription desc = new Texture2DDescription(); desc.Width = width; desc.Height = height; desc.Format = format; desc.ArraySize = 1; desc.MipLevels = 1; desc.BindFlags = BindFlags.UnorderedAccess | BindFlags.ShaderResource | BindFlags.RenderTarget; desc.Usage = ResourceUsage.Default; desc.CpuAccessFlags = 0; desc.SampleDescription.Count = samplesCount; desc.SampleDescription.Quality = samplesQuality; desc.OptionFlags = 0; m_resource = new Texture2D(MyRender11.Device, desc); m_UAV = new UnorderedAccessView(MyRender11.Device, m_resource); m_SRV = new ShaderResourceView(MyRender11.Device, m_resource); m_RTV = new RenderTargetView(MyRender11.Device, m_resource); if (debugName != null) { m_resource.DebugName = debugName; m_UAV.DebugName = debugName + "UAV"; m_SRV.DebugName = debugName + "SRV"; m_RTV.DebugName = debugName + "RTV"; } }
private void SetupRasterTable(Device device, ShaderCache shaderCache) { DeviceContext context = device.ImmediateContext; BufferDescription desc = new BufferDescription { SizeInBytes = RasterTableElementCount * RasterSizeInBytes, Usage = ResourceUsage.Default, BindFlags = BindFlags.UnorderedAccess | BindFlags.ShaderResource, OptionFlags = ResourceOptionFlags.BufferStructured, StructureByteStride = RasterSizeInBytes }; var buildTableShader = shaderCache.GetComputeShader <GpuOcclusionCalculator>("occlusion/BuildHemisphericalRasterizingTable"); using (Buffer buffer = new Buffer(device, desc)) using (UnorderedAccessView view = new UnorderedAccessView(device, buffer)) { context.ComputeShader.Set(buildTableShader); context.ComputeShader.SetUnorderedAccessView(0, view); context.ComputeShader.SetConstantBuffer(0, hemispherePointsAndWeightsConstantBuffer); context.Dispatch(RasterTableDim / BuildTableShaderNumThreads, RasterTableDim, RasterTableFaceCount); context.ClearState(); rasterCacheView = new ShaderResourceView(device, buffer); } }
/// <summary> /// Constructs a new texture 2D with interop capabilities. /// </summary> /// <param name="accelerator">The target accelerator.</param> /// <param name="d3dDevice">The target DX device.</param> /// <param name="texture">The width.</param> /// <param name="bufferFlags">The used buffer flags.</param> /// <param name="viewFlags">The used view flags.</param> protected DirectXTexture2D( Accelerator accelerator, Device d3dDevice, Texture2D texture, DirectXBufferFlags bufferFlags, DirectXViewFlags viewFlags) : base(accelerator, d3dDevice, GetNumElements(texture), bufferFlags, viewFlags) { var desc = texture.Description; Width = desc.Width; Height = desc.Height; Format = desc.Format; this.texture = texture; if ((desc.BindFlags & BindFlags.RenderTarget) != 0) { targetView = new RenderTargetView(d3dDevice, texture); } if ((desc.BindFlags & BindFlags.ShaderResource) != 0) { ResourceView = new ShaderResourceView(d3dDevice, texture); } if ((desc.BindFlags & BindFlags.UnorderedAccess) != 0) { UnorderedAccessView = new UnorderedAccessView(d3dDevice, texture); } }
private void BindUnorderedAccessView(Texture target, UnorderedAccessView uav, int slot, ShaderStages stages, uint resourceSet) { Debug.Assert(stages == ShaderStages.Compute || ((stages & ShaderStages.Compute) == 0)); if (target != null && uav != null) { if (!_boundUAVs.TryGetValue(target, out List <BoundTextureInfo> list)) { list = GetNewOrCachedBoundTextureInfoList(); _boundUAVs.Add(target, list); } list.Add(new BoundTextureInfo { Slot = slot, Stages = stages, ResourceSet = resourceSet }); } int baseSlot = 0; if (stages != ShaderStages.Compute && _framebuffer != null) { baseSlot = _framebuffer.ColorTargets.Count; } if (stages == ShaderStages.Compute) { _context.ComputeShader.SetUnorderedAccessView(baseSlot + slot, uav); } else { _context.OutputMerger.SetUnorderedAccessView(baseSlot + slot, uav); } }
public Texture3DAndViews(Device device, SlimDX.DXGI.Format format, ItemCount <Pixel> width, ItemCount <Pixel> height, ItemCount <Pixel> depth, DataBox[] data = null) { Width = width; Height = height; Depth = depth; var texDesc = new Texture3DDescription() { BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, Depth = depth.Count, Format = format, Height = height.Count, Width = width.Count, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Default }; if (data != null) { _data = new Texture3D(device, texDesc, data); } else { _data = new Texture3D(device, texDesc); } SRV = new ShaderResourceView(device, _data); UAV = new UnorderedAccessView(device, _data); }
public void Render(DX11RenderContext context, DX11RenderSettings settings) { if (this.FLayerIn.IsConnected) { for (int i = 0; i < FSemantic.SliceCount; i++) { RWStructuredBufferRenderSemantic ccrs = null; foreach (var rsem in settings.CustomSemantics) { if (rsem.Semantic == FSemantic[i]) { if (rsem is RWStructuredBufferRenderSemantic) { ccrs = rsem as RWStructuredBufferRenderSemantic; } } } if (FInReset[i]) { if (ccrs != null) { int[] resetval = { FInResetCounterValue[i] }; var uavarray = new UnorderedAccessView[1] { ccrs.Data.UAV }; context.CurrentDeviceContext.ComputeShader.SetUnorderedAccessViews(uavarray, 0, 1, resetval); } } } this.FLayerIn[0][context].Render(context, settings); } }
protected override void InitializeViews() { // Creates the shader resource view if ((Description.BindFlags & BindFlags.ShaderResource) != 0) { shaderResourceViews = new Dictionary <TextureViewKey, TextureView>(); // Pre initialize by default the view on the first array/mipmap var viewFormat = Format; if (!FormatHelper.IsTypeless(viewFormat)) { // Only valid for non-typeless viewformat defaultShaderResourceView = GetShaderResourceView(viewFormat, ViewType.Full, 0, 0); } } // Creates the unordered access view if ((Description.BindFlags & BindFlags.UnorderedAccess) != 0) { // Initialize the unordered access views unorderedAccessViews = new UnorderedAccessView[GetViewCount()]; // Pre initialize by default the view on the first array/mipmap GetUnorderedAccessView(0, 0); } }
public SemiGridSPHSimulation(Device device, UnorderedAccessView outputBuffer) { MarkupList = new List <ShaderFileEditor.MarkupTag>() { new ShaderFileEditor.MarkupTag("GridReadSlot", _readBufferSlot), new ShaderFileEditor.MarkupTag("GridWriteSlot", _writeBufferSlot), new ShaderFileEditor.MarkupTag("Resolution", _resolution) }; var generatedFilename = GenerateTempFile( "GridFluid/GridFluid.hlsl", MarkupList.Concat( OutputShader.MarkupList)); /*_outputShader = * new OutputShader( * generatedFilename, * device, * _resolution, * outputBuffer, * _massVelBuffers, * _readBufferSlot, * _writeBufferSlot, * _inkBuffers, * _inkReadBufferSlot);*/ }
private void GenerateCS(string name, UnorderedAccessView append) { var context = this.DeviceManager.Direct3DContext; // Compile the shader if it isn't already if (!computeShaders.ContainsKey(name)) { int oldThreadsX = ThreadsX; int oldThreadsY = ThreadsY; ThreadsX = GeneratorThreadsX; ThreadsY = 1; CompileComputeShader(name); ThreadsX = oldThreadsX; ThreadsY = oldThreadsY; } // Set the shader to run context.ComputeShader.Set(computeShaders[name]); clock.Restart(); // Dispatch the compute shader thread groups context.Dispatch((int)Math.Ceiling(ParticlesPerBatch / 16.0), 1, 1); LastDispatchTicks = clock.ElapsedTicks; DebugCount("Gen-append", context, append); }
/// <summary> /// バインド /// </summary> public void Bind() { var context = GraphicsCore.D3D11ImmediateContext; context.ComputeShader.Set(shader_); context.ComputeShader.SetConstantBuffer(constantBuffer_, 0); if (resources_ != null) { ShaderResourceView[] srvs = new ShaderResourceView[resources_.Length]; for (int i = 0; i < resources_.Length; i++) { srvs[i] = resources_[i].ShaderResourceView; } context.ComputeShader.SetShaderResources(srvs, 0, srvs.Length); } if (uavs_ != null) { UnorderedAccessView[] uavs = new UnorderedAccessView[uavs_.Length]; for (int i = 0; i < uavs_.Length; i++) { uavs[i] = uavs_[i].UnorderedAccessView; } context.ComputeShader.SetUnorderedAccessViews(uavs, 0, uavs.Length); } }
/// <summary> /// Creates texture /// </summary> /// <param name="device"></param> public VolumeRWTexture(GraphicsDevice device, int width, int height, int depth, ColorFormat format, bool mips) : base(device) { this.Width = width; this.Height = height; this.Depth = depth; this.format = format; this.mipCount = mips ? ShaderResource.CalculateMipLevels(Width, Height, Depth) : 1; var texDesc = new Texture3DDescription(); texDesc.BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess; texDesc.CpuAccessFlags = CpuAccessFlags.None; texDesc.Format = Converter.Convert(format); texDesc.Height = Height; texDesc.MipLevels = mipCount; texDesc.OptionFlags = ResourceOptionFlags.None; texDesc.Usage = ResourceUsage.Default; texDesc.Width = Width; texDesc.Depth = Depth; var uavDesc = new UnorderedAccessViewDescription(); uavDesc.Format = Converter.Convert(format); uavDesc.Dimension = UnorderedAccessViewDimension.Texture3D; uavDesc.Texture3D.FirstWSlice = 0; uavDesc.Texture3D.MipSlice = 0; uavDesc.Texture3D.WSize = depth; tex3D = new D3D.Texture3D(device.Device, texDesc); SRV = new D3D.ShaderResourceView(device.Device, tex3D); uav = new UnorderedAccessView(device.Device, tex3D, uavDesc); }
/// <summary> /// Gets the unordered access view array. /// </summary> /// <param name="offset">The offset.</param> /// <param name="count">The count.</param> /// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns> /// <unmanaged>HRESULT ID3DX11EffectUnorderedAccessViewVariable::GetUnorderedAccessViewArray([Out, Buffer] ID3D11UnorderedAccessView** ppResources,[In] unsigned int Offset,[In] unsigned int Count)</unmanaged> public UnorderedAccessView[] GetUnorderedAccessViewArray(int offset, int count) { var temp = new UnorderedAccessView[count]; this.GetUnorderedAccessViewArray(temp, offset, count); return(temp); }
// Functions public GPUBufferObject() { m_Size = 1; m_BufferObject = null; m_ShaderResourceView = null; m_UnorderedAccessView = null; }
/// <summary> /// Function to perform initialization of the shader view resource. /// </summary> protected override void OnInitialize() { var bufferType = UnorderedAccessViewBufferFlags.None; Gorgon.Log.Print("Creating structured buffer unordered access view for {0}.", LoggingLevel.Verbose, Resource.Name); switch (ViewType) { case UnorderedAccessViewType.AppendConsume: bufferType = UnorderedAccessViewBufferFlags.Append; break; case UnorderedAccessViewType.Counter: bufferType = UnorderedAccessViewBufferFlags.Counter; break; } var desc = new UnorderedAccessViewDescription { Dimension = UnorderedAccessViewDimension.Buffer, Buffer = { FirstElement = ElementStart, ElementCount = ElementCount, Flags = bufferType }, Format = (Format)Format }; D3DView = new UnorderedAccessView(Resource.Graphics.D3DDevice, Resource.D3DResource, desc) { DebugName = "Gorgon Unordered Access View for " + Resource.Name }; }
private void InitLightsBuffers() { BufferDescription LightBufferDesc = new BufferDescription() { Usage = ResourceUsage.Default, SizeInBytes = sizeof(float) * 4 * MAX_NUM_LIGHTS, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.Write, }; LightCenterAndRadiusBuffer = Buffer.Create(GetDevice, NonDirLightCenterAndRadiusArray, LightBufferDesc); LightColorBuffer = Buffer.Create(GetDevice, NonDirLightColorArray, LightBufferDesc); LightParamsBuffer = Buffer.Create(GetDevice, NonDirLightParamsArray, LightBufferDesc); m_DirLightsConstBuffer = new CommonStructs.ConstBufferDirLightStruct[] { new CommonStructs.ConstBufferDirLightStruct(), new CommonStructs.ConstBufferDirLightStruct(), new CommonStructs.ConstBufferDirLightStruct() }; DirLightBuffer = new Buffer( GetDevice, Utilities.SizeOf <CommonStructs.ConstBufferDirLightStruct>() * m_DirLightsConstBuffer.Length, ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0 ); int NumTiles = GetNumTilesX() * GetNumTilesY(); int MaxNumLightsPerTile = GetMaxNumLightsPerTile(); LightIndexBuffer = new Buffer(GetDevice, new BufferDescription() { Usage = ResourceUsage.Default, SizeInBytes = 4 * MaxNumLightsPerTile * NumTiles, BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess, }); LightIndexSRV = new ShaderResourceView(GetDevice, LightIndexBuffer, new ShaderResourceViewDescription() { Format = Format.R32_UInt, Dimension = ShaderResourceViewDimension.Buffer, Buffer = new ShaderResourceViewDescription.BufferResource() { ElementOffset = 0, ElementWidth = MaxNumLightsPerTile * NumTiles, }, }); LightIndexURV = new UnorderedAccessView(GetDevice, LightIndexBuffer, new UnorderedAccessViewDescription() { Format = Format.R32_UInt, Dimension = UnorderedAccessViewDimension.Buffer, Buffer = new UnorderedAccessViewDescription.BufferResource() { FirstElement = 0, ElementCount = MaxNumLightsPerTile * NumTiles, }, }); }
/// <summary cref="DisposeBase.Dispose(bool)"/> protected override void Dispose(bool disposing) { ResourceView?.Dispose(); ResourceView = null; UnorderedAccessView?.Dispose(); UnorderedAccessView = null; }
// Functions public GPUBufferObject() { m_Size = -1; m_BufferObject = null; m_ShaderResourceView = null; m_UnorderedAccessView = null; m_StagingBufferObject = null; m_StagingCountBufferObject = null; }
public DX11RenderTexture3D(DX11RenderContext context, Texture3D tex, ShaderResourceView srv, UnorderedAccessView uav) : base(context) { this.Resource = tex; this.SRV = srv; this.UAV = uav; }
// Functions public TextureObject() { m_Width = m_Height = 1; m_Mips = 1; m_Depth = 1; m_ArraySize = 1; m_IsCube = false; m_TexFormat = Format.Unknown; m_TextureObject2D = null; m_TextureObject3D = null; m_RenderTargetView = null; m_ShaderResourceView = null; m_DepthStencilView = null; m_UnorderedAccessView = null; m_ArrayRenderTargetViews = null; m_ArrayShaderResourceViews = null; m_ArrayDepthStencilViews = null; m_ArrayUnorderedAccessViews = null; }
public void SetByName(string name, UnorderedAccessView value) { this.effect.GetVariableByName(name).AsUnorderedAccessView().SetView(value); }
public CounterResetUAV(UnorderedAccessView uav, int counter) { this.UAV = uav; this.Counter = counter; }
private void Generate(ComputeShader computePositionWeight, int width, int height, int depth) { int count = width * height * depth; int widthD = width - 1; int heightD = height - 1; int depthD = depth - 1; int countD = widthD * heightD * depthD; int nearestW = NearestPowerOfTwo(widthD); int nearestH = NearestPowerOfTwo(heightD); int nearestD = NearestPowerOfTwo(depthD); int nearestCount = nearestW * nearestH * nearestD; Vector3 gridDim = new Vector3((float)Math.Ceiling(width / 8.0f), (float)Math.Ceiling(height / 8.0f), (float)Math.Ceiling(depth / 8.0f)); Vector3 gridDimD = new Vector3((float)Math.Ceiling(widthD / 8.0f), (float)Math.Ceiling(heightD / 8.0f), (float)Math.Ceiling(depthD / 8.0f)); constantBufferContainer = new DirectComputeConstantBuffer() { Width = 16, Height = 16, Depth = 16, Seed = (int)DateTime.Now.Ticks }; DirectComputeNoiseCube noiseCube = new DirectComputeNoiseCube(graphicsDevice); ShaderResourceView noiseSRV = noiseCube.GenerateNoiseTexture(constantBuffer, constantBufferContainer); Buffer voxelsBuffer = new Buffer(graphicsDevice, new BufferDescription() { BindFlags = BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.StructuredBuffer, Usage = ResourceUsage.Default, SizeInBytes = Marshal.SizeOf(typeof(Voxel)) * count, StructureByteStride = Marshal.SizeOf(typeof(Voxel)) }); UnorderedAccessView voxelsUAV = new UnorderedAccessView(graphicsDevice, voxelsBuffer); constantBufferContainer = new DirectComputeConstantBuffer() { Width = width, Height = height, Depth = depth, AmbientRayWidth = container.Settings.AmbientRayWidth, AmbientSamplesCount = container.Settings.AmbientSamplesCount, NearestWidth = nearestW, NearestHeight = nearestH, NearestDepth = nearestD }; DataBox data = graphicsDevice.ImmediateContext.MapSubresource(constantBuffer, MapMode.WriteDiscard, MapFlags.None); data.Data.Write<DirectComputeConstantBuffer>(constantBufferContainer); graphicsDevice.ImmediateContext.UnmapSubresource(constantBuffer, 0); graphicsDevice.ImmediateContext.ComputeShader.Set(computePositionWeight); graphicsDevice.ImmediateContext.ComputeShader.SetConstantBuffer(constantBuffer, 0); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(voxelsUAV, 0); graphicsDevice.ImmediateContext.ComputeShader.SetShaderResource(noiseSRV, 0); graphicsDevice.ImmediateContext.Dispatch((int)gridDim.X, (int)gridDim.Y, (int)gridDim.Z); graphicsDevice.ImmediateContext.ComputeShader.Set(computeNormalAmbient); graphicsDevice.ImmediateContext.Dispatch((int)gridDim.X, (int)gridDim.Y, (int)gridDim.Z); Buffer offsetsBuffer = new Buffer(graphicsDevice, new BufferDescription() { BindFlags = BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.StructuredBuffer, Usage = ResourceUsage.Default, SizeInBytes = Marshal.SizeOf(typeof(int)) * countD, StructureByteStride = Marshal.SizeOf(typeof(int)) }); UnorderedAccessView offsetsUAV = new UnorderedAccessView(graphicsDevice, offsetsBuffer); Buffer trisCountBuffer = new Buffer(graphicsDevice, new BufferDescription() { BindFlags = BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.StructuredBuffer, Usage = ResourceUsage.Default, SizeInBytes = Marshal.SizeOf(typeof(int)) * nearestCount, StructureByteStride = Marshal.SizeOf(typeof(int)) }); UnorderedAccessView trisCountUAV = new UnorderedAccessView(graphicsDevice, trisCountBuffer); graphicsDevice.ImmediateContext.ClearUnorderedAccessView(trisCountUAV, new int[] { 0, 0, 0, 0 }); graphicsDevice.ImmediateContext.ComputeShader.Set(computeMarchingCubesCases); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(offsetsUAV, 1); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(trisCountUAV, 2); graphicsDevice.ImmediateContext.Dispatch((int)gridDimD.X, (int)gridDimD.Y, (int)gridDimD.Z); UnorderedAccessView prefixSumsUAV = prefixScan.PrefixSumArray(constantBuffer, trisCountUAV); int lastTrisCount = DirectComputeBufferHelper.CopyBuffer<int>(graphicsDevice, trisCountBuffer, nearestCount - 1, 1)[0]; int lastPrefixSum = DirectComputeBufferHelper.CopyBuffer<int>(graphicsDevice, prefixSumsUAV.Resource, nearestCount - 1, 1)[0]; int totalVerticesCount = (lastTrisCount + lastPrefixSum) * 3; if (totalVerticesCount > 0) { if (container.Geometry != null) container.Geometry.Dispose(); container.VertexCount = totalVerticesCount; container.Geometry = new Buffer(graphicsDevice, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = Marshal.SizeOf(typeof(VoxelMeshVertex)) * totalVerticesCount, Usage = ResourceUsage.Default }); Buffer verticesBuffer = new Buffer(graphicsDevice, new BufferDescription() { BindFlags = BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.StructuredBuffer, Usage = ResourceUsage.Default, SizeInBytes = Marshal.SizeOf(typeof(VoxelMeshVertex)) * totalVerticesCount, StructureByteStride = Marshal.SizeOf(typeof(VoxelMeshVertex)) }); UnorderedAccessView verticesUAV = new UnorderedAccessView(graphicsDevice, verticesBuffer); constantBufferContainer = new DirectComputeConstantBuffer() { Width = width, Height = height, Depth = depth, NearestWidth = nearestW, NearestHeight = nearestH, NearestDepth = nearestD }; data = graphicsDevice.ImmediateContext.MapSubresource(constantBuffer, MapMode.WriteDiscard, MapFlags.None); data.Data.Write<DirectComputeConstantBuffer>(constantBufferContainer); graphicsDevice.ImmediateContext.UnmapSubresource(constantBuffer, 0); graphicsDevice.ImmediateContext.ComputeShader.Set(computeMarchingCubesVertices); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(trisCountUAV, 2); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(prefixSumsUAV, 3); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(verticesUAV, 5); graphicsDevice.ImmediateContext.Dispatch((int)gridDimD.X, (int)gridDimD.Y, (int)gridDimD.Z); graphicsDevice.ImmediateContext.CopyResource(verticesBuffer, container.Geometry); verticesUAV.Dispose(); verticesBuffer.Dispose(); } else { container.VertexCount = 0; if (container.Geometry != null) container.Geometry.Dispose(); } for (int i = 0; i <= 5; i++) { graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, i); } prefixSumsUAV.Resource.Dispose(); prefixSumsUAV.Dispose(); noiseCube.Dispose(); noiseSRV.Resource.Dispose(); noiseSRV.Dispose(); voxelsBuffer.Dispose(); voxelsUAV.Dispose(); offsetsBuffer.Dispose(); offsetsUAV.Dispose(); trisCountBuffer.Dispose(); trisCountUAV.Dispose(); }
public DX11IndexBuffer(DX11RenderContext context, int elementcount, bool uav = false) { this.context = context; this.IndicesCount = elementcount; format =SlimDX.DXGI.Format.R32_UInt; BufferDescription bd = new BufferDescription() { BindFlags = BindFlags.IndexBuffer | BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.None, OptionFlags =ResourceOptionFlags.None, SizeInBytes = elementcount * sizeof(int), Usage = ResourceUsage.Default, }; if (uav && context.IsFeatureLevel11) { bd.BindFlags |= BindFlags.UnorderedAccess; bd.OptionFlags |= ResourceOptionFlags.RawBuffer; } this.Buffer = new SlimDX.Direct3D11.Buffer(context.Device, bd); if (uav) { UnorderedAccessViewDescription uavd = new UnorderedAccessViewDescription() { Format = SlimDX.DXGI.Format.R32_Typeless, Dimension = UnorderedAccessViewDimension.Buffer, Flags = UnorderedAccessViewBufferFlags.RawData, ElementCount = elementcount }; this.uav = new UnorderedAccessView(context.Device, this.Buffer, uavd); } this.CreateSRV(); }
public UnorderedAccessView PrefixSumArray(Buffer constantBuffer, UnorderedAccessView trisCountUAV) { int arrayLength = trisCountUAV.Description.ElementCount; int batchSize = trisCountUAV.Description.ElementCount / arrayLength; if (!IsPowerOfTwo(trisCountUAV.Description.ElementCount)) throw new Exception("Input array length is not power of two."); Buffer buffer = new Buffer(graphicsDevice, new BufferDescription() { BindFlags = BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.StructuredBuffer, Usage = ResourceUsage.Default, SizeInBytes = Marshal.SizeOf(typeof(int)) * trisCountUAV.Description.ElementCount, StructureByteStride = Marshal.SizeOf(typeof(int)) }); UnorderedAccessView bufferUAV = new UnorderedAccessView(graphicsDevice, buffer); Buffer output = new Buffer(graphicsDevice, new BufferDescription() { BindFlags = BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.StructuredBuffer, Usage = ResourceUsage.Default, SizeInBytes = Marshal.SizeOf(typeof(int)) * trisCountUAV.Description.ElementCount, StructureByteStride = Marshal.SizeOf(typeof(int)) }); UnorderedAccessView outputUAV = new UnorderedAccessView(graphicsDevice, output); DirectComputeConstantBuffer constantBufferContainer = new DirectComputeConstantBuffer() { PrefixSize = 4 * threadBlockSize, PrefixN = (batchSize * arrayLength) / (4 * threadBlockSize), PrefixArrayLength = arrayLength / (4 * threadBlockSize) }; DataBox data = graphicsDevice.ImmediateContext.MapSubresource(constantBuffer, MapMode.WriteDiscard, MapFlags.None); data.Data.Write<DirectComputeConstantBuffer>(constantBufferContainer); graphicsDevice.ImmediateContext.UnmapSubresource(constantBuffer, 0); Vector3 gridDim = new Vector3((batchSize * arrayLength) / (4 * threadBlockSize), 1, 1); Vector3 gridDimShared2 = new Vector3((int)Math.Ceiling(((batchSize * arrayLength) / (4 * threadBlockSize)) / (double)threadBlockSize), 1, 1); graphicsDevice.ImmediateContext.ComputeShader.Set(computeScanExclusiveShared); graphicsDevice.ImmediateContext.ComputeShader.SetConstantBuffer(constantBuffer, 0); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(trisCountUAV, 2); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(outputUAV, 3); graphicsDevice.ImmediateContext.ComputeShader.SetUnorderedAccessView(bufferUAV, 4); graphicsDevice.ImmediateContext.Dispatch((int)gridDim.X, (int)gridDim.Y, (int)gridDim.Z); graphicsDevice.ImmediateContext.ComputeShader.Set(computeScanExclusiveShared2); graphicsDevice.ImmediateContext.Dispatch((int)gridDimShared2.X, (int)gridDimShared2.Y, (int)gridDimShared2.Z); graphicsDevice.ImmediateContext.ComputeShader.Set(computeUniformUpdate); graphicsDevice.ImmediateContext.Dispatch((int)gridDim.X, (int)gridDim.Y, (int)gridDim.Z); buffer.Dispose(); bufferUAV.Dispose(); return outputUAV; }
/// <summary> /// Sets the specified data ref. /// </summary> /// <param name="dataRef">The data ref.</param> /// <param name="offset">The offset.</param> /// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns> /// <unmanaged>HRESULT ID3DX11EffectUnorderedAccessViewVariable::SetUnorderedAccessViewArray([In, Buffer] ID3D11UnorderedAccessView** ppResources,[In] unsigned int Offset,[In] unsigned int Count)</unmanaged> public void Set(UnorderedAccessView[] dataRef, int offset) { Set(dataRef, offset, dataRef.Length); }
/// <summary> /// Gets the unordered access view array. /// </summary> /// <param name="offset">The offset.</param> /// <param name="count">The count.</param> /// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns> /// <unmanaged>HRESULT ID3DX11EffectUnorderedAccessViewVariable::GetUnorderedAccessViewArray([Out, Buffer] ID3D11UnorderedAccessView** ppResources,[In] unsigned int Offset,[In] unsigned int Count)</unmanaged> public UnorderedAccessView[] GetUnorderedAccessViewArray(int offset, int count) { var temp = new UnorderedAccessView[count]; this.GetUnorderedAccessViewArray(temp, offset, count); return temp; }
/// <summary> /// Initializes render target object using specified values. If resources have been created /// earlier then it will dispose them. /// </summary> /// <param name="width">Texture width.</param> /// <param name="height">Texture height.</param> /// <param name="format">Texture format.</param> public void Initialize(int width, int height, Format format) { this.width = width; this.height = height; this.format = format; Texture2DDescription textureDescription = new Texture2DDescription() { ArraySize = 1, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource | BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, Format = format, Height = height, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, Width = width }; if (texture != null) texture.Dispose(); texture = new Texture2D(graphicsDevice, textureDescription); if (renderTargetView != null) renderTargetView.Dispose(); renderTargetView = new RenderTargetView(graphicsDevice, texture); if (shaderResourceView != null) shaderResourceView.Dispose(); shaderResourceView = new ShaderResourceView(graphicsDevice, texture); if (unorderedAccessView != null) unorderedAccessView.Dispose(); unorderedAccessView = new UnorderedAccessView(graphicsDevice, texture); }
/// <summary> /// Sets the specified view. /// </summary> /// <param name="view">The view.</param> /// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns> /// <unmanaged>HRESULT ID3DX11EffectUnorderedAccessViewVariable::SetUnorderedAccessViewArray([In, Buffer] ID3D11UnorderedAccessView** ppResources,[In] unsigned int Offset,[In] unsigned int Count)</unmanaged> public void Set(UnorderedAccessView[] view) { Set(view, 0); }
static void Main() { var form = new RenderForm("SlimDX - Conway's game of life Direct3D 11 Sample"); var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device device; SwapChain swapChain; Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, desc, out device, out swapChain); device.Factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); Texture2D backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0); var renderView = new RenderTargetView(device, backBuffer); var bytecode = ShaderBytecode.CompileFromFile("Render.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None); var effect = new Effect(device, bytecode); var technique = effect.GetTechniqueByIndex(0); var pass = technique.GetPassByIndex(0); String errors; var computeByteCode = ShaderBytecode.CompileFromFile("compute.fx", "CS", "cs_5_0", ShaderFlags.None, EffectFlags.None, null, null, out errors); var compute = new ComputeShader(device, computeByteCode); // shader variable handles var conwayResourceH = effect.GetVariableByName("tex").AsResource(); var resolutionInvH = effect.GetVariableByName("resolutionInv").AsVector(); resolutionInvH.Set(new Vector2(1.0f / form.ClientSize.Width, 1.0f / form.ClientSize.Height)); EffectVectorVariable lightPosSSH = effect.GetVariableByName("lightPosSS").AsVector(); // create texture, fill it with random data Texture2DDescription textureDesc = new Texture2DDescription() { Width = form.ClientSize.Width, Height = form.ClientSize.Height, MipLevels = 1, ArraySize = 1, CpuAccessFlags = CpuAccessFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.None, BindFlags = BindFlags.UnorderedAccess | BindFlags.ShaderResource, Format = Format.R32_Float }; var random = new Random(); var data = new float[form.ClientSize.Width * form.ClientSize.Height]; for (int i = 0; i < form.ClientSize.Width; ++i) { for (int j = 0; j < form.ClientSize.Height; ++j) data[i * form.ClientSize.Height + j] = (float)random.Next(2); } DataStream ds = new DataStream(data, true, false); DataRectangle dataRect = new DataRectangle(4 * form.ClientSize.Width, ds); Texture2D conwayTex = new Texture2D(device, textureDesc, dataRect); // Create SRV and UAV over the same texture UnorderedAccessView conwayUAV = new UnorderedAccessView(device, conwayTex); ShaderResourceView conwaySRV = new ShaderResourceView(device, conwayTex); // On the more typical setup where you switch shaders, // you will have to set the texture after every conwayResourceH.SetResource(conwaySRV); device.ImmediateContext.OutputMerger.SetTargets(renderView); device.ImmediateContext.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f)); device.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip; Vector2 lightPosSS; float angle = 0; MessagePump.Run(form, () => { // this does the light rotation angle += 0.002f; lightPosSS = new Vector2((float)Math.Sin(angle) * 0.5f + 0.5f, (float)Math.Cos(angle) * 0.5f + 0.5f); lightPosSSH.Set(lightPosSS); device.ImmediateContext.ComputeShader.Set(compute); device.ImmediateContext.ComputeShader.SetUnorderedAccessView(conwayUAV, 0); device.ImmediateContext.Dispatch(form.ClientSize.Width / 16 + 1, form.ClientSize.Height / 16 + 1, 1); // After running the CS you have to unset UAV from the shader, so you can use it as SRV device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 0); device.ImmediateContext.ClearRenderTargetView(renderView, Color.Black); for (int i = 0; i < technique.Description.PassCount; ++i) { pass.Apply(device.ImmediateContext); // No vertices are send as they are created in the vertex shader on the fly. device.ImmediateContext.Draw(4, 0); } swapChain.Present(0, PresentFlags.None); }); computeByteCode.Dispose(); conwayUAV.Dispose(); conwaySRV.Dispose(); conwayTex.Dispose(); ds.Dispose(); bytecode.Dispose(); effect.Dispose(); renderView.Dispose(); backBuffer.Dispose(); device.Dispose(); swapChain.Dispose(); }
public void BindAsRenderTarget(DeviceContext context, bool depth = false, bool color = true) { RenderTargetView[] renderTargetViews = new RenderTargetView[m_NumRTs]; UnorderedAccessView[] unorderedAccessViews = new UnorderedAccessView[m_UAVsNum]; for (int i = 0; i < m_NumRTs; ++i) { renderTargetViews[i] = m_RenderTargets[i].m_RenderTargetView; } for (int i = 0; i < m_UAVsNum; ++i) { unorderedAccessViews[i] = m_UAVs[i].m_UnorderedAccessView; } if (m_UAVsNum > 0) { context.OutputMerger.SetTargets(depth ? m_DepthStencil.m_DepthStencilView : null, color ? m_NumRTs : 0, color ? unorderedAccessViews : null, color ? renderTargetViews : null); } else { if (SurfaceDebugManager.m_GPUDebugOn) { UnorderedAccessView[] unorderedAccessViewsDebug = new UnorderedAccessView[1]; unorderedAccessViewsDebug[0] = SurfaceDebugManager.m_DebugAppendBuffer.m_UnorderedAccessView; int[] initialLenghts = new int[1]; initialLenghts[0] = SurfaceDebugManager.m_FirstCallThisFrame ? 0 : -1; SurfaceDebugManager.m_FirstCallThisFrame = false; context.OutputMerger.SetTargets(depth ? m_DepthStencil.m_DepthStencilView : null, 7, unorderedAccessViewsDebug, initialLenghts, color ? renderTargetViews : null); } else { context.OutputMerger.SetTargets(depth ? m_DepthStencil.m_DepthStencilView : null, renderTargetViews); } } context.Rasterizer.SetViewports(m_Viewport); context.OutputMerger.DepthStencilState = depth ? m_DepthStencilState : m_DepthStencilStateNoDepth; }
public void SetByName(string name, UnorderedAccessView value, int counter = -1) { this.effect.GetVariableByName(name).AsUnorderedAccessView().SetView(value); this.resetuavs.Add(new CounterResetUAV(value, counter)); }
public void Resize(int w, int h) { if (this.RTV != null) { this.RTV.Dispose(); } if (this.SRV != null) { this.SRV.Dispose(); } if (this.UAV != null) { this.UAV.Dispose(); } this.Resource.Dispose(); this.swapchain.ResizeBuffers(1,w, h, SlimDX.DXGI.Format.Unknown, SwapChainFlags.AllowModeSwitch); this.Resource = Texture2D.FromSwapChain<Texture2D>(this.swapchain, 0); this.RTV = new RenderTargetView(context.Device, this.Resource); this.SRV = new ShaderResourceView(context.Device, this.Resource); if (this.allowuav) { this.UAV = new UnorderedAccessView(context.Device, this.Resource); } this.desc = this.Resource.Description; }