Beispiel #1
0
 public void Dispose()
 {
     InputLayout.Dispose();
     VertexShader.Dispose();
     GeometryShader?.Dispose();
     FragmentShader.Dispose();
 }
Beispiel #2
0
 public void Dispose()
 {
     VertexShader?.Dispose();
     GeometryShader?.Dispose();
     PixelShader?.Dispose();
     Layout?.Dispose();
 }
Beispiel #3
0
        /// <summary>
        /// Release Elements
        /// </summary>
        public void Dispose()
        {
            if (VertexShader != null)
            {
                VertexShader.Dispose();
            }

            if (PixelShader != null)
            {
                PixelShader.Dispose();
            }

            if (GeometryShader != null)
            {
                GeometryShader.Dispose();
            }

            if (DomainShader != null)
            {
                DomainShader.Dispose();
            }

            if (HullShader != null)
            {
                HullShader.Dispose();
            }

            if (Layout != null)
            {
                Layout.Dispose();
            }
        }
Beispiel #4
0
 void IDisposable.Dispose()
 {
     VS.Dispose();
     GS_Tri.Dispose();
     PS.Dispose();
     RS.Dispose();
 }
Beispiel #5
0
 public void Dispose()
 {
     vertex?.Dispose();
     pixel?.Dispose();
     compute?.Dispose();
     geometry?.Dispose();
 }
Beispiel #6
0
 public void Dispose()
 {
     VertexShader?.Dispose();
     PixelShader?.Dispose();
     GeometryShader?.Dispose();
     Signature?.Dispose();
     InputLayout?.Dispose();
 }
Beispiel #7
0
 public void Dispose()
 {
     InputLayout.Dispose();
     VertexShader.Dispose();
     GeometryShader?.Dispose();
     FragmentShader.Dispose();
     GL.DeleteProgram(ProgramID);
 }
 public void Dispose()
 {
     InputLayout.Dispose();
     VertexShader.Dispose();
     TessellationControlShader?.Dispose();
     TessellationEvaluationShader?.Dispose();
     GeometryShader?.Dispose();
     FragmentShader.Dispose();
 }
 public void Dispose()
 {
     vertexRefinerGeometryShader.Dispose();
     foreach (IDisposable disposable in shaderResources)
     {
         disposable.Dispose();
     }
     refinedVertexBuffer.Dispose();
 }
Beispiel #10
0
 public override void Dispose()
 {
     base.Dispose();
     Utilities.Dispose(ref _cb);
     _tri.Dispose();
     _quad.Dispose();
     _HShader.Dispose();
     _DShader.Dispose();
     _GShader.Dispose();
 }
Beispiel #11
0
        public override void Dispose()
        {
            geometryShader?.Dispose();
            vertexShader?.Dispose();
            pixelShader?.Dispose();
            layout?.Dispose();
            Texture?.Dispose();

            blendStateDepth?.Dispose();

            depthStencilStateNoDepth?.Dispose();
        }
Beispiel #12
0
 public void Dispose()
 {
     geometryShader.Dispose();
     vertexShaderSo.Dispose();
     soInputSignature.Dispose();
     soInputLayout.Dispose();
     colorPixelShader.Dispose();
     colorVertexShader.Dispose();
     colorInputSignature.Dispose();
     colorInputLayout.Dispose();
     constantBufferColor.Dispose();
     bufferForSo.Dispose();
     soBuffer.Dispose();
 }
        private void ShuddownShader()
        {
            SamplerState?.Dispose();
            SamplerState = null;
            // Release the matrix constant buffer.
            ConstantMatrixBuffer?.Dispose();
            ConstantMatrixBuffer = null;
            // Release the layout.
            Layout?.Dispose();
            Layout = null;
            // Release the pixel shader.
            PixelShader?.Dispose();
            PixelShader = null;
            // Release the vertex shader.
            VertexShader?.Dispose();
            VertexShader = null;

            GeometryShader?.Dispose();
            GeometryShader = null;
        }
Beispiel #14
0
        private System.Tuple <Shader, System.Exception> LoadShader(string shaderFile, params InputElement[] inputElements)
        {
            var result = new Shader()
            {
                vertexShader   = null,
                pixelShader    = null,
                geometryShader = null,
                layout         = null,
                validState     = false
            };

            ShaderBytecode  vertexShaderByteCode   = null;
            ShaderBytecode  pixelShaderByteCode    = null;
            ShaderBytecode  geometryShaderByteCode = null;
            ShaderSignature signature      = null;
            VertexShader    vertexShader   = null;
            PixelShader     pixelShader    = null;
            GeometryShader  geometryShader = null;
            InputLayout     layout         = null;

            System.Exception error = null;
            try
            {
                var shaderFileBytecode = File.ReadAllBytes(shaderFile);
                vertexShaderByteCode = ShaderBytecode.Compile(shaderFileBytecode, "VS", "vs_4_0", ShaderFlags.OptimizationLevel0);
                vertexShader         = new VertexShader(device, vertexShaderByteCode);
                var reflection = new ShaderReflection(vertexShaderByteCode);
                /* Iterate through constant buffers */
                for (int i = 0; i < reflection.Description.ConstantBuffers; ++i)
                {
                    var cb = reflection.GetConstantBuffer(i);
                    if (cb.Description.Name == "worldViewProj")
                    {
                        result.vertexShaderSlot.worldViewProj = i;
                    }
                }
                reflection.Dispose();

                pixelShaderByteCode = ShaderBytecode.Compile(shaderFileBytecode, "PS", "ps_4_0", ShaderFlags.OptimizationLevel0);
                pixelShader         = new PixelShader(device, pixelShaderByteCode);

                try
                {
                    geometryShaderByteCode = ShaderBytecode.Compile(shaderFileBytecode, "GS", "gs_4_0", ShaderFlags.OptimizationLevel0);
                    geometryShader         = new GeometryShader(device, geometryShaderByteCode);
                }
                catch (CompilationException e)
                {
                    if (!e.Message.Contains("'GS': entrypoint"))
                    {
                        throw e;
                    }
                }

                signature = ShaderSignature.GetInputSignature(vertexShaderByteCode);
                layout    = new InputLayout(device, signature, inputElements);
                signature.Dispose();

                result.vertexShader   = vertexShader;
                result.pixelShader    = pixelShader;
                result.geometryShader = geometryShader;
                result.layout         = layout;
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine("Error while compiling shader {0}:\n{1}", shaderFile, e);
                error = e;
            }
            finally
            {
                if (geometryShaderByteCode != null)
                {
                    geometryShaderByteCode.Dispose();
                }
                if (vertexShaderByteCode != null)
                {
                    vertexShaderByteCode.Dispose();
                }
                if (pixelShaderByteCode != null)
                {
                    pixelShaderByteCode.Dispose();
                }
                if (geometryShaderByteCode != null)
                {
                    geometryShaderByteCode.Dispose();
                }
                if (signature != null)
                {
                    signature.Dispose();
                }

                if (error != null && vertexShader != null)
                {
                    vertexShader.Dispose();
                }
                if (error != null && pixelShader != null)
                {
                    pixelShader.Dispose();
                }
                if (error != null && geometryShader != null)
                {
                    geometryShader.Dispose();
                }
                if (error != null && layout != null)
                {
                    signature.Dispose();
                }
            }

            result.validState = error == null;

            return(System.Tuple.Create(result, error));
        }
        public static BasicHlsl.VertexShaderOutput ExecuteVertexShader(string compiledShaderFile,
                                                                       BasicHlsl.ConstantBufferGlobals globals, VertexPositionNormalTexture vertex)
        {
            var device = new Device(DriverType.Warp);

            var vertexShaderBytes    = File.ReadAllBytes(compiledShaderFile);
            var vertexShaderBytecode = new ShaderBytecode(vertexShaderBytes);
            var vertexShader         = new VertexShader(device, vertexShaderBytecode);

            var layout = new InputLayout(device,
                                         ShaderSignature.GetInputSignature(vertexShaderBytecode),
                                         new[]
            {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("NORMAL", 0, Format.R32G32B32_Float, 16, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32_Float, 28, 0)
            });

            var vertices = Buffer.Create(device, BindFlags.VertexBuffer, new[] { vertex });

            var constantBuffer = Buffer.Create(device, ref globals, new BufferDescription
            {
                BindFlags      = BindFlags.ConstantBuffer,
                CpuAccessFlags = CpuAccessFlags.None,
                Usage          = ResourceUsage.Default
            });

            var geometryShader = new GeometryShader(device, vertexShaderBytecode,
                                                    new[]
            {
                new StreamOutputElement {
                    SemanticName = "SV_POSITION", ComponentCount = 4
                },
                new StreamOutputElement {
                    SemanticName = "COLOR", ComponentCount = 4
                },
                new StreamOutputElement {
                    SemanticName = "TEXCOORD", ComponentCount = 2
                }
            },
                                                    BasicHlsl.VertexShaderOutput.SizeInBytes);

            var outputBuffer = Buffer.Create(device, new BasicHlsl.VertexShaderOutput[1],
                                             new BufferDescription
            {
                CpuAccessFlags = CpuAccessFlags.None,
                BindFlags      = BindFlags.StreamOutput,
                Usage          = ResourceUsage.Default
            });

            var stagingBuffer = Buffer.Create(device, new BasicHlsl.VertexShaderOutput[1],
                                              new BufferDescription
            {
                CpuAccessFlags = CpuAccessFlags.Read,
                BindFlags      = BindFlags.None,
                Usage          = ResourceUsage.Staging
            });

            device.InputAssembler.InputLayout       = layout;
            device.InputAssembler.PrimitiveTopology = PrimitiveTopology.PointList;
            device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, VertexPositionNormalTexture.SizeInBytes, 0));
            device.VertexShader.SetConstantBuffer(0, constantBuffer);
            device.VertexShader.Set(vertexShader);
            device.GeometryShader.Set(geometryShader);
            device.StreamOutput.SetTargets(new StreamOutputBufferBinding(outputBuffer, 0));

            device.Draw(1, 0);

            device.CopyResource(outputBuffer, stagingBuffer);
            device.Flush();

            var stream = stagingBuffer.Map(MapMode.Read, SharpDX.Direct3D10.MapFlags.None);
            var bytes  = new byte[BasicHlsl.VertexShaderOutput.SizeInBytes];

            stream.Read(bytes, 0, bytes.Length);
            stream.Dispose();

            outputBuffer.Dispose();
            vertices.Dispose();
            layout.Dispose();
            geometryShader.Dispose();
            vertexShader.Dispose();
            vertexShaderBytecode.Dispose();
            device.Dispose();

            return(StructUtility.FromBytes <BasicHlsl.VertexShaderOutput>(bytes));
        }
Beispiel #16
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form1();

            Device          device;
            SwapChain       swapChain;
            ShaderSignature inputSignature;
            VertexShader    vertexShader;
            GeometryShader  geometryShader;
            PixelShader     pixelShader;

            var description = new SwapChainDescription()
            {
                BufferCount       = 2,
                Usage             = Usage.RenderTargetOutput,
                OutputHandle      = form.Handle,
                IsWindowed        = true,
                ModeDescription   = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags             = SwapChainFlags.AllowModeSwitch,
                SwapEffect        = SwapEffect.Discard
            };

            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, description, out device, out swapChain);

            // create a view of our render target, which is the backbuffer of the swap chain we just created
            RenderTargetView renderTarget;

            using (var resource = Resource.FromSwapChain <Texture2D>(swapChain, 0))
                renderTarget = new RenderTargetView(device, resource);

            // setting a viewport is required if you want to actually see anything
            var context  = device.ImmediateContext;
            var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);

            context.OutputMerger.SetTargets(renderTarget);
            context.Rasterizer.SetViewports(viewport);

            // load and compile the vertex shader
            using (var bytecode = ShaderBytecode.CompileFromFile("Voxel.fx", "VS", "vs_4_0", ShaderFlags.None, EffectFlags.None))
            {
                inputSignature = ShaderSignature.GetInputSignature(bytecode);
                vertexShader   = new VertexShader(device, bytecode);
            }

            using (var bytecode = ShaderBytecode.CompileFromFile("Voxel.fx", "GS", "gs_4_0", ShaderFlags.None, EffectFlags.None))
            {
                geometryShader = new GeometryShader(device, bytecode);
            }

            // load and compile the pixel shader
            using (var bytecode = ShaderBytecode.CompileFromFile("Voxel.fx", "PS", "ps_4_0", ShaderFlags.None, EffectFlags.None))
                pixelShader = new PixelShader(device, bytecode);

            // create test vertex data, making sure to rewind the stream afterward
            var vertices = new DataStream(12 * 3, true, true);

            vertices.Write(new Vector3(0.0f, 0.5f, 0.5f));
            vertices.Write(new Vector3(0.5f, -0.5f, 0.5f));
            vertices.Write(new Vector3(-0.5f, -0.5f, 0.5f));
            vertices.Position = 0;

            // create the vertex layout and buffer
            var elements     = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0) };
            var layout       = new InputLayout(device, inputSignature, elements);
            var vertexBuffer = new Buffer(device, vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            // configure the Input Assembler portion of the pipeline with the vertex data
            context.InputAssembler.InputLayout       = layout;
            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 12, 0));

            // set the shaders
            context.VertexShader.Set(vertexShader);
            // context.GeometryShader.Set(geometryShader);
            context.PixelShader.Set(pixelShader);

            // prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
            using (var factory = swapChain.GetParent <Factory>())
                factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);

            // handle alt+enter ourselves
            form.KeyDown += (o, e) =>
            {
                if (e.Alt && e.KeyCode == Keys.Enter)
                {
                    swapChain.IsFullScreen = !swapChain.IsFullScreen;
                }
            };

            // handle form size changes
            form.Resize += (o, e) =>
            {
                renderTarget.Dispose();

                swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
                using (var resource = Resource.FromSwapChain <Texture2D>(swapChain, 0))
                    renderTarget = new RenderTargetView(device, resource);

                context.OutputMerger.SetTargets(renderTarget);
            };

            MessagePump.Run(form, () =>
            {
                // clear the render target to a soothing blue
                context.ClearRenderTargetView(renderTarget, new Color4(0.5f, 0.5f, 1.0f));

                // draw the triangle
                context.Draw(3, 0);
                swapChain.Present(0, PresentFlags.None);
            });

            // clean up all resources
            // anything we missed will show up in the debug output
            vertices.Close();
            vertexBuffer.Dispose();
            layout.Dispose();
            inputSignature.Dispose();
            vertexShader.Dispose();
            geometryShader.Dispose();
            pixelShader.Dispose();
            renderTarget.Dispose();
            swapChain.Dispose();
            device.Dispose();
        }
Beispiel #17
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form1();

            Device device;
            SwapChain swapChain;
            ShaderSignature inputSignature;
            VertexShader vertexShader;
            GeometryShader geometryShader;
            PixelShader pixelShader;

            var description = new SwapChainDescription()
            {
                BufferCount = 2,
                Usage = Usage.RenderTargetOutput,
                OutputHandle = form.Handle,
                IsWindowed = true,
                ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags = SwapChainFlags.AllowModeSwitch,
                SwapEffect = SwapEffect.Discard
            };

            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, description, out device, out swapChain);

            // create a view of our render target, which is the backbuffer of the swap chain we just created
            RenderTargetView renderTarget;
            using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
                renderTarget = new RenderTargetView(device, resource);

            // setting a viewport is required if you want to actually see anything
            var context = device.ImmediateContext;
            var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
            context.OutputMerger.SetTargets(renderTarget);
            context.Rasterizer.SetViewports(viewport);

            // load and compile the vertex shader
            using (var bytecode = ShaderBytecode.CompileFromFile("Voxel.fx", "VS", "vs_4_0", ShaderFlags.None, EffectFlags.None))
            {
                inputSignature = ShaderSignature.GetInputSignature(bytecode);
                vertexShader = new VertexShader(device, bytecode);
            }

            using (var bytecode = ShaderBytecode.CompileFromFile("Voxel.fx", "GS", "gs_4_0", ShaderFlags.None, EffectFlags.None))
            {
                geometryShader = new GeometryShader(device, bytecode);
            }

            // load and compile the pixel shader
            using (var bytecode = ShaderBytecode.CompileFromFile("Voxel.fx", "PS", "ps_4_0", ShaderFlags.None, EffectFlags.None))
                pixelShader = new PixelShader(device, bytecode);

            // create test vertex data, making sure to rewind the stream afterward
            var vertices = new DataStream(12 * 3, true, true);
            vertices.Write(new Vector3(0.0f, 0.5f, 0.5f));
            vertices.Write(new Vector3(0.5f, -0.5f, 0.5f));
            vertices.Write(new Vector3(-0.5f, -0.5f, 0.5f));
            vertices.Position = 0;

            // create the vertex layout and buffer
            var elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0) };
            var layout = new InputLayout(device, inputSignature, elements);
            var vertexBuffer = new Buffer(device, vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            // configure the Input Assembler portion of the pipeline with the vertex data
            context.InputAssembler.InputLayout = layout;
            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 12, 0));

            // set the shaders
            context.VertexShader.Set(vertexShader);
            // context.GeometryShader.Set(geometryShader);
            context.PixelShader.Set(pixelShader);

            // prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
            using (var factory = swapChain.GetParent<Factory>())
                factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);

            // handle alt+enter ourselves
            form.KeyDown += (o, e) =>
            {
                if (e.Alt && e.KeyCode == Keys.Enter)
                    swapChain.IsFullScreen = !swapChain.IsFullScreen;
            };

            // handle form size changes
            form.Resize += (o, e) =>
            {
                renderTarget.Dispose();

                swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
                using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
                    renderTarget = new RenderTargetView(device, resource);

                context.OutputMerger.SetTargets(renderTarget);
            };

            MessagePump.Run(form, () =>
            {
                // clear the render target to a soothing blue
                context.ClearRenderTargetView(renderTarget, new Color4(0.5f, 0.5f, 1.0f));

                // draw the triangle
                context.Draw(3, 0);
                swapChain.Present(0, PresentFlags.None);
            });

            // clean up all resources
            // anything we missed will show up in the debug output
            vertices.Close();
            vertexBuffer.Dispose();
            layout.Dispose();
            inputSignature.Dispose();
            vertexShader.Dispose();
            geometryShader.Dispose();
            pixelShader.Dispose();
            renderTarget.Dispose();
            swapChain.Dispose();
            device.Dispose();
        }