public DeviceResourceContext(GraphicsDevice device, Effect effect, VertexDeclaration declaration, ResourceBufferInfo resourceBufferInfo, int indexStructSize) { var vertexSize = declaration.CalculateSize(); VertexCount = resourceBufferInfo.VertexCount; IndexCount = resourceBufferInfo.IndexCount; IsIndexBufferDynamic = resourceBufferInfo.IsIndexBufferDynamic; VertexBuffer = Buffer.Vertex.New(device, VertexCount * vertexSize, GraphicsResourceUsage.Dynamic).DisposeBy(this); if (IsIndexBufferDynamic) { IndexBuffer = Buffer.Index.New(device, IndexCount * indexStructSize, GraphicsResourceUsage.Dynamic).DisposeBy(this); } else { IndexBuffer = Buffer.Index.New(device, resourceBufferInfo.StaticIndices).DisposeBy(this); IndexBuffer.Reload = graphicsResource => ((Buffer)graphicsResource).Recreate(resourceBufferInfo.StaticIndices); } var indexBufferBinding = new IndexBufferBinding(IndexBuffer, indexStructSize == sizeof(int), IndexBuffer.Description.SizeInBytes / indexStructSize); var vertexBufferBinding = new VertexBufferBinding(VertexBuffer, declaration, VertexCount, vertexSize); // Creates a VAO VertexArrayObject = VertexArrayObject.New(device, effect.InputSignature, indexBufferBinding, vertexBufferBinding).DisposeBy(this); }
public SharedData(GraphicsDevice device, EffectInputSignature defaultSignature) { var vertexBuffer = Buffer.Vertex.New(device, QuadsVertices).DisposeBy(this); // Register reload vertexBuffer.Reload = (graphicsResource) => ((Buffer)graphicsResource).Recreate(QuadsVertices); VertexBuffer = VertexArrayObject.New(device, defaultSignature, new VertexBufferBinding(vertexBuffer, VertexPositionTexture.Layout, QuadsVertices.Length, VertexPositionTexture.Size)).DisposeBy(this); }
/// <summary> /// Initializes a new instance of the <see cref="GeometricPrimitive{T}"/> class. /// </summary> /// <param name="graphicsDevice">The graphics device.</param> /// <param name="geometryMesh">The geometry mesh.</param> /// <exception cref="System.InvalidOperationException">Cannot generate more than 65535 indices on feature level HW <= 9.3</exception> public GeometricPrimitive(GraphicsDevice graphicsDevice, GeometricMeshData <T> geometryMesh) { GraphicsDevice = graphicsDevice; var vertices = geometryMesh.Vertices; var indices = geometryMesh.Indices; if (geometryMesh.IsLeftHanded) { ReverseWinding(vertices, indices); } if (indices.Length < 0xFFFF) { var indicesShort = new ushort[indices.Length]; for (int i = 0; i < indicesShort.Length; i++) { indicesShort[i] = (ushort)indices[i]; } IndexBuffer = Buffer.Index.New(graphicsDevice, indicesShort).RecreateWith(indicesShort).DisposeBy(this); } else { if (graphicsDevice.Features.Profile <= GraphicsProfile.Level_9_3) { throw new InvalidOperationException("Cannot generate more than 65535 indices on feature level HW <= 9.3"); } IndexBuffer = Buffer.Index.New(graphicsDevice, indices).RecreateWith(indices).DisposeBy(this); IsIndex32Bits = true; } // For now it will keep buffers for recreation. // TODO: A better alternative would be to store recreation parameters so that we can reuse procedural code. VertexBuffer = Buffer.Vertex.New(graphicsDevice, vertices).RecreateWith(vertices).DisposeBy(this); vertexArrayObject = VertexArrayObject.New(graphicsDevice, new IndexBufferBinding(IndexBuffer, IsIndex32Bits, indices.Length), new VertexBufferBinding(VertexBuffer, new T().GetLayout(), vertices.Length)).DisposeBy(this); }