예제 #1
0
        // Zavedenie zdrojov
        public void LoadResources(DX11.Device device)
        {
            Vertex[] vertices;
            short[]  indices;
            if (telesoObjektu.Equals("Ihlan"))
            {
                //Ihlan - pyramid
                vertices = new Vertex[]
                {
                    new Vertex(new Vector3(-1, -1, 1), Color.Red.ToArgb()),
                    new Vertex(new Vector3(1, -1, 1), Color.LightBlue.ToArgb()),
                    new Vertex(new Vector3(-1, -1, -1), Color.LightCyan.ToArgb()),
                    new Vertex(new Vector3(1, -1, -1), Color.CadetBlue.ToArgb()),
                    new Vertex(new Vector3(0, 2, 0), Color.Yellow.ToArgb()),
                };

                indices = new short[]
                {
                    //Podstava
                    0, 2, 1,
                    1, 2, 3,
                    //Steny
                    0, 1, 4,
                    1, 3, 4,
                    3, 2, 4,
                    2, 0, 4
                };
            }
            else
            {
                //Kocka - box
                vertices = new Vertex[]   //Matica 8 vrcholov
                {
                    new Vertex(new Vector3(-1, -1, -1), Color.Red.ToArgb()),
                    new Vertex(new Vector3(1, -1, -1), Color.LightBlue.ToArgb()),
                    new Vertex(new Vector3(1, -1, 1), Color.LightCyan.ToArgb()),
                    new Vertex(new Vector3(-1, -1, 1), Color.CadetBlue.ToArgb()),
                    new Vertex(new Vector3(-1, 1, -1), Color.Red.ToArgb()),
                    new Vertex(new Vector3(1, 1, -1), Color.Orange.ToArgb()),
                    new Vertex(new Vector3(1, 1, 1), Color.Goldenrod.ToArgb()),
                    new Vertex(new Vector3(-1, 1, 1), Color.Yellow.ToArgb())
                };

                indices = new short[] //Matica 36 indexov
                {
                    4, 1, 0, 4, 5, 1, //Stena 1 ...
                    5, 2, 1, 5, 6, 2,
                    6, 3, 2, 6, 7, 3,
                    7, 0, 3, 7, 4, 0,
                    7, 5, 4, 7, 6, 5,
                    2, 3, 0, 2, 0, 1
                };
            }



            DataStream outStream      = new DataStream(8 * Marshal.SizeOf(typeof(Vertex)), true, true); //Inicializácia objektu typu DataStream pre vertex buffer
            DataStream outStreamIndex = new DataStream(36 * Marshal.SizeOf(typeof(short)), true, true); //Index buffer

            for (int loop = 0; loop < vertices.Length; loop++)                                          //Načítanie hodnôt
            {
                outStream.Write <Vertex>(vertices[loop]);
            }

            for (int loop = 0; loop < indices.Length; loop++)     //Načítanie hodnôt
            {
                outStreamIndex.Write <short>(indices[loop]);
            }

            outStream.Position      = 0;   //Nastavenie polohy
            outStreamIndex.Position = 0;

            DX11.BufferDescription bufferDescription = new DX11.BufferDescription();
            bufferDescription.BindFlags      = DX11.BindFlags.VertexBuffer;
            bufferDescription.CpuAccessFlags = DX11.CpuAccessFlags.None;
            bufferDescription.OptionFlags    = DX11.ResourceOptionFlags.None;
            bufferDescription.SizeInBytes    = 8 * Marshal.SizeOf(typeof(Vertex));
            bufferDescription.Usage          = DX11.ResourceUsage.Default;

            DX11.BufferDescription bufferDescriptionIndex = new DX11.BufferDescription();
            bufferDescriptionIndex.BindFlags      = DX11.BindFlags.IndexBuffer;
            bufferDescriptionIndex.CpuAccessFlags = DX11.CpuAccessFlags.None;
            bufferDescriptionIndex.OptionFlags    = DX11.ResourceOptionFlags.None;
            bufferDescriptionIndex.SizeInBytes    = 36 * Marshal.SizeOf(typeof(short));
            bufferDescriptionIndex.Usage          = DX11.ResourceUsage.Default;

            m_vertexBuffer = new DX11.Buffer(device, outStream, bufferDescription);           //Inicializácia pre vertex buffer
            m_indexBuffer  = new DX11.Buffer(device, outStreamIndex, bufferDescriptionIndex); //Index buffer
            outStream.Close();
            outStreamIndex.Close();

            //Preloženie efektového súboru (HLSL) a priradenie objektu m_ShaderByteCode
            D3DCompiler.ShaderBytecode m_shaderByteCode =
                D3DCompiler.ShaderBytecode.CompileFromFile("Shader1.fx",
                                                           "fx_5_0",
                                                           D3DCompiler.ShaderFlags.None,
                                                           D3DCompiler.EffectFlags.None);

            m_effect            = new DX11.Effect(device, m_shaderByteCode);              //Objekt pre správu množiny stavovývh objektov, zdrojov a šejdrov pre implementáciu D3D renderovacieho efektu
            m_effectTechnique   = m_effect.GetTechniqueByIndex(0);                        //Získať techniku cez index
            m_effectPass        = m_effectTechnique.GetPassByIndex(0);                    //Získať prechod cez index
            m_transformVariable = m_effect.GetVariableByName("WorldViewProj").AsMatrix(); //Získanie premennej cez meno

            DX11.InputElement[] m_inputElements = new DX11.InputElement[]                 //Parameter pre metódu InputLayout
            {
                new DX11.InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32_Float, 0, 0),
                new DX11.InputElement("COLOR", 0, SlimDX.DXGI.Format.R8G8B8A8_UNorm, 12, 0)
            };
            m_vertexLayout = new DX11.InputLayout(device, m_effectPass.Description.Signature, m_inputElements); //Vytvorenie objektu pre opis vstupu pre "Input assembler stage"
        }
예제 #2
0
파일: Program.cs 프로젝트: zhandb/slimdx
        static void Main(string[] args)
        {
            const int elementCount = 16;
            const int bufferSizeInBytes = elementCount * sizeof(float);

            D3D.Device device = new D3D.Device(D3D.DriverType.Hardware, D3D.DeviceCreationFlags.Debug);

            // The input to the computation will be a constant buffer containing
            // integers (in floating point representation) from 1 to numberOfElements,
            // inclusive. The compute shader itself will double these values and write
            // them to the output buffer.
            D3D.BufferDescription inputBufferDescription = new D3D.BufferDescription
            {
                BindFlags = D3D.BindFlags.ConstantBuffer,
                CpuAccessFlags = D3D.CpuAccessFlags.Write,
                OptionFlags = D3D.ResourceOptionFlags.None,
                SizeInBytes = bufferSizeInBytes,
                StructureByteStride = sizeof(float),
                Usage = D3D.ResourceUsage.Dynamic,
            };
            D3D.Buffer inputBuffer = new D3D.Buffer(device, inputBufferDescription);
            DataBox input = device.ImmediateContext.MapSubresource(inputBuffer, 0, bufferSizeInBytes, D3D.MapMode.WriteDiscard, D3D.MapFlags.None);
            for (int value = 1; value <= elementCount; ++value)
                input.Data.Write((float)value);
            device.ImmediateContext.UnmapSubresource(inputBuffer, 0);

            // A staging buffer is used to copy data between the CPU and GPU; the output
            // buffer (which gets the computation results) cannot be mapped directly.
            D3D.BufferDescription stagingBufferDescription = new D3D.BufferDescription
            {
                BindFlags = D3D.BindFlags.None,
                CpuAccessFlags = D3D.CpuAccessFlags.Read,
                OptionFlags = D3D.ResourceOptionFlags.StructuredBuffer,
                SizeInBytes = bufferSizeInBytes,
                StructureByteStride = sizeof(float),
                Usage = D3D.ResourceUsage.Staging,
            };
            D3D.Buffer stagingBuffer = new D3D.Buffer(device, stagingBufferDescription);

            // The output buffer itself, and the view required to bind it to the pipeline.
            D3D.BufferDescription outputBufferDescription = new D3D.BufferDescription
            {
                BindFlags = D3D.BindFlags.UnorderedAccess | D3D.BindFlags.ShaderResource,
                OptionFlags = D3D.ResourceOptionFlags.StructuredBuffer,
                SizeInBytes = bufferSizeInBytes,
                StructureByteStride = sizeof(float),
                Usage = D3D.ResourceUsage.Default,
            };
            D3D.Buffer outputBuffer = new D3D.Buffer(device, outputBufferDescription);
            D3D.UnorderedAccessViewDescription outputViewDescription = new D3D.UnorderedAccessViewDescription
            {
                ElementCount = elementCount,
                Format = DXGI.Format.Unknown,
                Dimension = D3D.UnorderedAccessViewDimension.Buffer
            };
            D3D.UnorderedAccessView outputView = new D3D.UnorderedAccessView(device, outputBuffer, outputViewDescription);

            // Compile the shader.
            ShaderBytecode computeShaderCode = ShaderBytecode.CompileFromFile("BasicComputeShader.hlsl", "main", "cs_4_0", ShaderFlags.None, EffectFlags.None);
            D3D.ComputeShader computeShader = new D3D.ComputeShader(device, computeShaderCode);

            device.ImmediateContext.ComputeShader.Set(computeShader);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(outputView, 0);
            device.ImmediateContext.ComputeShader.SetConstantBuffer(inputBuffer, 0);

            // Compute shaders execute on multiple threads at the same time. Those execution
            // threads are grouped; Dispatch() indicates how many groups in the X, Y and Z
            // dimension will be utilized. The shader itself specified how many threads per
            // group (also in X, Y and Z dimensions) to use via the [numthreads] attribute.
            // In this sample, one thread group will be used with 16 threads, each thread
            // will process one element of the input data.
            device.ImmediateContext.Dispatch(1, 1, 1);

            device.ImmediateContext.CopyResource(outputBuffer, stagingBuffer);
            DataBox output = device.ImmediateContext.MapSubresource(stagingBuffer, 0, sizeof(float) * elementCount, D3D.MapMode.Read, D3D.MapFlags.None);

            Console.Write("Results:");
            for (int index = 0; index < elementCount; ++index)
                Console.Write(" {0}", output.Data.Read<float>());
            device.ImmediateContext.UnmapSubresource(outputBuffer, 0);
            Console.WriteLine();

            computeShader.Dispose();
            outputView.Dispose();
            outputBuffer.Dispose();
            stagingBuffer.Dispose();
            inputBuffer.Dispose();
            device.Dispose();
        }
예제 #3
0
        // Renders a box of the given points
        public void renderBox(Point min, Point max, Color c)
        {
            // Variables
            Vertex[]	vertices=	new Vertex[]	{
                new Vertex(new Point(min.x, max.y, min.z)), // A : 0
                new Vertex(new Point(max.x, max.y, min.z)), // B : 1
                new Vertex(new Point(max.x, min.y, min.z)), // C : 2
                new Vertex(new Point(min.x, min.y, min.z)), // D : 3
                new Vertex(new Point(min.x, max.y, max.z)), // E : 4
                new Vertex(new Point(max.x, max.y, max.z)), // F : 5
                new Vertex(new Point(max.x, min.y, max.z)), // G : 6
                new Vertex(new Point(min.x, min.y, max.z)) // H : 7
            };
            DX3D.BufferDescription	vbd=	new DX3D.BufferDescription(
                Vertex.stride*vertices.Length,
                DX3D.ResourceUsage.Immutable,
                DX3D.BindFlags.VertexBuffer,
                DX3D.CpuAccessFlags.None,
                DX3D.ResourceOptionFlags.None,
                0
            );
            DX3D.BufferDescription	ibd;
            uint[]	indices=	new uint[0];

            vbuffer=	new DX3D.Buffer(
                dxg.device,
                new DX.DataStream(
                    vertices,
                    true,
                    false
                ),
                vbd
            );
            if(renderType== RenderType.FILL)
            {
                indices=	new uint[]	{
                    0, 1, 2, // ABC
                    0, 2, 3, // ACD
                    4, 5, 6, // EFG
                    4, 6, 7, // AGH
                    0, 4, 5, // AEF
                    0, 5, 1, // AFB
                    1, 5, 6, // BFG
                    1, 6, 2, // BGC
                    2, 6, 7, // CGH
                    2, 7, 3, // CHD
                    3, 7, 4, // DHE
                    3, 4, 0 // DEA
                };
            }

            ibd=	new DX3D.BufferDescription(
                sizeof(uint)*indices.Length,
                DX3D.ResourceUsage.Immutable,
                DX3D.BindFlags.IndexBuffer,
                DX3D.CpuAccessFlags.None,
                DX3D.ResourceOptionFlags.None,
                0
            );
            ibuffer=	new DX3D.Buffer(
                dxg.device,
                new DX.DataStream(
                    indices,
                    false,
                    false
                ),
                ibd
            );
            layout=	new DX3D.InputLayout(dxg.device, null, new DX3D.InputLayout[]	{

            });
        }
예제 #4
0
파일: Program.cs 프로젝트: xmcy0011/slimdx
        static void Main(string[] args)
        {
            const int elementCount      = 16;
            const int bufferSizeInBytes = elementCount * sizeof(float);

            D3D.Device device = new D3D.Device(D3D.DriverType.Hardware, D3D.DeviceCreationFlags.Debug);

            // The input to the computation will be a constant buffer containing
            // integers (in floating point representation) from 1 to numberOfElements,
            // inclusive. The compute shader itself will double these values and write
            // them to the output buffer.
            D3D.BufferDescription inputBufferDescription = new D3D.BufferDescription
            {
                BindFlags           = D3D.BindFlags.ConstantBuffer,
                CpuAccessFlags      = D3D.CpuAccessFlags.Write,
                OptionFlags         = D3D.ResourceOptionFlags.None,
                SizeInBytes         = bufferSizeInBytes,
                StructureByteStride = sizeof(float),
                Usage = D3D.ResourceUsage.Dynamic,
            };
            D3D.Buffer inputBuffer = new D3D.Buffer(device, inputBufferDescription);
            DataBox    input       = device.ImmediateContext.MapSubresource(inputBuffer, D3D.MapMode.WriteDiscard, D3D.MapFlags.None);

            for (int value = 1; value <= elementCount; ++value)
            {
                input.Data.Write((float)value);
            }
            device.ImmediateContext.UnmapSubresource(inputBuffer, 0);

            // A staging buffer is used to copy data between the CPU and GPU; the output
            // buffer (which gets the computation results) cannot be mapped directly.
            D3D.BufferDescription stagingBufferDescription = new D3D.BufferDescription
            {
                BindFlags           = D3D.BindFlags.None,
                CpuAccessFlags      = D3D.CpuAccessFlags.Read,
                OptionFlags         = D3D.ResourceOptionFlags.StructuredBuffer,
                SizeInBytes         = bufferSizeInBytes,
                StructureByteStride = sizeof(float),
                Usage = D3D.ResourceUsage.Staging,
            };
            D3D.Buffer stagingBuffer = new D3D.Buffer(device, stagingBufferDescription);

            // The output buffer itself, and the view required to bind it to the pipeline.
            D3D.BufferDescription outputBufferDescription = new D3D.BufferDescription
            {
                BindFlags           = D3D.BindFlags.UnorderedAccess | D3D.BindFlags.ShaderResource,
                OptionFlags         = D3D.ResourceOptionFlags.StructuredBuffer,
                SizeInBytes         = bufferSizeInBytes,
                StructureByteStride = sizeof(float),
                Usage = D3D.ResourceUsage.Default,
            };
            D3D.Buffer outputBuffer = new D3D.Buffer(device, outputBufferDescription);
            D3D.UnorderedAccessViewDescription outputViewDescription = new D3D.UnorderedAccessViewDescription
            {
                ElementCount = elementCount,
                Format       = DXGI.Format.Unknown,
                Dimension    = D3D.UnorderedAccessViewDimension.Buffer
            };
            D3D.UnorderedAccessView outputView = new D3D.UnorderedAccessView(device, outputBuffer, outputViewDescription);

            // Compile the shader.
            ShaderBytecode computeShaderCode = ShaderBytecode.CompileFromFile("BasicComputeShader.hlsl", "main", "cs_4_0", ShaderFlags.None, EffectFlags.None);

            D3D.ComputeShader computeShader = new D3D.ComputeShader(device, computeShaderCode);

            device.ImmediateContext.ComputeShader.Set(computeShader);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(outputView, 0);
            device.ImmediateContext.ComputeShader.SetConstantBuffer(inputBuffer, 0);

            // Compute shaders execute on multiple threads at the same time. Those execution
            // threads are grouped; Dispatch() indicates how many groups in the X, Y and Z
            // dimension will be utilized. The shader itself specified how many threads per
            // group (also in X, Y and Z dimensions) to use via the [numthreads] attribute.
            // In this sample, one thread group will be used with 16 threads, each thread
            // will process one element of the input data.
            device.ImmediateContext.Dispatch(1, 1, 1);

            device.ImmediateContext.CopyResource(outputBuffer, stagingBuffer);
            DataBox output = device.ImmediateContext.MapSubresource(stagingBuffer, D3D.MapMode.Read, D3D.MapFlags.None);

            Console.Write("Results:");
            for (int index = 0; index < elementCount; ++index)
            {
                Console.Write(" {0}", output.Data.Read <float>());
            }
            device.ImmediateContext.UnmapSubresource(outputBuffer, 0);
            Console.WriteLine();

            computeShader.Dispose();
            outputView.Dispose();
            outputBuffer.Dispose();
            stagingBuffer.Dispose();
            inputBuffer.Dispose();
            device.Dispose();
        }
예제 #5
0
        public void Evaluate(int SpreadMax)
        {
            if (this.FBufferIn.IsConnected)
            {
                if (this.RenderRequest != null)
                {
                    this.RenderRequest(this, this.FHost);
                }

                if (this.AssignedContext == null)
                {
                    this.SetNull(); return;
                }

                this.FPointer.SliceCount = SpreadMax;

                DX11RenderContext context = this.AssignedContext;


                try
                {
                    if (this.FBufferIn[0].Contains(context))
                    {
                        var inputBuffer = this.FBufferIn[0][context].Buffer;

                        if (this.currentBuffer != null)
                        {
                            if (this.currentDescription != inputBuffer.Description)
                            {
                                this.currentBuffer.Dispose();
                                this.currentBuffer = null;
                                this.sharedResource.Dispose();
                                this.sharedResource = null;
                            }
                        }

                        //As it can come from dynamic, make sure to allow share put in default pool and deny cpu access
                        if (this.currentBuffer == null)
                        {
                            var desc = inputBuffer.Description;

                            desc.BindFlags      = BindFlags.ShaderResource; //This is important, as resource needs read access later on, it's not only a copy, if not set other side will not be able to create srv
                            desc.OptionFlags   |= ResourceOptionFlags.Shared;
                            desc.Usage          = ResourceUsage.Default;
                            desc.CpuAccessFlags = CpuAccessFlags.None;

                            this.currentBuffer      = new SlimDX.Direct3D11.Buffer(context.Device, desc);
                            this.sharedResource     = new SlimDX.DXGI.Resource(this.currentBuffer);
                            this.currentDescription = inputBuffer.Description;

                            this.FPointer[0] = this.sharedResource.SharedHandle.ToString();
                        }

                        this.AssignedContext.CurrentDeviceContext.CopyResource(inputBuffer, this.currentBuffer);

                        if (this.flush[0])
                        {
                            this.AssignedContext.CurrentDeviceContext.Flush();
                        }
                    }
                    else
                    {
                        this.SetDefault(0);
                    }
                }
                catch
                {
                    this.SetDefault(0);
                }
            }
            else
            {
                this.SetNull();
            }
        }