Beispiel #1
0
        void GenerateIfRequired()
        {
            if (_buffer != null)
            {
                return;
            }

            // TODO: To use Immutable resources we would need to delay creation of
            // the Buffer until SetData() and recreate them if set more than once.

            var accessflags = SharpDX.Direct3D11.CpuAccessFlags.None;
            var usage       = SharpDX.Direct3D11.ResourceUsage.Default;

            if (_isDynamic)
            {
                accessflags |= SharpDX.Direct3D11.CpuAccessFlags.Write;
                usage        = SharpDX.Direct3D11.ResourceUsage.Dynamic;
            }

            _buffer = new SharpDX.Direct3D11.Buffer(GraphicsDevice._d3dDevice,
                                                    VertexDeclaration.VertexStride * VertexCount,
                                                    usage,
                                                    SharpDX.Direct3D11.BindFlags.VertexBuffer,
                                                    accessflags,
                                                    SharpDX.Direct3D11.ResourceOptionFlags.None,
                                                    0      // StructureSizeInBytes
                                                    );

            _binding = new SharpDX.Direct3D11.VertexBufferBinding(_buffer, VertexDeclaration.VertexStride, 0);
        }
        /// <summary>
        /// Renders one frame using the vertex and pixel shaders.
        /// On devices that do not support the D3D11_FEATURE_D3D11_OPTIONS3::
        /// VPAndRTArrayIndexFromAnyShaderFeedingRasterizer optional feature,
        /// a pass-through geometry shader is also used to set the render
        /// target array index.
        /// </summary>
        public void Render()
        {
            // Loading is asynchronous. Resources must be created before drawing can occur.
            if (!this.loadingComplete)
            {
                return;
            }

            var context = this.deviceResources.D3DDeviceContext;

            // Each vertex is one instance of the VertexPositionColor struct.
            int stride        = SharpDX.Utilities.SizeOf <VertexPositionTexture>();
            int offset        = 0;
            var bufferBinding = new SharpDX.Direct3D11.VertexBufferBinding(this.vertexBuffer, stride, offset);

            context.InputAssembler.SetVertexBuffers(0, bufferBinding);
            context.InputAssembler.SetIndexBuffer(
                this.indexBuffer,
                SharpDX.DXGI.Format.R32_UInt, // Each index is one 16-bit unsigned integer (short).
                0);
            context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            context.InputAssembler.InputLayout       = this.inputLayout;

            // Attach the vertex shader.
            context.VertexShader.SetShader(this.vertexShader, null, 0);
            // Apply the model constant buffer to the vertex shader.
            context.VertexShader.SetConstantBuffers(0, this.modelConstantBuffer);

            if (!this.usingVprtShaders)
            {
                // On devices that do not support the D3D11_FEATURE_D3D11_OPTIONS3::
                // VPAndRTArrayIndexFromAnyShaderFeedingRasterizer optional feature,
                // a pass-through geometry shader is used to set the render target
                // array index.
                context.GeometryShader.SetShader(this.geometryShader, null, 0);
            }

            // Attach the pixel shader.
            context.PixelShader.SetShader(this.pixelShader, null, 0);

            lock (thisLock)
            {
                context.PixelShader.SetShaderResources(0, 1, m_textureView);

                context.PixelShader.SetSamplers(0, 1, m_quadTextureSamplerState);

                // Draw the objects.
                context.DrawIndexedInstanced(
                    indexCount,     // Index count per instance.
                    2,              // Instance count.
                    0,              // Start index location.
                    0,              // Base vertex location.
                    0               // Start instance location.
                    );
            }
        }
        public void Render()
        {
            if (!loadingFinished || !physicalCamera.Ready)
            {
                return;
            }

            var device        = deviceResources.D3DDevice;
            var context       = deviceResources.D3DDeviceContext;
            int stride        = SharpDX.Utilities.SizeOf <VertexPositionUV>();
            int offset        = 0;
            var bufferBinding = new SharpDX.Direct3D11.VertexBufferBinding(vertexBuffer, stride, offset);

            context.InputAssembler.SetVertexBuffers(0, bufferBinding);
            context.InputAssembler.SetIndexBuffer(indexBuffer, SharpDX.DXGI.Format.R16_UInt, 0);
            context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            context.InputAssembler.InputLayout       = inputLayout;

            context.VertexShader.SetShader(vertexShader, null, 0);
            context.VertexShader.SetConstantBuffers(0, modelConstantBuffer);
            context.PixelShader.SetShader(pixelShader, null, 0);
            var cameraTexture = physicalCamera.AcquireTexture();

            if (cameraTexture == null)
            {
                return;
            }
            var luminanceView = new SharpDX.Direct3D11.ShaderResourceView(device, cameraTexture, new SharpDX.Direct3D11.ShaderResourceViewDescription()
            {
                Format    = SharpDX.DXGI.Format.R8_UInt,
                Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource()
                {
                    MipLevels = 1
                }
            });
            var chrominanceView = new SharpDX.Direct3D11.ShaderResourceView(device, cameraTexture, new SharpDX.Direct3D11.ShaderResourceViewDescription()
            {
                Format    = SharpDX.DXGI.Format.R8G8_UInt,
                Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource()
                {
                    MipLevels = 1
                }
            });

            context.PixelShader.SetShaderResource(0, luminanceView);
            context.PixelShader.SetShaderResource(1, chrominanceView);

            context.DrawIndexedInstanced(6, 2, 0, 0, 0);

            luminanceView.Dispose();
            chrominanceView.Dispose();
            physicalCamera.ReleaseTexture();
        }
Beispiel #4
0
        /// <summary>
        /// The GraphicsDevice is resetting, so GPU resources must be recreated.
        /// </summary>
        internal protected override void GraphicsDeviceResetting()
        {
#if OPENGL
            vbo = 0;
#endif

#if DIRECTX
            _binding = new SharpDX.Direct3D11.VertexBufferBinding();
            SharpDX.Utilities.Dispose(ref _buffer);
#endif
        }
Beispiel #5
0
        protected VertexBuffer(GraphicsDevice graphicsDevice, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage bufferUsage, bool dynamic)
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("Graphics Device Cannot Be null");
            }

            this.GraphicsDevice    = graphicsDevice;
            this.VertexDeclaration = vertexDeclaration;
            this.VertexCount       = vertexCount;
            this.BufferUsage       = bufferUsage;

            // Make sure the graphics device is assigned in the vertex declaration.
            if (vertexDeclaration.GraphicsDevice != graphicsDevice)
            {
                vertexDeclaration.GraphicsDevice = graphicsDevice;
            }

            _isDynamic = dynamic;

#if DIRECTX
            // TODO: To use Immutable resources we would need to delay creation of
            // the Buffer until SetData() and recreate them if set more than once.

            var accessflags = SharpDX.Direct3D11.CpuAccessFlags.None;
            var usage       = SharpDX.Direct3D11.ResourceUsage.Default;

            if (dynamic)
            {
                accessflags |= SharpDX.Direct3D11.CpuAccessFlags.Write;
                usage        = SharpDX.Direct3D11.ResourceUsage.Dynamic;
            }

            _buffer = new SharpDX.Direct3D11.Buffer(graphicsDevice._d3dDevice,
                                                    vertexDeclaration.VertexStride * vertexCount,
                                                    usage,
                                                    SharpDX.Direct3D11.BindFlags.VertexBuffer,
                                                    accessflags,
                                                    SharpDX.Direct3D11.ResourceOptionFlags.None,
                                                    0      // StructureSizeInBytes
                                                    );

            _binding = new SharpDX.Direct3D11.VertexBufferBinding(_buffer, VertexDeclaration.VertexStride, 0);
#elif PSM
            //Do nothing, we cannot create the storage array yet
#else
            Threading.BlockOnUIThread(GenerateIfRequired);
#endif
        }
Beispiel #6
0
        public void SetVertexBuffer(GpuBuffer buffer)
        {
            //set vertex buffer to input stage

            //test if the buffer can be set
            Debug.Assert(GpuConvert.HasBindUsage(buffer.ResourceInfo.BindUsage, GpuBindUsage.VertexBufferr) == true);

            //create buffer binding
            var bufferBinding = new SharpDX.Direct3D11.VertexBufferBinding(
                buffer.Resource as SharpDX.Direct3D11.Buffer,
                buffer.ElementSize, 0);

            //set vertex buffer
            ImmediateContext.InputAssembler.SetVertexBuffers(0, bufferBinding);
        }
        /// <summary>
        /// Renders one frame using the vertex and pixel shaders.
        /// On devices that do not support the D3D11_FEATURE_D3D11_OPTIONS3::
        /// VPAndRTArrayIndexFromAnyShaderFeedingRasterizer optional feature,
        /// a pass-through geometry shader is also used to set the render
        /// target array index.
        /// </summary>
        public void Render()
        {
            // Loading is asynchronous. Resources must be created before drawing can occur.
            if (!loadingComplete)
            {
                return;
            }

            var context = deviceResources.D3DDeviceContext;

            // Each vertex is one instance of the VertexPositionColor struct.
            int stride        = SharpDX.Utilities.SizeOf <VertexPositionColor>();
            int offset        = 0;
            var bufferBinding = new SharpDX.Direct3D11.VertexBufferBinding(vertexBuffer, stride, offset);

            context.InputAssembler.SetVertexBuffers(0, bufferBinding);
            context.InputAssembler.SetIndexBuffer(
                indexBuffer,
                SharpDX.DXGI.Format.R16_UInt, // Each index is one 16-bit unsigned integer (short).
                0);
            context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            context.InputAssembler.InputLayout       = inputLayout;

            // Attach the vertex shader.
            context.VertexShader.SetShader(vertexShader, null, 0);
            // Apply the model constant buffer to the vertex shader.
            context.VertexShader.SetConstantBuffers(0, modelConstantBuffer);

            // Attach the pixel shader.
            context.PixelShader.SetShader(pixelShader, null, 0);

            // Draw the objects.
            context.DrawIndexedInstanced(
                indexCount,     // Index count per instance.
                2,              // Instance count.
                0,              // Start index location.
                0,              // Base vertex location.
                0               // Start instance location.
                );
        }
Beispiel #8
0
        protected VertexBuffer(GraphicsDevice graphicsDevice, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage bufferUsage, bool dynamic)
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("Graphics Device Cannot Be null");
            }

            this.graphicsDevice    = graphicsDevice;
            this.VertexDeclaration = vertexDeclaration;
            this.VertexCount       = vertexCount;
            this.BufferUsage       = bufferUsage;

            // Make sure the graphics device is assigned in the vertex declaration.
            if (vertexDeclaration.GraphicsDevice != graphicsDevice)
            {
                vertexDeclaration.GraphicsDevice = graphicsDevice;
            }

            _isDynamic = dynamic;

#if DIRECTX
            // TODO: To use Immutable resources we would need to delay creation of
            // the Buffer until SetData() and recreate them if set more than once.

            var accessflags = SharpDX.Direct3D11.CpuAccessFlags.None;
            var usage       = SharpDX.Direct3D11.ResourceUsage.Default;

            if (dynamic)
            {
                accessflags |= SharpDX.Direct3D11.CpuAccessFlags.Write;
                usage        = SharpDX.Direct3D11.ResourceUsage.Dynamic;
            }

            _buffer = new SharpDX.Direct3D11.Buffer(graphicsDevice._d3dDevice,
                                                    vertexDeclaration.VertexStride * vertexCount,
                                                    usage,
                                                    SharpDX.Direct3D11.BindFlags.VertexBuffer,
                                                    accessflags,
                                                    SharpDX.Direct3D11.ResourceOptionFlags.None,
                                                    0      // StructureSizeInBytes
                                                    );

            _binding = new SharpDX.Direct3D11.VertexBufferBinding(_buffer, VertexDeclaration.VertexStride, 0);
#elif PSS
            //Do nothing, we cannot create the storage array yet
#else
            Threading.BlockOnUIThread(() =>
            {
                //GLExt.Oes.GenVertexArrays(1, out this.vao);
                //GLExt.Oes.BindVertexArray(this.vao);
#if IPHONE || ANDROID
                GL.GenBuffers(1, ref this.vbo);
#else
                GL.GenBuffers(1, out this.vbo);
#endif
                GraphicsExtensions.CheckGLError();
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.vbo);
                GraphicsExtensions.CheckGLError();
                GL.BufferData(BufferTarget.ArrayBuffer,
                              new IntPtr(vertexDeclaration.VertexStride * vertexCount), IntPtr.Zero,
                              dynamic ? BufferUsageHint.StreamDraw : BufferUsageHint.StaticDraw);
                GraphicsExtensions.CheckGLError();
            });
#endif
        }
        public void MakeSphere(SharpDX.Direct3D11.Device d3dDevice)
        {
            // Layout from VertexShader input signature


            texture = Texture11.FromFile(d3dDevice, "earthMap.jpg");

            uint[] indexes = new uint[subDivisionsX * subDivisionsY * 6];
            float[] verts = new float[((subDivisionsX + 1) * (subDivisionsY + 1)) * 6];


            double lat, lng;

            uint index = 0;
            double latMin = 90;
            double latMax = -90;
            double lngMin = -180;
            double lngMax = 180;


            uint x1, y1;

            double latDegrees = latMax - latMin;
            double lngDegrees = lngMax - lngMin;

            double textureStepX = 1.0f / subDivisionsX;
            double textureStepY = 1.0f / subDivisionsY;
            for (y1 = 0; y1 <= subDivisionsY; y1++)
            {

                if (y1 != subDivisionsY)
                {
                    lat = latMax - (textureStepY * latDegrees * (double)y1);
                }
                else
                {
                    lat = latMin;
                }

                for (x1 = 0; x1 <= subDivisionsX; x1++)
                {
                    if (x1 != subDivisionsX)
                    {
                        lng = lngMin + (textureStepX * lngDegrees * (double)x1);
                    }
                    else
                    {
                        lng = lngMax;
                    }
                    index = (y1 * (subDivisionsX + 1) + x1) * 6;

                    GeoTo3d(verts, (int)index, lat, lng);
                }
            }

            triangleCount = (subDivisionsX) * (subDivisionsY) * 2;

            for (y1 = 0; y1 < subDivisionsY; y1++)
            {
                for (x1 = 0; x1 < subDivisionsX; x1++)
                {
                    index = (y1 * subDivisionsX * 6) + 6 * x1;
                    // First triangle in quad
                    indexes[index] = (y1 * (subDivisionsX + 1) + x1);
                    indexes[index + 1] = ((y1 + 1) * (subDivisionsX + 1) + x1);
                    indexes[index + 2] = (y1 * (subDivisionsX + 1) + (x1 + 1));

                    // Second triangle in quad
                    indexes[index + 3] = (y1 * (subDivisionsX + 1) + (x1 + 1));
                    indexes[index + 4] = ((y1 + 1) * (subDivisionsX + 1) + x1);
                    indexes[index + 5] = ((y1 + 1) * (subDivisionsX + 1) + (x1 + 1));
                }
            }

            vertices = SharpDX.Direct3D11.Buffer.Create(d3dDevice, SharpDX.Direct3D11.BindFlags.VertexBuffer, verts);

            vertexBufferBinding = new SharpDX.Direct3D11.VertexBufferBinding(vertices, sizeof(float) * 6, 0);

            indexBuffer = SharpDX.Direct3D11.Buffer.Create(d3dDevice, SharpDX.Direct3D11.BindFlags.IndexBuffer, indexes);


        }
 private void PlatformGraphicsDeviceResetting()
 {
     _binding = new SharpDX.Direct3D11.VertexBufferBinding();
     SharpDX.Utilities.Dispose(ref _buffer);
 }