コード例 #1
0
        public void SetIndices <T>(T[] indices, IndexFormat format, int stride, int elementOffset) where T : struct
        {
            _format = D3DFormats.ConvertIndexFormat(format);
            int elementSizeInBytes = Unsafe.SizeOf <T>();

            SetData(indices, elementSizeInBytes * indices.Length, elementOffset * elementSizeInBytes);
        }
コード例 #2
0
ファイル: OpenGLIndexBuffer.cs プロジェクト: GavinHwa/veldrid
        public void SetIndices <T>(T[] indices, IndexFormat format, int stride, int elementOffset) where T : struct
        {
            int elementSizeInBytes = Unsafe.SizeOf <T>();

            SetData(indices, elementSizeInBytes * indices.Length, elementOffset * elementSizeInBytes);
            ElementsType = OpenGLFormats.MapIndexFormat(format);
        }
コード例 #3
0
        public void SetIndices(IntPtr indices, IndexFormat format, int count, int elementOffset)
        {
            int elementSizeInBytes = format == IndexFormat.UInt16 ? sizeof(ushort) : sizeof(uint);

            SetData(indices, count * elementSizeInBytes, elementOffset * elementSizeInBytes);
            ElementsType = OpenGLFormats.MapIndexFormat(format);
        }
コード例 #4
0
ファイル: IndexBuffer.cs プロジェクト: K0bin/DotGame
        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);
            }
        }
コード例 #5
0
ファイル: ResourceFactory.cs プロジェクト: GavinHwa/veldrid
        /// <summary>
        /// Creates an <see cref="IndexBuffer"/> containing the given <see cref="System.Int32"/> index data.
        /// </summary>
        /// <param name="indices">The index data.</param>
        /// <param name="format">The format of the index data.</param>
        /// <param name="isDynamic">A value indicating whether or not the buffer should be optimized for dynamic access.</param>
        /// <returns>A new <see cref="IndexBuffer"/></returns>
        public IndexBuffer CreateIndexBuffer <T>(T[] indices, IndexFormat format, bool isDynamic) where T : struct
        {
            IndexBuffer ib = CreateIndexBuffer(sizeof(int) * indices.Length, isDynamic);

            ib.SetIndices(indices, format);
            return(ib);
        }
コード例 #6
0
        private int[] GetMeshTriangles(Mesh meshData, uint vertCount)
        {
            IndexFormat format = GetIndexFormat(meshData);

            int[] indices = new int[meshData.IndexCount];

            switch (format)
            {
                case IndexFormat.UInt16:
                    for (int i = 0; i < meshData.IndexCount; i++)
                    {
                        // We reverse the indices otherwise our normals will be inverted.
                        indices[meshData.IndexCount - 1 - i] = Convert.ToInt32(vertCount + BitConverter.ToUInt16(meshData.IndexData, i * 2));
                    }
                    break;
                case IndexFormat.UInt32:
                    for (int i = 0; i < meshData.IndexCount; i++)
                    {
                        // We reverse the indices otherwise our normals will be inverted.
                        indices[meshData.IndexCount - 1 - i] = Convert.ToInt32(vertCount + BitConverter.ToUInt32(meshData.IndexData, i * 4));
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return indices;
        }
コード例 #7
0
        public void SetIndices(IntPtr indices, IndexFormat format, int count, int elementOffset)
        {
            int elementSizeInBytes = FormatHelpers.GetIndexFormatElementByteSize(format);

            SetData(indices, elementSizeInBytes * count, elementOffset * elementSizeInBytes);
            IndexType = VkFormats.VeldridToVkIndexFormat(format);
        }
コード例 #8
0
 public ModelResources(DeviceBuffer vertexBuffer, DeviceBuffer indexBuffer, IndexFormat indexFormat, uint indexCount)
 {
     VertexBuffer = vertexBuffer;
     IndexBuffer  = indexBuffer;
     IndexFormat  = indexFormat;
     IndexCount   = indexCount;
 }
コード例 #9
0
        protected override void SetIndexBufferCore(Buffer buffer, IndexFormat format)
        {
            VkBuffer vkBuffer = Util.AssertSubtype <Buffer, VkBuffer>(buffer);

            vkCmdBindIndexBuffer(_cb, vkBuffer.DeviceBuffer, 0, VkFormats.VdToVkIndexFormat(format));
            _referencedResources.Add(vkBuffer);
        }
コード例 #10
0
ファイル: IndexBuffer.cs プロジェクト: K0bin/DotGame
        public IndexBuffer(GraphicsDevice graphicsDevice, IndexFormat format, ResourceUsage usage, int indexCount) : base(graphicsDevice, new System.Diagnostics.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.");
            }
            EnumConverter.Convert(Format); //Check whether format is supported

            this.Usage  = usage;
            this.Format = format;
            SizeBytes   = indexCount * graphicsDevice.GetSizeOf(format);

            IboID = GL.GenBuffer();

            if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
            {
                graphicsDevice.BindManager.IndexBuffer = this;
                GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(SizeBytes), IntPtr.Zero, EnumConverter.Convert(Usage));
            }
            else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
            {
                OpenTK.Graphics.OpenGL.GL.Ext.NamedBufferData(IboID, new IntPtr(SizeBytes), IntPtr.Zero, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.Convert(Usage));
            }

            graphicsDevice.CheckGLError("IndexBuffer Constructor");
        }
コード例 #11
0
 public static void SetIndexBuffer(IndexBuffer buffer, int offset, IndexFormat format)
 {
     Context.SetIndexBuffer(buffer?.GetPlatformBuffer(), offset, format);
     indexBuffer = buffer;
     indexOffset = offset;
     indexFormat = format;
 }
コード例 #12
0
        /// <summary>
        /// Deserializes the object and populates it from the input.
        /// </summary>
        /// <param name="input">Savable input</param>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying implementation fails or the render
        /// system is not set.</exception>
        public void Read(ISavableReader input)
        {
            IRenderSystemProvider renderSystem = input.RenderSystem;

            if (renderSystem == null)
            {
                Dispose();
                throw new TeslaException("Render system provider not set, cannot create graphics resource implementation.");
            }

            base.RenderSystem = renderSystem;
            String        name       = input.ReadString();
            int           indexCount = input.ReadInt();
            IndexFormat   format     = input.ReadEnum <IndexFormat>();
            ResourceUsage usage      = input.ReadEnum <ResourceUsage>();

            byte[] byteBuffer = input.ReadByteArray();

            try {
                _impl      = renderSystem.CreateIndexBufferImplementation(format, indexCount, usage);
                _impl.Name = name;
                SetData <byte>(byteBuffer);
            } catch (Exception e) {
                Dispose();
                throw new TeslaException("Error creating underlying implementation, see inner exception for details.", e);
            }
        }
コード例 #13
0
        /// <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);
            }
        }
コード例 #14
0
ファイル: IndexBuffer.cs プロジェクト: K0bin/DotGame
        public IndexBuffer(GraphicsDevice graphicsDevice, IndexFormat format, 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.");
            }
            EnumConverter.Convert(Format); //Check whether format is supported

            this.Usage  = usage;
            this.Format = format;

            IboID = GL.GenBuffer();

            if (!data.IsNull)
            {
                SizeBytes = data.Size;

                if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None)
                {
                    graphicsDevice.BindManager.IndexBuffer = this;
                    GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(SizeBytes), data.Pointer, EnumConverter.Convert(Usage));
                }
                else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension)
                {
                    OpenTK.Graphics.OpenGL.GL.Ext.NamedBufferData(IboID, new IntPtr(SizeBytes), data.Pointer, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.Convert(Usage));
                }
            }

            graphicsDevice.CheckGLError("IndexBuffer Constructor");
        }
コード例 #15
0
        /// <summary>
        /// Creates a new index buffer implementation.
        /// </summary>
        /// <param name="format">Index format, 16 or 32 bits.</param>
        /// <param name="indexCount">Number of indices</param>
        /// <param name="usage">Resource usage</param>
        /// <returns>
        /// Index buffer implementation
        /// </returns>
        public IndexBufferImplementation CreateIndexBufferImplementation(IndexFormat format, int indexCount, ResourceUsage usage)
        {
            D3D10IndexBufferImplementation impl = new D3D10IndexBufferImplementation(_renderer, format, indexCount, usage);

            SetGraphicsID(impl);
            return(impl);
        }
コード例 #16
0
 public override GraphicBuffer CreateIndexBuffer(int size,
                                                 IndexFormat format      = IndexFormat.Index16,
                                                 ResourceUsage usage     = ResourceUsage.Default,
                                                 CpuAccessFlags cpuAcces = CpuAccessFlags.ReadWrite,
                                                 IntPtr data             = default(IntPtr))
 {
     return(new ESGraphicBuffer(size, format == IndexFormat.Index16?2:4, All.ElementArrayBuffer, usage, cpuAcces, ResBinding.IndexBuffer, data));
 }
コード例 #17
0
        public void SetIndices(IntPtr indices, IndexFormat format, int count, int elementOffset)
        {
            int elementSizeInBytes = format == IndexFormat.UInt16 ? sizeof(ushort) : sizeof(uint);

            SetData(indices, elementSizeInBytes * count, elementSizeInBytes * elementOffset);
            SharpDX.DXGI.Format dxgiFormat = D3DFormats.VeldridToD3DIndexFormat(format);
            _format = dxgiFormat;
        }
コード例 #18
0
 protected override void SetIndexBufferCore(DeviceBuffer buffer, IndexFormat format)
 {
     if (_ib != buffer)
     {
         _ib = buffer;
         D3D11Buffer d3d11Buffer = Util.AssertSubtype <DeviceBuffer, D3D11Buffer>(buffer);
         _context.InputAssembler.SetIndexBuffer(d3d11Buffer.Buffer, D3D11Formats.ToDxgiFormat(format), 0);
     }
 }
コード例 #19
0
ファイル: GLWrapper.cs プロジェクト: Suceru/sc2.2mobile
#pragma warning disable CS0246 // 未能找到类型或命名空间名“All”(是否缺少 using 指令或程序集引用?)
        public static All TranslateIndexFormat(IndexFormat indexFormat)
#pragma warning restore CS0246 // 未能找到类型或命名空间名“All”(是否缺少 using 指令或程序集引用?)
        {
            if (indexFormat == IndexFormat.SixteenBits)
            {
                return(All.UnsignedShort);
            }
            throw new InvalidOperationException("Unsupported index format.");
        }
コード例 #20
0
 public void InitializeIndexBuffer(IndexFormat indexFormat, int indicesCount)
 {
     if (indicesCount <= 0)
     {
         throw new ArgumentException("Indices count must be greater than 0.");
     }
     IndexFormat  = indexFormat;
     IndicesCount = indicesCount;
 }
コード例 #21
0
ファイル: EnumConverter.cs プロジェクト: K0bin/DotGame
        internal static DrawElementsType Convert(IndexFormat indexFormat)
        {
            if (!indexFormats.ContainsKey(indexFormat))
            {
                throw new NotSupportedException("indexFormat is not supported");
            }

            return(indexFormats[indexFormat]);
        }
コード例 #22
0
        public static List <Mesh> ConvertTerrain2Mesh(Terrain terrain, IndexFormat format, int splitCount = 0, int lod = 0)
        {
            var terrainData = terrain.terrainData;
            var width       = GetTerrainResolution(terrainData, format, lod, splitCount);
            var height      = width;
            var size        = terrainData.size;

            var tileGridNum = (int)Mathf.Pow(2, splitCount);
            var maxWidth    = (width - 1) * tileGridNum + 1;
            var maxHeight   = maxWidth;
            var meshScale   = new Vector3(size.x / (width - 1) / tileGridNum, 1, size.z / (height - 1) / tileGridNum);

            // The function returns a two-dimensional array of size [yCount, xCount]
            var heights = terrainData.GetInterpolatedHeights(0, 0, maxWidth, maxHeight, 1f / (maxWidth - 1), 1f / (maxHeight - 1));

            var meshes = new List <Mesh>(tileGridNum * tileGridNum);

            for (int chunkI = 0; chunkI < tileGridNum; chunkI++)
            {
                for (int chunkJ = 0; chunkJ < tileGridNum; chunkJ++)
                {
                    var meshData = new QuadMeshData(width, height);

                    // 左下角开始,从下往上,从左往右添加顶点
                    for (int i = 0; i < width; i++)
                    {
                        for (int j = 0; j < height; j++)
                        {
                            // 共用一条边
                            var widthIndex  = chunkI * (width - 1) + i;
                            var heightIndex = chunkJ * (height - 1) + j;
                            // meshData.AddVertex(i, j, heights[heightIndex, widthIndex], meshScale);
                            meshData.AddVertex(i, j, heights[heightIndex, widthIndex], meshScale, new Vector2(widthIndex / (maxWidth - 1f), heightIndex / (maxHeight - 1f)));
                        }
                    }

                    // 正方形顺序随顶点顺序,三角形顺时针方向为模型的正面
                    for (int i = 0; i < width - 1; i++)
                    {
                        for (int j = 0; j < height - 1; j++)
                        {
                            var a = i + j * width;
                            var b = i + (j + 1) * width;
                            var c = (i + 1) + (j + 1) * width;
                            var d = (i + 1) + j * width;

                            meshData.AddTriangle(a, b, c);
                            meshData.AddTriangle(a, c, d);
                        }
                    }

                    meshes.Add(meshData.CreateMesh(format));
                }
            }

            return(meshes);
        }
コード例 #23
0
        public GraphicBuffer CreateIndexBuffer <T>(ResourceUsage usage = ResourceUsage.Default, CpuAccessFlags cpuAcces = CpuAccessFlags.ReadWrite, T[] data = null)
            where T : struct
        {
            int         stride = ClrRuntime.Runtime.SizeOf <T>();
            int         size   = data.Length * stride;
            IndexFormat format = stride == 2 ? IndexFormat.Index16 : IndexFormat.Index32;

            return(CreateIndexBuffer(size, data, format, usage, cpuAcces));
        }
コード例 #24
0
 /// <summary>
 /// Creates a new instance of <see cref="IndexBufferImplementation"/>.
 /// </summary>
 /// <param name="indices">The index data.</param>
 /// <param name="usage">The resource usage specifying the type of memory the buffer should use.</param>
 /// <exception cref="System.ArgumentNullException">Thrown if the index data is null.</exception>
 protected IndexBufferImplementation(DataBuffer <short> indices, ResourceUsage usage)
 {
     if (indices == null)
     {
         throw new ArgumentNullException("indices", "Indices cannot be null.");
     }
     _format      = IndexFormat.SixteenBits;
     _indexCount  = indices.Length;
     _bufferUsage = usage;
 }
コード例 #25
0
        public static Format Convert(IndexFormat format)
        {
            Format f;

            if (!indexFormats.TryGetValue(format, out f))
            {
                throw new NotSupportedException("Format is not supported.");
            }
            return(f);
        }
コード例 #26
0
 public void SetIndexBuffer(IPlatformBuffer buffer, int offset, IndexFormat format)
 {
     if (indexBuffer != buffer)
     {
         indexBuffer      = (PlatformBuffer)buffer;
         indexBufferDirty = true;
     }
     indexOffset = offset;
     indexFormat = format;
 }
コード例 #27
0
        /// <summary>
        /// Create a mesh with the given name and buffer format. The mesh is marked dynamic for
        /// procedural modifications.
        /// </summary>
        /// <param name="meshName"></param>
        /// <param name="bufferFormat"></param>
        /// <returns></returns>
        public static Mesh CreateMeshForProceduralModifications(string meshName, IndexFormat bufferFormat)
        {
            Mesh mesh = new Mesh();

            mesh.name        = meshName;
            mesh.indexFormat = bufferFormat;
            mesh.MarkDynamic();

            return(mesh);
        }
コード例 #28
0
        public static int GetElementSize(this IndexFormat indexFormat)
        {
            switch (indexFormat)
            {
            case IndexFormat.Index16Bits:
                return(2);

            default:
                throw new NotSupportedException();
            }
        }
コード例 #29
0
ファイル: GLExtensions.cs プロジェクト: aologos/Citrus
        public static DrawElementsType ToGLDrawElementsType(this IndexFormat value)
        {
            switch (value)
            {
            case IndexFormat.Index16Bits:
                return(DrawElementsType.UnsignedShort);

            default:
                throw new NotSupportedException();
            }
        }
コード例 #30
0
        /// <summary>
        /// Sets the active <see cref="Buffer"/>.
        /// When drawing, an <see cref="Buffer"/> must be bound.
        /// </summary>
        /// <param name="buffer">The new <see cref="Buffer"/>.</param>
        /// <param name="format">The format of data in the <see cref="Buffer"/>.</param>
        public void SetIndexBuffer(Buffer buffer, IndexFormat format)
        {
#if VALIDATE_USAGE
            if ((buffer.Usage & BufferUsage.IndexBuffer) == 0)
            {
                throw new VeldridException(
                          $"Buffer cannot be bound as an index buffer because it was not created with BufferUsage.IndexBuffer.");
            }
#endif
            SetIndexBufferCore(buffer, format);
        }
コード例 #31
0
ファイル: CtObjectGL.cs プロジェクト: Zulkir/Beholder
 public static DrawElementsType DrawElementsType(IndexFormat bIndexFormat)
 {
     switch (bIndexFormat)
     {
         case IndexFormat.SixteenBit: return OpenTK.Graphics.OpenGL.DrawElementsType.UnsignedShort;
         case IndexFormat.ThirtyTwoBit: return OpenTK.Graphics.OpenGL.DrawElementsType.UnsignedInt;
         default: throw new ArgumentOutOfRangeException("bIndexFormat");
     }
 }
コード例 #32
0
ファイル: IndexSource.cs プロジェクト: Zulkir/Beholder
 public IndexSource(IBuffer buffer, int offset, IndexFormat format)
 {
     Buffer = buffer;
     Offset = offset;
     Format = format;
 }
コード例 #33
0
ファイル: SfntTables.cs プロジェクト: Mofangbao/CSharpGL
        public static void ReadLoca(DataReader reader, TableRecord[] tables, IndexFormat format, uint* table, int count)
        {
            SeekToTable(reader, tables, FourCC.Loca, required: true);

            if (format == IndexFormat.Short)
            {
                // values are ushort, divided by 2, so we need to shift back
                for (int i = 0; i < count; i++)
                    *table++ = (uint)(reader.ReadUInt16BE() << 1);
            }
            else
            {
                for (int i = 0; i < count; i++)
                    *table++ = reader.ReadUInt32BE();
            }
        }