private ShaderResourceView NormalizeMax(ShaderResourceView view, Size size) { var max = Math.Max(size.Width, size.Height); if (max == 1) { using (var temporary = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) { temporary.Resize(DeviceContextHolder, 1, 1, null); UseEffect(e => { e.FxInputMap.SetResource(view); e.FxOverlayMap.SetResource(view); e.TechMaximumApply.DrawAllPasses(DeviceContext, 6); }, temporary); temporary.KeepView = true; return(temporary.View); } } var originalView = view; for (var i = max / 4; i > 1 || ReferenceEquals(view, originalView); i /= 4) { if (i < 1) { i = 1; } using (var temporary = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) { temporary.Resize(DeviceContextHolder, i, i, null); UseEffect(e => { e.FxInputMap.SetResource(view); e.TechMaximum.DrawAllPasses(DeviceContext, 6); }, temporary); if (!ReferenceEquals(view, originalView)) { view.Dispose(); } view = temporary.View; temporary.KeepView = true; } } using (var temporary = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) { temporary.Resize(DeviceContextHolder, size.Width, size.Height, null); UseEffect(e => { e.FxInputMap.SetResource(originalView); e.FxOverlayMap.SetResource(view); e.TechMaximumApply.DrawAllPasses(DeviceContext, 6); }, temporary); view.Dispose(); temporary.KeepView = true; return(temporary.View); } }
public void RecreateSRV() { if (ressource != null) { ressource.Dispose(); } ressource = new ShaderResourceView(Display.device, Tex); }
public void Dispose() { scatterer?.Dispose(); vertexRefiner.Dispose(); foreach (var surface in surfaces) { surface.Dispose(); } materialSet.Dispose(); inputLayout.Dispose(); texturedVertexInfoPairsView?.Dispose(); }
public void End() { if (this.TextureSize == Vector2.Zero) { return; } this.DWRenderTarget.EndDraw(); this.MutexD3D10.Release(0); this.MutexD3D11.Acquire(0, 100); ShaderResourceView srv = new ShaderResourceView(this.DeviceManager.Device, this.TextureD3D11); BlendState lastBlendState = this.DeviceManager.Context.OutputMerger.BlendState; Viewport[] lastViewports = this.DeviceManager.Context.Rasterizer.GetViewports(); //Pass the WVP matrix effect this.SpriteEffect.GetVariableBySemantic("WORLDVIEWPROJECTION") .AsMatrix() .SetMatrix(this.ViewMatrix * this.SpriteProjectionMatrix); this.SpriteEffect.GetVariableBySemantic("SPRITETEXTURE").AsResource().SetResource(srv); this.DeviceManager.Context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(this.VertexBuffer, SpriteVertexLayout.SizeInBytes, 0)); this.DeviceManager.Context.InputAssembler.InputLayout = this.VertexInputLayout; this.DeviceManager.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; this.DeviceManager.Context.OutputMerger.BlendState = this.state; this.DeviceManager.Context.Rasterizer.SetViewports(this.spriteViewport); this.renderPass.Apply(this.DeviceManager.Context); this.DeviceManager.Context.Draw(6, 0); this.DeviceManager.Context.Rasterizer.SetViewports(lastViewports); this.DeviceManager.Context.OutputMerger.BlendState = lastBlendState; srv.Dispose(); this.MutexD3D11.Release(0); }
public void Unload() { PixelShader.Dispose(); VertexShader.Dispose(); TextureView.Dispose(); Sampler.Dispose(); BlendState.Dispose(); Texture2D.Dispose(); VertexBufferBinding.Buffer.Dispose(); VertexBuffer.Dispose(); Layout.Dispose(); ConstantBuffer.Dispose(); this.PixelShader = null; this.VertexShader = null; this.TextureView = null; this.Sampler = null; this.BlendState = null; this.Texture2D = null; this.VertexBufferBinding.Buffer = null; this.VertexBuffer = null; this.Layout = null; this.ConstantBuffer = null; }
public void Dispose() { if (_renderTargetView != null) { _renderTargetView.Dispose(); } if (_depthStencilView != null) { _depthStencilView.Dispose(); } if (_depthStencilTexture != null) { _depthStencilTexture.Dispose(); } if (_redDotTexture != null) { _redDotTexture.Dispose(); } if (_spriteRenderer != null) { _spriteRenderer.Dispose(); } }
public void FreeDeviceTexture() { mTexture?.Dispose(); mTexture = null; mResourceView?.Dispose(); mResourceView = null; }
private void Dispose(bool disposing) { if (mTexture != null) { mTexture.Dispose(); mTexture = null; } if (mView != null) { mView.Dispose(); mView = null; } mDevice = null; }
public void Dispose() { if (_shaderEffect != null) { _shaderEffect.Dispose(); } if (_textureView != null) { _textureView.Dispose(); } if (_indexBuffer != null) { _indexBuffer.Dispose(); } if (_vertexBuffer != null) { _vertexBuffer.Dispose(); } if (_accIndexBuffer != null) { _accIndexBuffer.Dispose(); } if (_valencePrefixBuffer != null) { _valencePrefixBuffer.Dispose(); } if (_valencePrefixView != null) { _valencePrefixView.Dispose(); } }
public void Dispose() { while (Models.Count > 0) { Model model = Models[0]; model.Dispose(); Models.RemoveAt(0); } while (FileShaderResourceViewDictionary.Count > 0) { string key = FileShaderResourceViewDictionary.Keys[0]; ShaderResourceView fsResourceView = FileShaderResourceViewDictionary[key]; fsResourceView.Dispose(); FileShaderResourceViewDictionary.Remove(key); } while (FileTextureDictionary.Count > 0) { string key = FileTextureDictionary.Keys[0]; Texture2D texture = FileTextureDictionary[key]; texture.Dispose(); FileTextureDictionary.Remove(key); } while (_TexturesAsPNGs.Count > 0) { string key = _TexturesAsPNGs.Keys[0]; Bitmap bitmap = _TexturesAsPNGs[key]; bitmap.Dispose(); _TexturesAsPNGs.Remove(key); } }
public void Dispose() { shader.Dispose(); box.Dispose(); diffuseTextureRv.Dispose(); GBuffer.Dispose(); }
public void Dispose() { if (_texture != null) { _texture.Dispose(); } }
private static void Main() { // Device creation var form = new RenderForm("Stereo test") { ClientSize = size, //FormBorderStyle = System.Windows.Forms.FormBorderStyle.None, //WindowState = FormWindowState.Maximized }; form.KeyDown += new KeyEventHandler(form_KeyDown); // form.Resize += new EventHandler(form_Resize); ModeDescription mDesc = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(120000, 1000), Format.R8G8B8A8_UNorm); mDesc.ScanlineOrdering = DisplayModeScanlineOrdering.Progressive; mDesc.Scaling = DisplayModeScaling.Unspecified; var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = mDesc, Flags = SwapChainFlags.AllowModeSwitch, IsWindowed = false, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device.CreateWithSwapChain(null, DriverType.Hardware, DeviceCreationFlags.Debug, desc, out device, out swapChain); //Stops Alt+enter from causing fullscreen skrewiness. factory = swapChain.GetParent<Factory>(); factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); backBuffer = Resource.FromSwapChain<Texture2D>(swapChain, 0); renderView = new RenderTargetView(device, backBuffer); ImageLoadInformation info = new ImageLoadInformation() { BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, FilterFlags = FilterFlags.None, Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, MipFilterFlags = FilterFlags.None, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Staging, MipLevels = 1 }; // Make texture 3D sourceTexture = Texture2D.FromFile(device, "medusa.jpg", info); ImageLoadInformation info2 = new ImageLoadInformation() { BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.None, FilterFlags = FilterFlags.None, Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, MipFilterFlags = FilterFlags.None, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Default, MipLevels = 1 }; Texture2D tShader = Texture2D.FromFile(device, "medusa.jpg", info2); srv = new ShaderResourceView(device, tShader); //ResizeDevice(new Size(1920, 1080), true); // Create a quad that fills the whole screen BuildQuad(); // Create world view (ortho) projection matrices //QuaternionCam qCam = new QuaternionCam(); // Load effect from file. It is a basic effect that renders a full screen quad through // an ortho projectio=n matrix effect = Effect.FromFile(device, "Texture.fx", "fx_4_0", ShaderFlags.Debug, EffectFlags.None); EffectTechnique technique = effect.GetTechniqueByIndex(0); EffectPass pass = technique.GetPassByIndex(0); InputLayout layout = new InputLayout(device, pass.Description.Signature, new[] { new InputElement( "POSITION", 0, Format. R32G32B32A32_Float, 0, 0), new InputElement( "TEXCOORD", 0, Format. R32G32_Float, 16, 0) }); //effect.GetVariableByName("mWorld").AsMatrix().SetMatrix( // Matrix.Translation(Layout.OrthographicTransform(Vector2.Zero, 99, size))); //effect.GetVariableByName("mView").AsMatrix().SetMatrix(qCam.View); //effect.GetVariableByName("mProjection").AsMatrix().SetMatrix(qCam.OrthoProjection); //effect.GetVariableByName("tDiffuse").AsResource().SetResource(srv); // Set RT and Viewports device.OutputMerger.SetTargets(renderView); device.Rasterizer.SetViewports(new Viewport(0, 0, size.Width, size.Height, 0.0f, 1.0f)); // Create solid rasterizer state RasterizerStateDescription rDesc = new RasterizerStateDescription() { CullMode = CullMode.None, IsDepthClipEnabled = true, FillMode = FillMode.Solid, IsAntialiasedLineEnabled = false, IsFrontCounterclockwise = true, //IsMultisampleEnabled = true, }; RasterizerState rState = RasterizerState.FromDescription(device, rDesc); device.Rasterizer.State = rState; Texture2DDescription rtDesc = new Texture2DDescription { ArraySize = 1, Width = size.Width, Height = size.Height, BindFlags = BindFlags.RenderTarget, CpuAccessFlags = CpuAccessFlags.None, Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Default, MipLevels = 1, SampleDescription = new SampleDescription(1, 0) }; rtTex = new Texture2D(device, rtDesc); rv = new RenderTargetView(device, rtTex); stereoizedTexture = Make3D(sourceTexture); //ResizeDevice(new Size(1920, 1080), true); Console.WriteLine(form.ClientSize); // Main Loop MessagePump.Run(form, () => { device.ClearRenderTargetView(renderView, Color.Cyan); //device.InputAssembler.SetInputLayout(layout); //device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList); //device.OutputMerger.SetTargets(rv); //device.InputAssembler.SetVertexBuffers(0, // new VertexBufferBinding(vertices, 24, 0)); //device.InputAssembler.SetIndexBuffer(indices, Format.R16_UInt, 0); //for (int i = 0; i < technique.Description.PassCount; ++i) //{ // // Render the full screen quad // pass.Apply(); // device.DrawIndexed(6, 0, 0); //} ResourceRegion stereoSrcBox = new ResourceRegion { Front = 0, Back = 1, Top = 0, Bottom = size.Height, Left = 0, Right = size.Width }; device.CopySubresourceRegion(stereoizedTexture, 0, stereoSrcBox, backBuffer, 0, 0, 0, 0); //device.CopyResource(rv.Resource, backBuffer); swapChain.Present(0, PresentFlags.None); }); // Dispose resources vertices.Dispose(); layout.Dispose(); effect.Dispose(); renderView.Dispose(); backBuffer.Dispose(); device.Dispose(); swapChain.Dispose(); rState.Dispose(); stereoizedTexture.Dispose(); sourceTexture.Dispose(); indices.Dispose(); srv.Dispose(); }
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(); }