public VertexBuffer(GraphicsDevice graphicsDevice, VertexDescription description, Graphics.ResourceUsage usage, DataArray data) : base(graphicsDevice, new StackTrace(1)) { if (data.IsNull) { throw new ArgumentNullException("data.Pointer"); } if (data.Size <= 0) { throw new ArgumentOutOfRangeException("data.Size", data.Size, "Size must be bigger than 0."); } this.Description = description; int descriptionSize = graphicsDevice.GetSizeOf(description); this.Usage = usage; if (!data.IsNull) { this.SizeBytes = data.Size; BufferDescription bufferDescription = new BufferDescription(SizeBytes, (SharpDX.Direct3D11.ResourceUsage)EnumConverter.Convert(usage), BindFlags.VertexBuffer, EnumConverter.ConvertToAccessFlag(Usage), ResourceOptionFlags.None, 0); this.Buffer = new SharpDX.Direct3D11.Buffer(graphicsDevice.Device, data.Pointer, bufferDescription); Binding = new VertexBufferBinding(Buffer, descriptionSize, 0); } }
public IndexBuffer(GraphicsDevice graphicsDevice, IndexFormat format, Graphics.ResourceUsage usage, int indexCount) : base(graphicsDevice, new StackTrace(1)) { if (indexCount <= 0) { throw new ArgumentOutOfRangeException("indexCount", indexCount, "indexCount must be bigger than zero."); } if (usage == ResourceUsage.Immutable) { throw new ArgumentException("data", "Immutable buffers must be initialized with data."); } this.Format = format; this.SizeBytes = indexCount * graphicsDevice.GetSizeOf(format); this.Usage = usage; BufferDescription bufferDescription = new BufferDescription(SizeBytes, (SharpDX.Direct3D11.ResourceUsage)EnumConverter.Convert(Usage), BindFlags.IndexBuffer, EnumConverter.ConvertToAccessFlag(Usage), ResourceOptionFlags.None, 0); this.Buffer = new SharpDX.Direct3D11.Buffer(graphicsDevice.Device, bufferDescription); }
public VertexBuffer(GraphicsDevice graphicsDevice, int vertexCount, VertexDescription description, Graphics.ResourceUsage usage) : base(graphicsDevice, new StackTrace(1)) { if (vertexCount <= 0) { throw new ArgumentException("vertexCount must not be smaller than/ equal to zero.", "vertexCount"); } if (usage == ResourceUsage.Immutable) { throw new ArgumentException("data", "Immutable buffers must be initialized with data."); } this.Description = description; int descriptionSize = graphicsDevice.GetSizeOf(description); this.SizeBytes = descriptionSize * vertexCount; this.Usage = usage; BufferDescription bufferDescription = new BufferDescription(SizeBytes, (SharpDX.Direct3D11.ResourceUsage)EnumConverter.Convert(Usage), BindFlags.VertexBuffer, EnumConverter.ConvertToAccessFlag(Usage), ResourceOptionFlags.None, 0); this.Buffer = new SharpDX.Direct3D11.Buffer(graphicsDevice.Device, bufferDescription); Binding = new VertexBufferBinding(Buffer, descriptionSize, 0); }
public IndexBuffer(GraphicsDevice graphicsDevice, IndexFormat format, Graphics.ResourceUsage usage, DataArray data) : base(graphicsDevice, new StackTrace(1)) { if (data.IsNull) { throw new ArgumentException("data.Pointer is null"); } if (data.Size <= 0) { throw new ArgumentOutOfRangeException("data.Size", data.Size, "Size must be bigger than 0."); } this.Format = format; this.SizeBytes = data.Size; this.Usage = usage; if (!data.IsNull) { this.SizeBytes = data.Size; BufferDescription bufferDescription = new BufferDescription(SizeBytes, (SharpDX.Direct3D11.ResourceUsage)EnumConverter.Convert(usage), BindFlags.IndexBuffer, EnumConverter.ConvertToAccessFlag(Usage), ResourceOptionFlags.None, 0); this.Buffer = new SharpDX.Direct3D11.Buffer(graphicsDevice.Device, data.Pointer, bufferDescription); } }
public InputLayout GetLayout(VertexDescription description, Shader shader) { InputLayout layout; if (inputLayoutPool.TryGetValue(description, out layout)) { return(layout); } InputElement[] dxElements = new InputElement[description.ElementCount]; VertexElement[] elements = description.GetElements(); for (int i = 0; i < description.ElementCount; i++) { dxElements[i] = new InputElement(EnumConverter.Convert(elements[i].Usage), 0, EnumConverter.Convert(elements[i].Type), elements[i].UsageIndex); } layout = inputLayoutPool[description] = new InputLayout(Device, shader.VertexCode, dxElements); return(layout); }
public int GetSizeOf(IndexFormat format) { return(SharpDX.DXGI.FormatHelper.SizeOfInBytes(EnumConverter.Convert(format))); }
public int GetSizeOf(VertexElementType format) { return(SharpDX.DXGI.FormatHelper.SizeOfInBytes(EnumConverter.Convert(format))); }
private void ApplyState() { ApplyRenderTargets(); var shader = graphicsDevice.Cast <Shader>(currentState.Shader, "currentState.Shader"); if (stateDirty) { if (vertexCache.Shader != shader) { Context.VertexShader.Set(shader.VertexShaderHandle); vertexCache.Shader = shader; } if (pixelCache.Shader != shader) { if (!currentState.Shader.VertexDescription.EqualsIgnoreOrder(currentVertexBuffer.Description)) { throw new GraphicsException("Current shader VertexDescription doesn't match the description of the current VertexBuffer"); } Context.PixelShader.Set(shader.PixelShaderHandle); pixelCache.Shader = shader; } if (primitiveType != currentState.PrimitiveType) { Context.InputAssembler.PrimitiveTopology = EnumConverter.Convert(currentState.PrimitiveType); primitiveType = currentState.PrimitiveType; } if (currentRasterizer != currentState.Rasterizer) { var rasterizer = graphicsDevice.Cast <RasterizerState>(currentState.Rasterizer, "currentState.Rasterizer"); Context.Rasterizer.State = rasterizer.Handle; currentRasterizer = rasterizer; } if (currentDepthStencil != currentState.DepthStencil) { var depthStencil = graphicsDevice.Cast <DepthStencilState>(currentState.DepthStencil, "currentState.DepthStencil"); Context.OutputMerger.DepthStencilState = depthStencil.Handle; currentDepthStencil = depthStencil; } if (currentBlend != currentState.Blend) { var blend = graphicsDevice.Cast <BlendState>(currentState.Blend, "currentState.Blend"); Context.OutputMerger.BlendState = blend.Handle; currentBlend = blend; } } if (vertexBufferDirty) { if (!currentState.Shader.VertexDescription.EqualsIgnoreOrder(currentVertexBuffer.Description)) { throw new GraphicsException("Current shader VertexDescription doesn't match the description of the current VertexBuffer"); } Context.InputAssembler.InputLayout = graphicsDevice.GetLayout(currentVertexBuffer.Description, shader); Context.InputAssembler.SetVertexBuffers(0, currentVertexBuffer.Binding); } if (indexBufferDirty) { Context.InputAssembler.SetIndexBuffer(currentIndexBuffer.Buffer, currentIndexBuffer.IndexFormat, 0); } stateDirty = false; vertexBufferDirty = false; indexBufferDirty = false; }