Exemple #1
0
        /// <summary>
        /// Creates a new instance of <see cref="D3D10EffectImplementation"/>.
        /// </summary>
        /// <param name="renderer">The D3D10 renderer.</param>
        /// <param name="shaderByteCode">The compiled shader byte code.</param>
        /// <param name="profileName">Name of the shader profile.</param>
        /// <param name="sFlags">The shader flags.</param>
        /// <param name="eFlags">The effect flags.</param>
        internal D3D10EffectImplementation(D3D10Renderer renderer, byte[] shaderByteCode, String profileName, D3DC.ShaderFlags sFlags, D3DC.EffectFlags eFlags)
        {
            _cachedByteCode = shaderByteCode;
            _renderer       = renderer;
            _graphicsDevice = _renderer.GraphicsDevice;
            _profileName    = profileName;
            _sFlags         = sFlags;
            _eFlags         = eFlags;
            _effect         = D3D.Effect.FromMemory(_graphicsDevice, _cachedByteCode, _profileName, _sFlags, _eFlags, null, null, null);

            //Add to resource tracker
            _renderer.Resources.AddTrackedObject(_effect.ComPointer, this);

            int techCount = _effect.Description.TechniqueCount;
            D3D10EffectTechniqueCollection techniques = new D3D10EffectTechniqueCollection();
            D3D10EffectParameterCollection parameters = new D3D10EffectParameterCollection();

            base.SetTechniques(techniques);
            base.SetParameters(parameters);

            for (int i = 0; i < techCount; i++)
            {
                D3D10EffectTechnique tech = new D3D10EffectTechnique(this, _effect, _effect.GetTechniqueByIndex(i));
                ((D3D10EffectTechniqueCollection)techniques).Add(tech);
            }

            //Set the current technique, the first one by default
            _currentTechnique = (D3D10EffectTechnique)techniques[0];
        }
Exemple #2
0
        /// <summary>
        /// Creates a new instance of <see cref="D3D10IndexBufferImplementation"/>.
        /// </summary>
        /// <param name="renderer">The D3D renderer.</param>
        /// <param name="format">The index format.</param>
        /// <param name="indexCount">The number of indices.</param>
        /// <param name="usage">The buffer usage specifying what type of memory the buffer should be created in.</param>
        internal D3D10IndexBufferImplementation(D3D10Renderer renderer, IndexFormat format, int indexCount, ResourceUsage usage)
            : base(format, indexCount, usage)
        {
            _renderer       = renderer;
            _graphicsDevice = _renderer.GraphicsDevice;

            int formatBytes = (format == IndexFormat.ThirtyTwoBits) ? 4 : 2;

            try {
                //Determine appropiate vertex buffer to create
                if (usage == ResourceUsage.Static)
                {
                    D3D.ResourceUsage  rUsage    = D3D.ResourceUsage.Default;
                    D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.None;
                    _buffer = new D3D.Buffer(_graphicsDevice, new D3D.BufferDescription(indexCount * formatBytes, rUsage, D3D.BindFlags.IndexBuffer, cpuAccess, D3D.ResourceOptionFlags.None));
                }
                else if (usage == ResourceUsage.Dynamic)
                {
                    D3D.ResourceUsage  rUsage    = D3D.ResourceUsage.Dynamic;
                    D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.Write;
                    _buffer = new D3D.Buffer(_graphicsDevice, new D3D.BufferDescription(indexCount * formatBytes, rUsage, D3D.BindFlags.IndexBuffer, cpuAccess, D3D.ResourceOptionFlags.None));
                }

                //Add to tracker
                _renderer.Resources.AddTrackedObject(_buffer.ComPointer, this);
            } catch (Exception e) {
                Dispose();
                throw new TeslaException("Error creating DX10 buffer:\n" + e.Message);
            }
        }
        /// <summary>
        /// Creates a new instance of <see cref="D3D10VertexBufferImplementation"/>.
        /// </summary>
        /// <param name="renderer">The D3D10 renderer.</param>
        /// <param name="decl">The vertex declaration.</param>
        /// <param name="vertexCount">The vertex count.</param>
        /// <param name="usage">The resource usage.</param>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying D3D10 buffer failed.</exception>
        internal D3D10VertexBufferImplementation(D3D10Renderer renderer, VertexDeclaration decl, int vertexCount, ResourceUsage usage)
            : base(decl, vertexCount, usage)
        {
            _renderer       = renderer;
            _graphicsDevice = _renderer.GraphicsDevice;

            try {
                //Determine appropiate vertex buffer to create, let any exceptions bubble up.
                if (base.BufferUsage == ResourceUsage.Static)
                {
                    D3D.ResourceUsage  rUsage    = D3D.ResourceUsage.Default;
                    D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.None;
                    _buffer = new D3D.Buffer(_graphicsDevice, new D3D.BufferDescription(vertexCount * decl.VertexStride, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None));
                }
                else if (base.BufferUsage == ResourceUsage.Dynamic)
                {
                    D3D.ResourceUsage  rUsage    = D3D.ResourceUsage.Dynamic;
                    D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.Write;
                    _buffer = new D3D.Buffer(_graphicsDevice, new D3D.BufferDescription(vertexCount * decl.VertexStride, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None));
                }

                //Add to tracker
                _renderer.Resources.AddTrackedObject(_buffer.ComPointer, this);
            } catch (Exception e) {
                Dispose();
                throw new TeslaException("Error creating D3D10 buffer: \n" + e.Message, e);
            }
        }
        /// <summary>
        /// Creates a new instance of <see cref="D3D10VertexBufferImplementation"/> initialized with the interleaved data.
        /// </summary>
        /// <param name="renderer">The D3D10 renderer.</param>
        /// <param name="decl">The vertex declaration.</param>
        /// <param name="usage">The resource usage.</param>
        /// <param name="data">The interleaved data.</param>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying D3D10 buffer or writing to it failed.</exception>
        internal D3D10VertexBufferImplementation(D3D10Renderer renderer, VertexDeclaration decl, ResourceUsage usage, DataBuffer data)
            : base(decl, usage, data)
        {
            _renderer       = renderer;
            _graphicsDevice = _renderer.GraphicsDevice;

            //Determine appropiate buffer, let any exceptions bubble up.
            try {
                using (SDX.DataStream ds = new SDX.DataStream(data.ByteDataCopy, true, false)) {
                    //Now create and populate appropiate D3D10 buffer
                    if (base.BufferUsage == ResourceUsage.Static)
                    {
                        D3D.ResourceUsage  rUsage    = D3D.ResourceUsage.Default;
                        D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.None;
                        _buffer = new D3D.Buffer(_graphicsDevice, ds, new D3D.BufferDescription(data.SizeInBytes, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None));
                    }
                    else if (base.BufferUsage == ResourceUsage.Dynamic)
                    {
                        D3D.ResourceUsage  rUsage    = D3D.ResourceUsage.Dynamic;
                        D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.Write;
                        _buffer = new D3D.Buffer(_graphicsDevice, ds, new D3D.BufferDescription(data.SizeInBytes, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None));
                    }
                }

                //Add to tracker
                _renderer.Resources.AddTrackedObject(_buffer.ComPointer, this);
            } catch (Exception e) {
                Dispose();
                throw new TeslaException("Error creating D3D10 buffer: \n" + e.Message, e);
            }
        }
 /// <summary>
 /// Creates a new instance of <see cref="D3D10SwapChainImplementation"/>.
 /// </summary>
 /// <param name="renderer">The D3D10 renderer.</param>
 /// <param name="factory">The DXGI factory.</param>
 /// <param name="windowHandle">The window handle.</param>
 /// <param name="presentParams">The presentation parameters to initialize to.</param>
 internal D3D10SwapChainImplementation(D3D10Renderer renderer, DXGI.Factory factory, IntPtr windowHandle, PresentationParameters presentParams)
 {
     _renderer       = renderer;
     _graphicsDevice = _renderer.GraphicsDevice;
     _factory        = factory;
     _resetting      = false;
     _isFullScreen   = false;
     _presentParams  = presentParams;
     _windowHandle   = windowHandle;
     CreateSwapChain(_windowHandle, _presentParams);
     CreateViews();
 }
        /// <summary>
        /// Creates a new instance of <see cref="D3D10VertexBufferImplementation"/> initialized with the vertex data array.
        /// </summary>
        /// <param name="renderer">The D3D10 renderer.</param>
        /// <param name="decl">The vertex declaration.</param>
        /// <param name="usage">The resource usage.</param>
        /// <param name="data">The array of vertex data.</param>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying D3D10 buffer or writing to it failed.</exception>
        internal D3D10VertexBufferImplementation(D3D10Renderer renderer, VertexDeclaration decl, ResourceUsage usage, params DataBuffer[] data)
            : base(decl, usage, data)
        {
            _renderer       = renderer;
            _graphicsDevice = renderer.GraphicsDevice;

            int totalSizeInBytes = base.VertexCount * decl.VertexStride;
            int vertexStride     = decl.VertexStride;

            try {
                //Now initialize the buffer with the supplied vertex store
                using (SDX.DataStream interleaved = new SDX.DataStream(totalSizeInBytes, true, true)) {
                    //Create the interleaved buffer
                    byte[] vertex = new byte[vertexStride];
                    for (int i = 0; i < base.VertexCount; i++)
                    {
                        int offset = 0;
                        for (int j = 0; j < data.Length; j++)
                        {
                            DataBuffer db          = data[j];
                            int        elementSize = db.ElementSizeInBytes;
                            db.Get(vertex, offset, elementSize);
                            offset += elementSize;
                        }
                        interleaved.Write(vertex, 0, vertexStride);
                    }
                    interleaved.Position = 0;
                    //Now create and populate appropiate D3D10 buffer
                    if (base.BufferUsage == ResourceUsage.Static)
                    {
                        D3D.ResourceUsage  rUsage    = D3D.ResourceUsage.Default;
                        D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.None;
                        _buffer = new D3D.Buffer(_graphicsDevice, interleaved, new D3D.BufferDescription(totalSizeInBytes, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None));
                    }
                    else if (base.BufferUsage == ResourceUsage.Dynamic)
                    {
                        D3D.ResourceUsage  rUsage    = D3D.ResourceUsage.Dynamic;
                        D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.Write;
                        _buffer = new D3D.Buffer(_graphicsDevice, interleaved, new D3D.BufferDescription(totalSizeInBytes, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None));
                    }
                }

                //Add to tracker
                _renderer.Resources.AddTrackedObject(_buffer.ComPointer, this);
            } catch (Exception e) {
                Dispose();
                throw new TeslaException("Error creating D3D10 buffer: \n" + e.Message, e);
            }
        }
Exemple #7
0
        /// <summary>
        /// Creates a new instance of <see cref="D3D10EffectImplementation"/>. Used for cloning.
        /// </summary>
        /// <param name="cloneSource">The implementation source to clone from</param>
        /// <param name="profileName">Name of the shader profile.</param>
        /// <param name="sFlags">The shader flags.</param>
        /// <param name="eFlags">The effect flags.</param>
        internal D3D10EffectImplementation(D3D10EffectImplementation cloneSource, String profileName, D3DC.ShaderFlags sFlags, D3DC.EffectFlags eFlags)
        {
            _cachedByteCode = cloneSource._cachedByteCode;
            _renderer       = cloneSource._renderer;
            _graphicsDevice = cloneSource._graphicsDevice;
            _profileName    = profileName;
            _sFlags         = sFlags;
            _eFlags         = eFlags;
            _effect         = D3D.Effect.FromMemory(_graphicsDevice, _cachedByteCode, _profileName, _sFlags, _eFlags, null, null, null);

            //Add to resource tracker
            _renderer.Resources.AddTrackedObject(_effect.ComPointer, this);

            //Clone techniques and parameters
            D3D10EffectTechniqueCollection techniques = cloneSource.Techniques as D3D10EffectTechniqueCollection;
            D3D10EffectParameterCollection parameters = cloneSource.Parameters as D3D10EffectParameterCollection;

            base.SetTechniques(techniques.Clone(_effect, this));
            base.SetParameters(parameters.Clone(_effect));
            _currentTechnique = (D3D10EffectTechnique)base.Techniques[0];
        }
Exemple #8
0
        /// <summary>
        /// Creates a new instance of <see cref="D3D10IndexBufferImplementation"/>.
        /// </summary>
        /// <param name="renderer">The D3D renderer.</param>
        /// <param name="indices">The index data</param>
        /// <param name="usage">The buffer usage specifying what type of memory the buffer should be created in.</param>
        internal D3D10IndexBufferImplementation(D3D10Renderer renderer, DataBuffer <int> indices, ResourceUsage usage)
            : base(indices, usage)
        {
            if (indices == null)
            {
                throw new ArgumentNullException("indices", "Indices cannot be null.");
            }

            _renderer       = renderer;
            _graphicsDevice = _renderer.GraphicsDevice;

            try {
                //Determine appropiate vertex buffer to create
                if (usage == ResourceUsage.Static)
                {
                    D3D.ResourceUsage  rUsage    = D3D.ResourceUsage.Default;
                    D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.None;
                    using (SDX.DataStream ds = new SDX.DataStream(indices.Buffer, true, true)) {
                        _buffer = new D3D.Buffer(_graphicsDevice, ds, new D3D.BufferDescription(indices.SizeInBytes, rUsage, D3D.BindFlags.IndexBuffer, cpuAccess, D3D.ResourceOptionFlags.None));
                    }
                }
                else if (usage == ResourceUsage.Dynamic)
                {
                    D3D.ResourceUsage  rUsage    = D3D.ResourceUsage.Dynamic;
                    D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.Write;
                    using (SDX.DataStream ds = new SDX.DataStream(indices.Buffer, true, true)) {
                        _buffer = new D3D.Buffer(_graphicsDevice, ds, new D3D.BufferDescription(indices.SizeInBytes, rUsage, D3D.BindFlags.IndexBuffer, cpuAccess, D3D.ResourceOptionFlags.None));
                    }
                }

                //Add to tracker
                _renderer.Resources.AddTrackedObject(_buffer.ComPointer, this);
            } catch (D3D.Direct3D10Exception e) {
                Dispose();
                throw new TeslaException("Error creating D3D10 buffer.", e);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="D3D10BlendStateImplementation"/> class.
 /// </summary>
 /// <param name="renderer">The renderer.</param>
 internal D3D10BlendStateImplementation(D3D10Renderer renderer) :
     base()
 {
     _renderer       = renderer;
     _graphicsDevice = _renderer.GraphicsDevice;
 }
Exemple #10
0
        /// <summary>
        /// Creates a new instance of <see cref="D3D10Texture2DImplementation"/>, for RenderTarget2D.
        /// </summary>
        /// <param name="renderer">The D3D10 renderer.</param>
        /// <param name="width">The texture width in pixels.</param>
        /// <param name="height">The texture height in pixels.</param>
        /// <param name="genMipMaps">True if a mipmap chain should be generated or not.</param>
        /// <param name="format">The surface format.</param>
        /// <param name="depthFormat">The depth-stencil format.</param>
        /// <param name="multiSampleCount">The multisample count.</param>
        /// <param name="usage">The target usage.</param>
        /// <exception cref="System.ArgumentException">Thrown if the formats are invalid to be used in a render target.</exception>
        internal D3D10Texture2DImplementation(D3D10Renderer renderer, int width, int height, bool genMipMaps, SurfaceFormat format, DepthFormat depthFormat, int multiSampleCount, RenderTargetUsage usage)
            : base(width, height, format, depthFormat, multiSampleCount, usage)
        {
            _renderer       = renderer;
            _graphicsDevice = _renderer.GraphicsDevice;

            if (!renderer.Adapter.QueryRenderTargetFormat(format, depthFormat, multiSampleCount))
            {
                throw new ArgumentException("Format combination not supported.");
            }

            D3D.Texture2DDescription desc2D = new D3D.Texture2DDescription();
            desc2D.ArraySize      = 1;
            desc2D.BindFlags      = D3D.BindFlags.ShaderResource | D3D.BindFlags.RenderTarget;
            desc2D.CpuAccessFlags = D3D.CpuAccessFlags.None;
            desc2D.Format         = D3D10Helper.ToD3DSurfaceFormat(format);
            desc2D.Height         = height;
            desc2D.Width          = width;
            desc2D.Usage          = _usage = D3D.ResourceUsage.Default;
            if (genMipMaps)
            {
                desc2D.OptionFlags = D3D.ResourceOptionFlags.GenerateMipMaps;
            }
            else
            {
                desc2D.OptionFlags = D3D.ResourceOptionFlags.None;
            }
            desc2D.MipLevels = (genMipMaps) ? 0 : 1;
            if (multiSampleCount > 1)
            {
                desc2D.SampleDescription = new SlimDX.DXGI.SampleDescription(multiSampleCount, 0);
            }
            else
            {
                desc2D.SampleDescription = new DXGI.SampleDescription(1, 0);
            }

            _texture2D          = new D3D.Texture2D(_graphicsDevice, desc2D);
            _shaderResourceView = new D3D.ShaderResourceView(_graphicsDevice, _texture2D);
            _renderTargetView   = new D3D.RenderTargetView(_graphicsDevice, _texture2D);
            _graphicsDevice.ClearRenderTargetView(_renderTargetView, new SDX.Color4(1.0f, 0, 0, 0));

            //Add to tracker
            _renderer.Resources.AddTrackedObject(_texture2D.ComPointer, this);
            _renderer.Resources.AddTrackedObject(_shaderResourceView.ComPointer, this);
            _renderer.Resources.AddTrackedObject(_renderTargetView.ComPointer, this);

            _mipCount = _texture2D.Description.MipLevels;

            if (depthFormat != DepthFormat.None)
            {
                D3D.Texture2DDescription dbdesc = new D3D.Texture2DDescription();
                dbdesc.ArraySize      = 1;
                dbdesc.BindFlags      = D3D.BindFlags.DepthStencil;
                dbdesc.CpuAccessFlags = D3D.CpuAccessFlags.None;
                dbdesc.Format         = D3D10Helper.ToD3DDepthFormat(depthFormat);
                dbdesc.Height         = height;
                dbdesc.Width          = width;
                dbdesc.Usage          = D3D.ResourceUsage.Default;
                dbdesc.OptionFlags    = D3D.ResourceOptionFlags.None;
                dbdesc.MipLevels      = 1;
                if (multiSampleCount > 1)
                {
                    dbdesc.SampleDescription = new SlimDX.DXGI.SampleDescription(multiSampleCount, 0);
                }
                else
                {
                    dbdesc.SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0);
                }

                using (D3D.Texture2D depthBuffer = new D3D.Texture2D(_graphicsDevice, dbdesc)) {
                    _depthStencilView = new D3D.DepthStencilView(_graphicsDevice, depthBuffer);
                    if (depthFormat == Tesla.Graphics.DepthFormat.Depth24Stencil8)
                    {
                        _graphicsDevice.ClearDepthStencilView(_depthStencilView, D3D.DepthStencilClearFlags.Depth | D3D.DepthStencilClearFlags.Stencil, 1.0f, 0);
                    }
                    else
                    {
                        _graphicsDevice.ClearDepthStencilView(_depthStencilView, D3D.DepthStencilClearFlags.Depth, 1.0f, 0);
                    }

                    //Add to tracker
                    _renderer.Resources.AddTrackedObject(_depthStencilView.ComPointer, this);
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// Creates a new instance of <see cref="D3D10Texture2DImplementation"/>, for Texture2D.
        /// </summary>
        /// <param name="renderer">The D3D10 renderer.</param>
        /// <param name="width">The texture width in pixels.</param>
        /// <param name="height">The texture height in pixels.</param>
        /// <param name="genMipMaps">True if a mipmap chain should be generated or not.</param>
        /// <param name="format">The surface format.</param>
        /// <param name="data">The data to initialize the first mip map level.</param>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if there was an error writing to the texture.</exception>
        internal D3D10Texture2DImplementation(D3D10Renderer renderer, int width, int height, bool genMipMaps, SurfaceFormat format, DataBuffer data)
            : base(width, height, format)
        {
            //Set common properties
            _renderer       = renderer;
            _graphicsDevice = _renderer.GraphicsDevice;

            //Do we want to generate mip maps, and do we have the data to do so? (if not, cancel mip map generation)
            bool canGenMipmaps = (genMipMaps && (data != null));

            //Setup texture description
            D3D.Texture2DDescription descTex = new D3D.Texture2DDescription();
            descTex.ArraySize         = 1;
            descTex.Width             = width;
            descTex.Height            = height;
            descTex.Usage             = _usage = D3D.ResourceUsage.Default;
            descTex.CpuAccessFlags    = D3D.CpuAccessFlags.None;
            descTex.Format            = D3D10Helper.ToD3DSurfaceFormat(format);
            descTex.SampleDescription = new DXGI.SampleDescription(1, 0); //Default sample desc
            descTex.MipLevels         = (genMipMaps) ? 0 : 1;

            //Set mip map generation params
            if (canGenMipmaps)
            {
                descTex.BindFlags   = D3D.BindFlags.ShaderResource | D3D.BindFlags.RenderTarget;
                descTex.OptionFlags = D3D.ResourceOptionFlags.GenerateMipMaps;
            }
            else
            {
                descTex.BindFlags   = D3D.BindFlags.ShaderResource;
                descTex.OptionFlags = D3D.ResourceOptionFlags.None;
            }

            //Create the texture and shader view
            _texture2D          = new D3D.Texture2D(_graphicsDevice, descTex);
            _shaderResourceView = new D3D.ShaderResourceView(_graphicsDevice, _texture2D);

            //Add to tracker
            _renderer.Resources.AddTrackedObject(_texture2D.ComPointer, this);
            _renderer.Resources.AddTrackedObject(_shaderResourceView.ComPointer, this);

            //Set the final mip count
            _mipCount = _texture2D.Description.MipLevels;

            //Now set the initial data if its present
            if (data != null)
            {
                try {
                    this.SetData <byte>(data.ByteDataCopy, 0, null, 0, data.ElementSizeInBytes * data.Length);

                    if (genMipMaps)
                    {
                        _graphicsDevice.GenerateMips(_shaderResourceView);
                    }

                    //Dispose of the staging texture
                    if (_staging != null)
                    {
                        _staging.Dispose();
                        _staging = null;
                    }
                } catch (Exception e) {
                    Dispose();
                    throw new TeslaException("Error setting Texture data.", e);
                }
            }
        }
Exemple #12
0
        /// <summary>
        /// Creates a new instance of <see cref="D3D10TextureCubeImplementation"/>.
        /// </summary>
        /// <param name="renderer">The D3D10 renderer.</param>
        /// <param name="size">The size (width/height) of each cube face.</param>
        /// <param name="genMipMaps">True if a mipmap chain should be generated or not.</param>
        /// <param name="format">The surface  format.</param>
        /// <param name="depthFormat">The depth-stencil format.</param>
        /// <param name="multiSampleCount">The number of sample locations for multisampling</param>
        /// <param name="usage">Sets the render target's behavior.</param>
        internal D3D10TextureCubeImplementation(D3D10Renderer renderer, int size, bool genMipMaps, SurfaceFormat format, DepthFormat depthFormat, int multiSampleCount, RenderTargetUsage usage)
            : base(size, format, depthFormat, multiSampleCount, usage)
        {
            _renderer       = renderer;
            _graphicsDevice = _renderer.GraphicsDevice;

            if (!renderer.Adapter.QueryRenderTargetFormat(format, depthFormat, multiSampleCount))
            {
                throw new ArgumentException("Format combination not supported.");
            }

            D3D.Texture2DDescription desc2D = new D3D.Texture2DDescription();
            desc2D.ArraySize      = 6;
            desc2D.BindFlags      = D3D.BindFlags.ShaderResource | D3D.BindFlags.RenderTarget;
            desc2D.CpuAccessFlags = D3D.CpuAccessFlags.None;
            desc2D.Format         = D3D10Helper.ToD3DSurfaceFormat(format);
            desc2D.Height         = size;
            desc2D.Width          = size;
            desc2D.Usage          = D3D.ResourceUsage.Default;
            if (genMipMaps)
            {
                desc2D.OptionFlags = D3D.ResourceOptionFlags.GenerateMipMaps | D3D.ResourceOptionFlags.TextureCube;
            }
            else
            {
                desc2D.OptionFlags = D3D.ResourceOptionFlags.TextureCube;
            }
            desc2D.MipLevels = (genMipMaps) ? 0 : 1;
            if (multiSampleCount > 1)
            {
                desc2D.SampleDescription = new SlimDX.DXGI.SampleDescription(multiSampleCount, 0);
            }
            else
            {
                desc2D.SampleDescription = new DXGI.SampleDescription(1, 0);
            }

            //Create the resources
            _texture2D          = new D3D.Texture2D(_graphicsDevice, desc2D);
            _shaderResourceView = new D3D.ShaderResourceView(_graphicsDevice, _texture2D);
            _renderTargetView   = new D3D.RenderTargetView[6];

            //Add to tracker
            _renderer.Resources.AddTrackedObject(_texture2D.ComPointer, this);
            _renderer.Resources.AddTrackedObject(_shaderResourceView.ComPointer, this);

            //Setup each render target view for each face
            for (int i = 0; i < 6; i++)
            {
                D3D.RenderTargetViewDescription rtDesc = new D3D.RenderTargetViewDescription();
                rtDesc.ArraySize       = 1;
                rtDesc.FirstArraySlice = i;
                if (multiSampleCount > 1)
                {
                    rtDesc.Dimension = D3D.RenderTargetViewDimension.Texture2DMultisampledArray;
                }
                else
                {
                    rtDesc.Dimension = D3D.RenderTargetViewDimension.Texture2DArray;
                }
                rtDesc.Format        = desc2D.Format;
                _renderTargetView[i] = new D3D.RenderTargetView(_graphicsDevice, _texture2D, rtDesc);
                _graphicsDevice.ClearRenderTargetView(_renderTargetView[i], new SDX.Color4(1.0f, 0, 0, 0));
                _renderer.Resources.AddTrackedObject(_renderTargetView[i].ComPointer, this);
            }

            _mipCount = _texture2D.Description.MipLevels;

            if (depthFormat != DepthFormat.None)
            {
                D3D.Texture2DDescription dbdesc = new D3D.Texture2DDescription();
                dbdesc.ArraySize      = 6;
                dbdesc.BindFlags      = D3D.BindFlags.DepthStencil;
                dbdesc.CpuAccessFlags = D3D.CpuAccessFlags.None;
                dbdesc.Format         = D3D10Helper.ToD3DDepthFormat(depthFormat);
                dbdesc.Height         = size;
                dbdesc.Width          = size;
                dbdesc.Usage          = D3D.ResourceUsage.Default;
                dbdesc.OptionFlags    = D3D.ResourceOptionFlags.None;
                dbdesc.MipLevels      = 1;
                if (multiSampleCount > 1)
                {
                    dbdesc.SampleDescription = new SlimDX.DXGI.SampleDescription(multiSampleCount, 0);
                }
                else
                {
                    dbdesc.SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0);
                }

                using (D3D.Texture2D depthBuffer = new D3D.Texture2D(_graphicsDevice, dbdesc)) {
                    _depthStencilView = new D3D.DepthStencilView(_graphicsDevice, depthBuffer);
                    if (depthFormat == Tesla.Graphics.DepthFormat.Depth24Stencil8)
                    {
                        _graphicsDevice.ClearDepthStencilView(_depthStencilView, D3D.DepthStencilClearFlags.Depth | D3D.DepthStencilClearFlags.Stencil, 1.0f, 0);
                    }
                    else
                    {
                        _graphicsDevice.ClearDepthStencilView(_depthStencilView, D3D.DepthStencilClearFlags.Depth, 1.0f, 0);
                    }
                    //Add to tracker
                    _renderer.Resources.AddTrackedObject(_depthStencilView.ComPointer, this);
                }
            }
        }
Exemple #13
0
        /// <summary>
        /// Creates a new instance of <see cref="D3D10TextureCubeImplementation"/>.
        /// </summary>
        /// <param name="renderer">D3D10 renderer</param>
        /// <param name="size">The size (width/height) of each cube face.</param>
        /// <param name="genMipMaps">True if mip levels should be generated.</param>
        /// <param name="format">The surface format.</param>
        /// <param name="data">The initial data for the first mip level.</param>
        /// <exception cref="System.ArgumentException">Thrown if the data buffery array does not correspond to a valid cube map (6 faces, all the same size)</exception>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if there was an error writing to the texture.</exception>
        internal D3D10TextureCubeImplementation(D3D10Renderer renderer, int size, bool genMipMaps, SurfaceFormat format, DataBuffer[] data)
            : base(size, format)
        {
            //Set the common properties
            _renderer       = renderer;
            _graphicsDevice = _renderer.GraphicsDevice;

            //Do we want to generate mip maps, and do we have the data to do so? (if not, cancel mip map generation)
            bool canGenMipmaps = (genMipMaps && (data != null));

            //Setup texture description
            D3D.Texture2DDescription descTex = new D3D.Texture2DDescription();
            descTex.ArraySize         = 6;
            descTex.Width             = size;
            descTex.Height            = size;
            descTex.Usage             = _usage = D3D.ResourceUsage.Default;
            descTex.CpuAccessFlags    = D3D.CpuAccessFlags.None;
            descTex.Format            = D3D10Helper.ToD3DSurfaceFormat(format);
            descTex.SampleDescription = new DXGI.SampleDescription(1, 0);
            descTex.MipLevels         = (genMipMaps) ? 0 : 1;

            //Set mip map generation params
            if (canGenMipmaps)
            {
                descTex.BindFlags   = D3D.BindFlags.ShaderResource | D3D.BindFlags.RenderTarget;
                descTex.OptionFlags = D3D.ResourceOptionFlags.GenerateMipMaps | D3D.ResourceOptionFlags.TextureCube;
            }
            else
            {
                descTex.BindFlags   = D3D.BindFlags.ShaderResource;
                descTex.OptionFlags = D3D.ResourceOptionFlags.TextureCube;
            }

            //Create the texture and shader view
            _texture2D          = new D3D.Texture2D(_graphicsDevice, descTex);
            _shaderResourceView = new D3D.ShaderResourceView(_graphicsDevice, _texture2D);

            //Add to tracker
            _renderer.Resources.AddTrackedObject(_texture2D.ComPointer, this);
            _renderer.Resources.AddTrackedObject(_shaderResourceView.ComPointer, this);

            //Set the final mip count
            _mipCount = _texture2D.Description.MipLevels;

            //Now set the initial data if its present
            if (data != null)
            {
                if (data.Length != 6)
                {
                    Dispose();
                    throw new ArgumentException("Initial data array must have six data buffers, one for each cube face.");
                }

                int num = data[0].Length;

                for (int i = 1; i < 6; i++)
                {
                    if (data[i].Length != num)
                    {
                        Dispose();
                        throw new ArgumentException("All data buffers must be of same length");
                    }
                }

                try {
                    for (int i = 0; i < 6; i++)
                    {
                        DataBuffer db = data[i];
                        this.SetData <byte>((CubeMapFace)i, db.ByteDataCopy, 0, null, 0, db.ElementSizeInBytes * db.Length);
                    }

                    if (genMipMaps)
                    {
                        _graphicsDevice.GenerateMips(_shaderResourceView);
                    }

                    //Dispose of the staging texture
                    if (_staging != null)
                    {
                        _staging.Dispose();
                        _staging = null;
                    }
                } catch (Exception e) {
                    Dispose();
                    throw new TeslaException("Error setting Texture data.", e);
                }
            }
        }
Exemple #14
0
 /// <summary>
 /// Creates a new instance of <see cref="D3D10DepthStencilStateImplementation"/>.
 /// </summary>
 /// <param name="renderer">The D3D10 renderer.</param>
 internal D3D10DepthStencilStateImplementation(D3D10Renderer renderer)
     : base()
 {
     _renderer       = renderer;
     _graphicsDevice = renderer.GraphicsDevice;
 }