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); }
/// <summary> /// Create a new vertex buffer with the data in the host buffer. /// </summary> /// <param name="vertexCount">The number of vertices in the buffer.</param> /// <param name="description">The layout of the vertices in the buffer.</param> /// <param name="data">The optional initial vertex data.</param> /// <param name="dataOffset">The offset into the data source buffer from which to copy.</param> /// <param name="usage">The buffer usage policy.</param> public VertexBuffer(uint vertexCount, VertexDescription description, HostBuffer data, ulong dataOffset = 0, BufferUsage usage = BufferUsage.Static) : base(vertexCount * description.Stride, ResourceType.VertexBuffer, usage, data, dataOffset) { VertexCount = vertexCount; VertexDescription = description.Duplicate(); }
/// <summary> /// Create a new vertex buffer with optional pointer to initial data. /// </summary> /// <param name="vertexCount">The number of vertices in the buffer.</param> /// <param name="description">The layout of the vertices in the buffer.</param> /// <param name="data">The optional initial vertex data.</param> /// <param name="usage">The buffer usage policy.</param> public VertexBuffer(uint vertexCount, VertexDescription description, void *data = null, BufferUsage usage = BufferUsage.Static) : base(vertexCount * description.Stride, ResourceType.VertexBuffer, usage, data) { VertexCount = vertexCount; VertexDescription = description.Duplicate(); }
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); } }
internal int GetLayout(VertexDescription description, Shader shader) { if (!OpenGLCapabilities.VertexAttribBinding) { throw new PlatformNotSupportedException("VertexAttribBinding (separat vertex attributes) are not supported by the driver"); } int layout; if (inputLayoutPool.TryGetValue(description, out layout)) { return(layout); } layout = GL.GenVertexArray(); if (OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { BindManager.VertexArray = layout; int offset = 0; VertexElement[] elements = description.GetElements(); for (int i = 0; i < description.ElementCount; i++) { if (offset > OpenGLCapabilities.MaxVertexAttribBindingOffset) { throw new PlatformNotSupportedException("offset is higher than maximum supportet offset."); } GL.EnableVertexAttribArray(i); GL.VertexAttribBinding(i, 0); GL.VertexAttribFormat(i, GetComponentsOf(elements[i].Type), VertexAttribType.Float, false, offset); offset += GetSizeOf(elements[i].Type); } } else if (OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { int offset = 0; VertexElement[] elements = description.GetElements(); for (int i = 0; i < description.ElementCount; i++) { if (offset > OpenGLCapabilities.MaxVertexAttribBindingOffset) { throw new PlatformNotSupportedException("offset is higher than maximum supportet offset."); } Ext.EnableVertexArrayAttrib(layout, i); Ext.VertexArrayVertexAttribBinding(layout, i, elements[i].UsageIndex); Ext.VertexArrayVertexAttribFormat(layout, i, GetComponentsOf(elements[i].Type), (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)VertexAttribType.Float, false, offset); offset += GetSizeOf(elements[i].Type); } } inputLayoutPool.Add(description, layout); return(layout); }
public int GetSizeOf(VertexDescription description) { int size = 0; VertexElement[] elements = description.GetElements(); for (int i = 0; i < elements.Length; i++) { size += GetSizeOf(elements[i].Type); } return(size); }
public StaticMesh LoadMesh <TVertex>(string name, TVertex[] vertices, VertexDescription vertexDescription) where TVertex : struct { if (vertices == null) { throw new ArgumentNullException("vertices"); } if (vertices.Length == 0) { throw new ArgumentException("Vertices is empty.", "vertices"); } IVertexBuffer vertexBuffer = Engine.GraphicsDevice.Factory.CreateVertexBuffer(vertices, vertexDescription, ResourceUsage.Normal); return(new StaticMesh(this, name, AssetType.User, vertexBuffer)); }
public IVertexBuffer CreateVertexBuffer <T>(T[] data, VertexDescription description, ResourceUsage usage) where T : struct { VertexBuffer buffer = null; if (data != null) { GCHandle handle; DataArray dataArray = DataArray.FromArray(data, out handle); try { buffer = new VertexBuffer(graphicsDevice, description, usage, dataArray); } finally { handle.Free(); } } return(buffer); }
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); }
internal VertexBuffer(GraphicsDevice graphicsDevice, VertexDescription description, ResourceUsage usage, DataArray data) : base(graphicsDevice, new System.Diagnostics.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; this.Usage = usage; VboID = GL.GenBuffer(); if (!graphicsDevice.OpenGLCapabilities.VertexAttribBinding) { VaoID = GL.GenVertexArray(); LayoutDirty = true; } if (!data.IsNull) { SizeBytes = data.Size; if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { graphicsDevice.BindManager.VertexBuffer = this; GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(SizeBytes), data.Pointer, EnumConverter.Convert(Usage)); } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { OpenTK.Graphics.OpenGL.GL.Ext.NamedBufferData(VboID, new IntPtr(SizeBytes), data.Pointer, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.Convert(Usage)); } } graphicsDevice.CheckGLError("VertexBuffer Constructor"); }
private void FindAttributes() { int attributes; GL.GetProgram(ProgramID, GetProgramParameterName.ActiveAttributes, out attributes); VertexElement[] elements = new VertexElement[attributes]; for (int i = 0; i < attributes; i++) { int size; ActiveAttribType type; StringBuilder builder = new StringBuilder(255); int length; GL.GetActiveAttrib(ProgramID, i, 255, out length, out size, out type, builder); string name = builder.ToString(0, length); elements[attributes - 1 - i] = new VertexElement(EnumConverter.Convert(name), 0, EnumConverter.Convert(type)); } VertexDescription = new VertexDescription(elements); }
internal VertexBuffer(GraphicsDevice graphicsDevice, VertexDescription description, ResourceUsage usage, int vertexCount) : base(graphicsDevice, new System.Diagnostics.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; this.Usage = usage; VboID = GL.GenBuffer(); if (!graphicsDevice.OpenGLCapabilities.VertexAttribBinding) { VaoID = GL.GenVertexArray(); LayoutDirty = true; } SizeBytes = vertexCount * graphicsDevice.GetSizeOf(description); if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { graphicsDevice.BindManager.VertexBuffer = this; GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(SizeBytes), IntPtr.Zero, EnumConverter.Convert(Usage)); } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { OpenTK.Graphics.OpenGL.GL.Ext.NamedBufferData(VboID, new IntPtr(SizeBytes), IntPtr.Zero, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.Convert(Usage)); } graphicsDevice.CheckGLError("VertexBuffer Constructor"); }
public SceneShader GetShader(VertexDescription vertexDescription, MaterialDescription materialDescription) { return(shaders.First(s => s.VertexDescription.EqualsIgnoreOrder(vertexDescription) && s.MaterialDescription.Equals(materialDescription))); }
public Shader(GraphicsDevice graphicsDevice, string name, ShaderBytecode vertex, ShaderBytecode pixel) : base(graphicsDevice, new StackTrace(1)) { if (name == null) { throw new ArgumentNullException("name"); } if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Name is empty or whitespace.", "name"); } if (vertex == null) { throw new ArgumentNullException("vertex"); } if (pixel == null) { throw new ArgumentNullException("pixel"); } this.Name = name; this.VertexCode = vertex; this.PixelCode = pixel; using (ShaderReflection reflection = new ShaderReflection(VertexCode)) { for (int i = 0; i < reflection.Description.BoundResources; i++) { var res = reflection.GetResourceBindingDescription(i); resourcesVertex[res.Name] = res.BindPoint; if (res.Type == ShaderInputType.ConstantBuffer) { constantBufferSizes[res.Name] = reflection.GetConstantBuffer(res.Name).Description.Size; } } int inputCount = reflection.Description.InputParameters; var vertexElements = new VertexElement[inputCount]; for (int i = 0; i < inputCount; i++) { var input = reflection.GetInputParameterDescription(i); if (input.ComponentType != RegisterComponentType.Float32) { continue; } VertexElementType type; if (input.UsageMask.HasFlag(RegisterComponentMaskFlags.All)) { type = VertexElementType.Vector4; } else if (input.UsageMask.HasFlag(RegisterComponentMaskFlags.ComponentX | RegisterComponentMaskFlags.ComponentY | RegisterComponentMaskFlags.ComponentZ)) { type = VertexElementType.Vector3; } else if (input.UsageMask.HasFlag(RegisterComponentMaskFlags.ComponentX | RegisterComponentMaskFlags.ComponentY)) { type = VertexElementType.Vector2; } else if (input.UsageMask.HasFlag(RegisterComponentMaskFlags.ComponentX)) { type = VertexElementType.Single; } else { continue; } VertexElementUsage usage = EnumConverter.ConvertToUsage(input.SemanticName); vertexElements[i] = new VertexElement(usage, input.SemanticIndex, type); } VertexDescription = new VertexDescription(vertexElements); } using (ShaderReflection reflection = new ShaderReflection(PixelCode)) { for (int i = 0; i < reflection.Description.BoundResources; i++) { var res = reflection.GetResourceBindingDescription(i); resourcesPixel[res.Name] = res.BindPoint; if (res.Type == ShaderInputType.ConstantBuffer) { constantBufferSizes[res.Name] = reflection.GetConstantBuffer(res.Name).Description.Size; } } } VertexShaderHandle = new VertexShader(graphicsDevice.Device, VertexCode); PixelShaderHandle = new PixelShader(graphicsDevice.Device, PixelCode); using (MemoryStream stream = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(stream)) { writer.Write("DIRECTX11"); writer.Write(VertexCode.Data.Length); writer.Write(VertexCode.Data); writer.Write(PixelCode.Data.Length); writer.Write(PixelCode.Data); binaryCode = stream.GetBuffer(); } }
public IVertexBuffer CreateVertexBuffer(int vertexCount, VertexDescription description, ResourceUsage usage) { return(new VertexBuffer(graphicsDevice, vertexCount, description, usage)); }
public IVertexBuffer CreateVertexBuffer(int vertexCount, VertexDescription description, ResourceUsage usage) { AssertCurrent(); return(Register(new VertexBuffer(graphicsDevice, description, usage, vertexCount))); }
public void RemoveShader(VertexDescription vertexDescription, MaterialDescription materialDescription) { shaders.Remove(shaders.First(s => s.VertexDescription.EqualsIgnoreOrder(vertexDescription) && s.MaterialDescription.Equals(materialDescription))); }
public DrawCall(VertexDescription vtx_info, Material material) { VertexData = vtx_info; Material = material; }