• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // VertexArrayMtl.h:
7 //    Defines the class interface for VertexArrayMtl, implementing VertexArrayImpl.
8 //
9 
10 #ifndef LIBANGLE_RENDERER_METAL_VERTEXARRAYMTL_H_
11 #define LIBANGLE_RENDERER_METAL_VERTEXARRAYMTL_H_
12 
13 #include "libANGLE/renderer/VertexArrayImpl.h"
14 #include "libANGLE/renderer/metal/BufferMtl.h"
15 #include "libANGLE/renderer/metal/mtl_buffer_pool.h"
16 #include "libANGLE/renderer/metal/mtl_command_buffer.h"
17 #include "libANGLE/renderer/metal/mtl_context_device.h"
18 #include "libANGLE/renderer/metal/mtl_format_utils.h"
19 #include "libANGLE/renderer/metal/mtl_resources.h"
20 
21 namespace rx
22 {
23 class ContextMtl;
24 
25 class VertexArrayMtl : public VertexArrayImpl
26 {
27   public:
28     VertexArrayMtl(const gl::VertexArrayState &state, ContextMtl *context);
29     ~VertexArrayMtl() override;
30 
31     void destroy(const gl::Context *context) override;
32 
33     angle::Result syncState(const gl::Context *context,
34                             const gl::VertexArray::DirtyBits &dirtyBits,
35                             gl::VertexArray::DirtyAttribBitsArray *attribBits,
36                             gl::VertexArray::DirtyBindingBitsArray *bindingBits) override;
37 
38     // Feed client side's vertex/index data
39     angle::Result updateClientAttribs(const gl::Context *context,
40                                       GLint firstVertex,
41                                       GLsizei vertexOrIndexCount,
42                                       GLsizei instanceCount,
43                                       gl::DrawElementsType indexTypeOrInvalid,
44                                       const void *indices);
45 
46     // vertexDescChanged is both input and output, the input value if is true, will force new
47     // mtl::VertexDesc to be returned via vertexDescOut. This typically happens when active shader
48     // program is changed.
49     // Otherwise, it is only returned when the vertex array is dirty.
50     angle::Result setupDraw(const gl::Context *glContext,
51                             mtl::RenderCommandEncoder *cmdEncoder,
52                             bool *vertexDescChanged,
53                             mtl::VertexDesc *vertexDescOut);
54 
55     angle::Result getIndexBuffer(const gl::Context *glContext,
56                                  gl::DrawElementsType indexType,
57                                  size_t indexCount,
58                                  const void *sourcePointer,
59                                  mtl::BufferRef *idxBufferOut,
60                                  size_t *idxBufferOffsetOut,
61                                  gl::DrawElementsType *indexTypeOut);
62 
63     std::vector<DrawCommandRange> getDrawIndices(const gl::Context *glContext,
64                                                  gl::DrawElementsType originalIndexType,
65                                                  gl::DrawElementsType indexType,
66                                                  gl::PrimitiveMode primitiveMode,
67                                                  mtl::BufferRef idxBuffer,
68                                                  uint32_t indexCount,
69                                                  size_t offset);
70 
71   private:
72     void reset(ContextMtl *context);
73 
74     void getVertexAttribFormatAndArraySize(const sh::ShaderVariable &var,
75                                            MTLVertexFormat *formatOut,
76                                            uint32_t *arraySizeOut);
77 
78     angle::Result syncDirtyAttrib(const gl::Context *glContext,
79                                   const gl::VertexAttribute &attrib,
80                                   const gl::VertexBinding &binding,
81                                   size_t attribIndex);
82 
83     angle::Result convertIndexBuffer(const gl::Context *glContext,
84                                      gl::DrawElementsType indexType,
85                                      size_t offset,
86                                      mtl::BufferRef *idxBufferOut,
87                                      size_t *idxBufferOffsetOut);
88     angle::Result streamIndexBufferFromClient(const gl::Context *glContext,
89                                               gl::DrawElementsType indexType,
90                                               size_t indexCount,
91                                               const void *sourcePointer,
92                                               mtl::BufferRef *idxBufferOut,
93                                               size_t *idxBufferOffsetOut);
94 
95     angle::Result convertIndexBufferGPU(const gl::Context *glContext,
96                                         gl::DrawElementsType indexType,
97                                         BufferMtl *idxBuffer,
98                                         size_t offset,
99                                         size_t indexCount,
100                                         IndexConversionBufferMtl *conversion);
101 
102     angle::Result convertVertexBuffer(const gl::Context *glContext,
103                                       BufferMtl *srcBuffer,
104                                       const gl::VertexBinding &binding,
105                                       size_t attribIndex,
106                                       const mtl::VertexFormat &vertexFormat);
107 
108     angle::Result convertVertexBufferCPU(ContextMtl *contextMtl,
109                                          BufferMtl *srcBuffer,
110                                          const gl::VertexBinding &binding,
111                                          size_t attribIndex,
112                                          const mtl::VertexFormat &convertedFormat,
113                                          GLuint targetStride,
114                                          size_t vertexCount,
115                                          ConversionBufferMtl *conversion);
116     angle::Result convertVertexBufferGPU(const gl::Context *glContext,
117                                          BufferMtl *srcBuffer,
118                                          const gl::VertexBinding &binding,
119                                          size_t attribIndex,
120                                          const mtl::VertexFormat &convertedFormat,
121                                          GLuint targetStride,
122                                          size_t vertexCount,
123                                          bool isExpandingComponents,
124                                          ConversionBufferMtl *conversion);
125 
126     // These can point to real BufferMtl or converted buffer in mConvertedArrayBufferHolders
127     gl::AttribArray<BufferHolderMtl *> mCurrentArrayBuffers;
128     gl::AttribArray<SimpleWeakBufferHolderMtl> mConvertedArrayBufferHolders;
129     gl::AttribArray<size_t> mCurrentArrayBufferOffsets;
130 
131     // Size to be uploaded as inline constant data. Used for client vertex attribute's data that
132     // is small enough that we can send directly as inline constant data instead of streaming
133     // through a buffer.
134     gl::AttribArray<size_t> mCurrentArrayInlineDataSizes;
135     // Array of host buffers storing converted data for client attributes that are small enough.
136     gl::AttribArray<angle::MemoryBuffer> mConvertedClientSmallArrays;
137     gl::AttribArray<const uint8_t *> mCurrentArrayInlineDataPointers;
138     // Max size of inline constant data that can be used for client vertex attribute.
139     size_t mInlineDataMaxSize;
140 
141     // Stride per vertex attribute
142     gl::AttribArray<GLuint> mCurrentArrayBufferStrides;
143     // Format per vertex attribute
144     gl::AttribArray<const mtl::VertexFormat *> mCurrentArrayBufferFormats;
145 
146     const mtl::VertexFormat &mDefaultFloatVertexFormat;
147     const mtl::VertexFormat &mDefaultIntVertexFormat;
148     const mtl::VertexFormat &mDefaultUIntVertexFormat;
149 
150     mtl::BufferPool mDynamicVertexData;
151     mtl::BufferPool mDynamicIndexData;
152 
153     std::vector<uint32_t> mEmulatedInstanceAttribs;
154 
155     bool mVertexArrayDirty = true;
156     bool mVertexDataDirty  = true;
157 };
158 }  // namespace rx
159 
160 #endif /* LIBANGLE_RENDERER_METAL_VERTEXARRAYMTL_H_ */
161