public Mesh (MemoryBlock vertices, VertexLayout decl, ushort[] indices) { var group = new MeshGroup(); group.VertexBuffer = new VertexBuffer(vertices, decl); group.IndexBuffer = new IndexBuffer(MemoryBlock.FromArray(indices)); vertexDecl = decl; groups = new List<MeshGroup> { group }; }
/// <summary> /// Welds vertices that are close together. /// </summary> /// <param name="layout">The layout of the vertex stream.</param> /// <param name="data">A pointer to the vertex data stream.</param> /// <param name="count">The number of vertices in the stream.</param> /// <param name="remappingTable">An output remapping table from the original vertices to the welded ones.</param> /// <param name="epsilon">The tolerance for welding vertex positions.</param> /// <returns> /// The number of unique vertices after welding. /// </returns> public static int WeldVertices(VertexLayout layout, IntPtr data, int count, out int[] remappingTable, float epsilon = 0.001f) { var output = stackalloc ushort[count]; var result = NativeMethods.bgfx_weld_vertices(output, ref layout.data, data, (ushort)count, epsilon); remappingTable = new int[count]; for (int i = 0; i < count; i++) { remappingTable[i] = output[i]; } return(result); }
private void Init() { _imguiContext = ImGui.CreateContext(); var io = ImGui.GetIO(); // load the shaders for imgui _imguiProgram = ResourceLoader.LoadProgram("vs_ocornut_imgui", "fs_ocornut_imgui"); _textureProgram = ResourceLoader.LoadProgram("vs_imgui_image", "fs_imgui_image"); _vertexLayout = new VertexLayout(); _vertexLayout.Begin() .Add(VertexAttributeUsage.Position, 2, VertexAttributeType.Float) .Add(VertexAttributeUsage.TexCoord0, 2, VertexAttributeType.Float) .Add(VertexAttributeUsage.Color0, 4, VertexAttributeType.UInt8, true) .End(); _texUniform = new Uniform("s_tex", UniformType.Sampler); _fonts.Add("default", io.Fonts.AddFontDefault()); // Fonts: All fonts need to be known here, because we compile the fontatlas only once // Example code: /*foreach (var font in fonts) * { * _fonts.Add(font.name, io.Fonts.AddFontFromFileTTF(font.path, font.size)); * }*/ // Build and save the fontatlas unsafe { io.Fonts.GetTexDataAsRGBA32(out var data, out var width, out var height, out var bytesPerPixel); FontAtlas = Texture.Create2D(width, height, false, 1, TextureFormat.BGRA8, 0, new MemoryBlock((IntPtr)data, width * height * bytesPerPixel)); _textures.Add((IntPtr)FontAtlas.GetHashCode(), FontAtlas); } io.Fonts.SetTexID((IntPtr)FontAtlas.GetHashCode()); // ToDo: Set KeyMappings // -> Engine specific }
public static extern ushort bgfx_create_dynamic_vertex_buffer(int vertexCount, ref VertexLayout.Data decl, BufferFlags flags);
public static extern void bgfx_vertex_unpack(float* output, VertexAttributeUsage attribute, ref VertexLayout.Data decl, IntPtr data, int index);
public static extern bool bgfx_check_avail_transient_buffers(int numVertices, ref VertexLayout.Data decl, int numIndices);
public static extern void bgfx_vertex_decl_begin(ref VertexLayout.Data decl, RendererBackend backend);
public static extern void bgfx_vertex_decl_skip(ref VertexLayout.Data decl, byte count);
public static extern bool bgfx_alloc_transient_buffers(out TransientVertexBuffer tvb, ref VertexLayout.Data decl, ushort numVertices, out TransientIndexBuffer tib, ushort numIndices);
/// <summary> /// Gets the available space in the global transient vertex buffer. /// </summary> /// <param name="count">The number of vertices required.</param> /// <param name="layout">The layout of each vertex.</param> /// <returns>The number of available vertices.</returns> public static int GetAvailableSpace(int count, VertexLayout layout) { return(NativeMethods.bgfx_get_avail_transient_vertex_buffer(count, ref layout.data)); }
/// <summary> /// Packs a vector into vertex stream format. /// </summary> /// <param name="input">The four element vector to pack.</param> /// <param name="inputNormalized"><c>true</c> if the input vector is normalized.</param> /// <param name="attribute">The attribute usage of the vector data.</param> /// <param name="layout">The layout of the vertex stream.</param> /// <param name="data">The pointer to the vertex data stream.</param> /// <param name="index">The index of the vertex within the stream.</param> public static void VertexPack(float* input, bool inputNormalized, VertexAttributeUsage attribute, VertexLayout layout, IntPtr data, int index = 0) { NativeMethods.bgfx_vertex_pack(input, inputNormalized, attribute, ref layout.data, data, index); }
/// <summary> /// Unpack a vector from a vertex stream. /// </summary> /// <param name="output">A pointer to four floats that will receive the unpacked vector.</param> /// <param name="attribute">The usage of the vertex attribute.</param> /// <param name="layout">The layout of the vertex stream.</param> /// <param name="data">A pointer to the vertex data stream.</param> /// <param name="index">The index of the vertex within the stream.</param> public static void VertexUnpack(float* output, VertexAttributeUsage attribute, VertexLayout layout, IntPtr data, int index = 0) { NativeMethods.bgfx_vertex_unpack(output, attribute, ref layout.data, data, index); }
/// <summary> /// Converts a stream of vertex data from one format to another. /// </summary> /// <param name="destinationLayout">The destination format.</param> /// <param name="destinationData">A pointer to the output location.</param> /// <param name="sourceLayout">The source format.</param> /// <param name="sourceData">A pointer to the source vertex data to convert.</param> /// <param name="count">The number of vertices to convert.</param> public static void VertexConvert(VertexLayout destinationLayout, IntPtr destinationData, VertexLayout sourceLayout, IntPtr sourceData, int count = 1) { NativeMethods.bgfx_vertex_convert(ref destinationLayout.data, destinationData, ref sourceLayout.data, sourceData, count); }
/// <summary> /// Checks for available space to allocate transient index and vertex buffers. /// </summary> /// <param name="vertexCount">The number of vertices to allocate.</param> /// <param name="layout">The layout of each vertex.</param> /// <param name="indexCount">The number of indices to allocate.</param> /// <returns><c>true</c> if there is sufficient space for both vertex and index buffers.</returns> public static bool CheckAvailableTransientBufferSpace(int vertexCount, VertexLayout layout, int indexCount) { return NativeMethods.bgfx_check_avail_transient_buffers(vertexCount, ref layout.data, indexCount); }
/// <summary> /// Attempts to allocate both a transient vertex buffer and index buffer. /// </summary> /// <param name="vertexCount">The number of vertices to allocate.</param> /// <param name="layout">The layout of each vertex.</param> /// <param name="indexCount">The number of indices to allocate.</param> /// <param name="vertexBuffer">Returns the allocated transient vertex buffer.</param> /// <param name="indexBuffer">Returns the allocated transient index buffer.</param> /// <returns><c>true</c> if both space requirements are satisfied and the buffers were allocated.</returns> public static bool AllocateTransientBuffers(int vertexCount, VertexLayout layout, int indexCount, out TransientVertexBuffer vertexBuffer, out TransientIndexBuffer indexBuffer) { return NativeMethods.bgfx_alloc_transient_buffers(out vertexBuffer, ref layout.data, (ushort)vertexCount, out indexBuffer, (ushort)indexCount); }
public static extern void bgfx_vertex_convert(ref VertexLayout.Data destDecl, IntPtr destData, ref VertexLayout.Data srcDecl, IntPtr srcData, int num);
/// <summary> /// Welds vertices that are close together. /// </summary> /// <param name="layout">The layout of the vertex stream.</param> /// <param name="data">A pointer to the vertex data stream.</param> /// <param name="count">The number of vertices in the stream.</param> /// <param name="remappingTable">An output remapping table from the original vertices to the welded ones.</param> /// <param name="epsilon">The tolerance for welding vertex positions.</param> /// <returns> /// The number of unique vertices after welding. /// </returns> public static int WeldVertices(VertexLayout layout, IntPtr data, int count, out int[] remappingTable, float epsilon = 0.001f) { var output = stackalloc ushort[count]; var result = NativeMethods.bgfx_weld_vertices(output, ref layout.data, data, (ushort)count, epsilon); remappingTable = new int[count]; for (int i = 0; i < count; i++) remappingTable[i] = output[i]; return result; }
/// <summary> /// Initializes a new instance of the <see cref="TransientVertexBuffer"/> struct. /// </summary> /// <param name="vertexCount">The number of vertices that fit in the buffer.</param> /// <param name="layout">The layout of the vertex data.</param> public TransientVertexBuffer(int vertexCount, VertexLayout layout) { NativeMethods.bgfx_alloc_transient_vertex_buffer(out this, vertexCount, ref layout.data); }
public static extern void bgfx_alloc_transient_vertex_buffer(out TransientVertexBuffer tvb, int num, ref VertexLayout.Data decl);
/// <summary> /// Checks for available space to allocate transient index and vertex buffers. /// </summary> /// <param name="vertexCount">The number of vertices to allocate.</param> /// <param name="layout">The layout of each vertex.</param> /// <param name="indexCount">The number of indices to allocate.</param> /// <returns><c>true</c> if there is sufficient space for both vertex and index buffers.</returns> public static bool CheckAvailableTransientBufferSpace(int vertexCount, VertexLayout layout, int indexCount) { return(NativeMethods.bgfx_check_avail_transient_buffers(vertexCount, ref layout.data, indexCount)); }
public static extern void bgfx_vertex_decl_add(ref VertexLayout.Data decl, VertexAttributeUsage attribute, byte count, VertexAttributeType type, [MarshalAs(UnmanagedType.U1)] bool normalized, [MarshalAs(UnmanagedType.U1)] bool asInt);
/// <summary> /// Initializes a new instance of the <see cref="VertexBuffer"/> struct. /// </summary> /// <param name="memory">The vertex data with which to populate the buffer.</param> /// <param name="layout">The layout of the vertex data.</param> /// <param name="flags">Flags used to control buffer behavior.</param> public VertexBuffer(MemoryBlock memory, VertexLayout layout, BufferFlags flags = BufferFlags.None) { handle = NativeMethods.bgfx_create_vertex_buffer(memory.ptr, ref layout.data, flags); }
public static extern void bgfx_vertex_decl_end(ref VertexLayout.Data decl);
internal Mesh (VertexLayout decl, List<MeshGroup> groups) { vertexDecl = decl; this.groups = groups; }
public static extern void bgfx_vertex_pack(float* input, [MarshalAs(UnmanagedType.U1)] bool inputNormalized, VertexAttributeUsage attribute, ref VertexLayout.Data decl, IntPtr data, int index);
/// <summary> /// Attempts to allocate both a transient vertex buffer and index buffer. /// </summary> /// <param name="vertexCount">The number of vertices to allocate.</param> /// <param name="layout">The layout of each vertex.</param> /// <param name="indexCount">The number of indices to allocate.</param> /// <param name="vertexBuffer">Returns the allocated transient vertex buffer.</param> /// <param name="indexBuffer">Returns the allocated transient index buffer.</param> /// <returns><c>true</c> if both space requirements are satisfied and the buffers were allocated.</returns> public static bool AllocateTransientBuffers(int vertexCount, VertexLayout layout, int indexCount, out TransientVertexBuffer vertexBuffer, out TransientIndexBuffer indexBuffer) { return(NativeMethods.bgfx_alloc_transient_buffers(out vertexBuffer, ref layout.data, (ushort)vertexCount, out indexBuffer, (ushort)indexCount)); }
public static extern ushort bgfx_weld_vertices(ushort* output, ref VertexLayout.Data decl, IntPtr data, ushort num, float epsilon);
/// <summary> /// Packs a vector into vertex stream format. /// </summary> /// <param name="input">The four element vector to pack.</param> /// <param name="inputNormalized"><c>true</c> if the input vector is normalized.</param> /// <param name="attribute">The attribute usage of the vector data.</param> /// <param name="layout">The layout of the vertex stream.</param> /// <param name="data">The pointer to the vertex data stream.</param> /// <param name="index">The index of the vertex within the stream.</param> public static void VertexPack(float *input, bool inputNormalized, VertexAttributeUsage attribute, VertexLayout layout, IntPtr data, int index = 0) { NativeMethods.bgfx_vertex_pack(input, inputNormalized, attribute, ref layout.data, data, index); }
public static extern bool bgfx_check_avail_transient_vertex_buffer(int num, ref VertexLayout.Data decl);
/// <summary> /// Unpack a vector from a vertex stream. /// </summary> /// <param name="output">A pointer to four floats that will receive the unpacked vector.</param> /// <param name="attribute">The usage of the vertex attribute.</param> /// <param name="layout">The layout of the vertex stream.</param> /// <param name="data">A pointer to the vertex data stream.</param> /// <param name="index">The index of the vertex within the stream.</param> public static void VertexUnpack(float *output, VertexAttributeUsage attribute, VertexLayout layout, IntPtr data, int index = 0) { NativeMethods.bgfx_vertex_unpack(output, attribute, ref layout.data, data, index); }
public static extern ushort bgfx_create_dynamic_vertex_buffer_mem(MemoryBlock.DataPtr* memory, ref VertexLayout.Data decl, BufferFlags flags);
/// <summary> /// Initializes a new instance of the <see cref="DynamicVertexBuffer"/> struct. /// </summary> /// <param name="vertexCount">The number of vertices that fit in the buffer.</param> /// <param name="layout">The layout of the vertex data.</param> /// <param name="flags">Flags used to control buffer behavior.</param> public DynamicVertexBuffer(int vertexCount, VertexLayout layout, BufferFlags flags = BufferFlags.None) { handle = NativeMethods.bgfx_create_dynamic_vertex_buffer(vertexCount, ref layout.data, flags); }