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