/// <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="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); } }
/// <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> /// 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); } } }
/// <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); } } }
/// <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); } } }