UNPKG

11.3 kBTypeScriptView Raw
1import { ViewableBuffer } from '../geometry/ViewableBuffer';
2import { State } from '../state/State';
3import { BaseTexture } from '../textures/BaseTexture';
4import { BatchDrawCall } from './BatchDrawCall';
5import { BatchGeometry } from './BatchGeometry';
6import { BatchShaderGenerator } from './BatchShaderGenerator';
7import { BatchTextureArray } from './BatchTextureArray';
8import { ObjectRenderer } from './ObjectRenderer';
9import type { BLEND_MODES } from '@pixi/constants';
10import type { ExtensionMetadata } from '@pixi/extensions';
11import type { Renderer } from '../Renderer';
12import type { Shader } from '../shader/Shader';
13import type { Texture } from '../textures/Texture';
14/**
15 * Interface for elements like Sprite, Mesh etc. for batching.
16 * @memberof PIXI
17 */
18export interface IBatchableElement {
19 _texture: Texture;
20 vertexData: Float32Array;
21 indices: Uint16Array | Uint32Array | Array<number>;
22 uvs: Float32Array;
23 worldAlpha: number;
24 _tintRGB: number;
25 blendMode: BLEND_MODES;
26}
27/**
28 * Renderer dedicated to drawing and batching sprites.
29 *
30 * This is the default batch renderer. It buffers objects
31 * with texture-based geometries and renders them in
32 * batches. It uploads multiple textures to the GPU to
33 * reduce to the number of draw calls.
34 * @memberof PIXI
35 */
36export declare class BatchRenderer extends ObjectRenderer {
37 /**
38 * The maximum textures that this device supports.
39 * @static
40 * @default 32
41 */
42 static get defaultMaxTextures(): number;
43 static set defaultMaxTextures(value: number);
44 /** @ignore */
45 private static _defaultMaxTextures;
46 /**
47 * The default sprite batch size.
48 *
49 * The default aims to balance desktop and mobile devices.
50 * @static
51 */
52 static defaultBatchSize: number;
53 /**
54 * Can we upload the same buffer in a single frame?
55 * @static
56 */
57 static get canUploadSameBuffer(): boolean;
58 static set canUploadSameBuffer(value: boolean);
59 /** @ignore */
60 private static _canUploadSameBuffer;
61 /** @ignore */
62 static extension: ExtensionMetadata;
63 /** The WebGL state in which this renderer will work. */
64 readonly state: State;
65 /**
66 * The number of bufferable objects before a flush
67 * occurs automatically.
68 * @default PIXI.BatchRenderer.defaultBatchSize * 4
69 */
70 size: number;
71 /**
72 * Maximum number of textures that can be uploaded to
73 * the GPU under the current context. It is initialized
74 * properly in `this.contextChange`.
75 * @see PIXI.BatchRenderer#contextChange
76 * @readonly
77 */
78 maxTextures: number;
79 /**
80 * This is used to generate a shader that can
81 * color each vertex based on a `aTextureId`
82 * attribute that points to an texture in `uSampler`.
83 *
84 * This enables the objects with different textures
85 * to be drawn in the same draw call.
86 *
87 * You can customize your shader by creating your
88 * custom shader generator.
89 */
90 protected shaderGenerator: BatchShaderGenerator;
91 /**
92 * The class that represents the geometry of objects
93 * that are going to be batched with this.
94 * @member {object}
95 * @default PIXI.BatchGeometry
96 */
97 protected geometryClass: typeof BatchGeometry;
98 /**
99 * Size of data being buffered per vertex in the
100 * attribute buffers (in floats). By default, the
101 * batch-renderer plugin uses 6:
102 *
103 * | aVertexPosition | 2 |
104 * |-----------------|---|
105 * | aTextureCoords | 2 |
106 * | aColor | 1 |
107 * | aTextureId | 1 |
108 * @default 6
109 */
110 protected vertexSize: number;
111 /** Total count of all vertices used by the currently buffered objects. */
112 protected _vertexCount: number;
113 /** Total count of all indices used by the currently buffered objects. */
114 protected _indexCount: number;
115 /**
116 * Buffer of objects that are yet to be rendered.
117 * @member {PIXI.DisplayObject[]}
118 */
119 protected _bufferedElements: Array<IBatchableElement>;
120 /**
121 * Data for texture batch builder, helps to save a bit of CPU on a pass.
122 * @member {PIXI.BaseTexture[]}
123 */
124 protected _bufferedTextures: Array<BaseTexture>;
125 /** Number of elements that are buffered and are waiting to be flushed. */
126 protected _bufferSize: number;
127 /**
128 * This shader is generated by `this.shaderGenerator`.
129 *
130 * It is generated specifically to handle the required
131 * number of textures being batched together.
132 */
133 protected _shader: Shader;
134 /**
135 * A flush may occur multiple times in a single
136 * frame. On iOS devices or when
137 * `BatchRenderer.canUploadSameBuffer` is false, the
138 * batch renderer does not upload data to the same
139 * `WebGLBuffer` for performance reasons.
140 *
141 * This is the index into `packedGeometries` that points to
142 * geometry holding the most recent buffers.
143 */
144 protected _flushId: number;
145 /**
146 * Pool of `ViewableBuffer` objects that are sorted in
147 * order of increasing size. The flush method uses
148 * the buffer with the least size above the amount
149 * it requires. These are used for passing attributes.
150 *
151 * The first buffer has a size of 8; each subsequent
152 * buffer has double capacity of its previous.
153 * @member {PIXI.ViewableBuffer[]}
154 * @see PIXI.BatchRenderer#getAttributeBuffer
155 */
156 protected _aBuffers: Array<ViewableBuffer>;
157 /**
158 * Pool of `Uint16Array` objects that are sorted in
159 * order of increasing size. The flush method uses
160 * the buffer with the least size above the amount
161 * it requires. These are used for passing indices.
162 *
163 * The first buffer has a size of 12; each subsequent
164 * buffer has double capacity of its previous.
165 * @member {Uint16Array[]}
166 * @see PIXI.BatchRenderer#getIndexBuffer
167 */
168 protected _iBuffers: Array<Uint16Array>;
169 protected _dcIndex: number;
170 protected _aIndex: number;
171 protected _iIndex: number;
172 protected _attributeBuffer: ViewableBuffer;
173 protected _indexBuffer: Uint16Array;
174 protected _tempBoundTextures: BaseTexture[];
175 /**
176 * Pool of `this.geometryClass` geometry objects
177 * that store buffers. They are used to pass data
178 * to the shader on each draw call.
179 *
180 * These are never re-allocated again, unless a
181 * context change occurs; however, the pool may
182 * be expanded if required.
183 * @member {PIXI.Geometry[]}
184 * @see PIXI.BatchRenderer.contextChange
185 */
186 private _packedGeometries;
187 /**
188 * Size of `this._packedGeometries`. It can be expanded
189 * if more than `this._packedGeometryPoolSize` flushes
190 * occur in a single frame.
191 */
192 private _packedGeometryPoolSize;
193 /**
194 * This will hook onto the renderer's `contextChange`
195 * and `prerender` signals.
196 * @param {PIXI.Renderer} renderer - The renderer this works for.
197 */
198 constructor(renderer: Renderer);
199 /**
200 * @see PIXI.BatchRenderer#maxTextures
201 * @deprecated since 7.1.0
202 * @readonly
203 */
204 get MAX_TEXTURES(): number;
205 /**
206 * The default vertex shader source
207 * @readonly
208 */
209 static get defaultVertexSrc(): string;
210 /**
211 * The default fragment shader source
212 * @readonly
213 */
214 static get defaultFragmentTemplate(): string;
215 /**
216 * Set the shader generator.
217 * @param {object} [options]
218 * @param {string} [options.vertex=PIXI.BatchRenderer.defaultVertexSrc] - Vertex shader source
219 * @param {string} [options.fragment=PIXI.BatchRenderer.defaultFragmentTemplate] - Fragment shader template
220 */
221 setShaderGenerator({ vertex, fragment }?: {
222 vertex?: string;
223 fragment?: string;
224 }): void;
225 /**
226 * Handles the `contextChange` signal.
227 *
228 * It calculates `this.maxTextures` and allocating the packed-geometry object pool.
229 */
230 contextChange(): void;
231 /** Makes sure that static and dynamic flush pooled objects have correct dimensions. */
232 initFlushBuffers(): void;
233 /** Handles the `prerender` signal. It ensures that flushes start from the first geometry object again. */
234 onPrerender(): void;
235 /**
236 * Buffers the "batchable" object. It need not be rendered immediately.
237 * @param {PIXI.DisplayObject} element - the element to render when
238 * using this renderer
239 */
240 render(element: IBatchableElement): void;
241 buildTexturesAndDrawCalls(): void;
242 /**
243 * Populating drawcalls for rendering
244 * @param texArray
245 * @param start
246 * @param finish
247 */
248 buildDrawCalls(texArray: BatchTextureArray, start: number, finish: number): void;
249 /**
250 * Bind textures for current rendering
251 * @param texArray
252 */
253 bindAndClearTexArray(texArray: BatchTextureArray): void;
254 updateGeometry(): void;
255 drawBatches(): void;
256 /** Renders the content _now_ and empties the current batch. */
257 flush(): void;
258 /** Starts a new sprite batch. */
259 start(): void;
260 /** Stops and flushes the current batch. */
261 stop(): void;
262 /** Destroys this `BatchRenderer`. It cannot be used again. */
263 destroy(): void;
264 /**
265 * Fetches an attribute buffer from `this._aBuffers` that can hold atleast `size` floats.
266 * @param size - minimum capacity required
267 * @returns - buffer than can hold atleast `size` floats
268 */
269 getAttributeBuffer(size: number): ViewableBuffer;
270 /**
271 * Fetches an index buffer from `this._iBuffers` that can
272 * have at least `size` capacity.
273 * @param size - minimum required capacity
274 * @returns - buffer that can fit `size` indices.
275 */
276 getIndexBuffer(size: number): Uint16Array;
277 /**
278 * Takes the four batching parameters of `element`, interleaves
279 * and pushes them into the batching attribute/index buffers given.
280 *
281 * It uses these properties: `vertexData` `uvs`, `textureId` and
282 * `indicies`. It also uses the "tint" of the base-texture, if
283 * present.
284 * @param {PIXI.DisplayObject} element - element being rendered
285 * @param attributeBuffer - attribute buffer.
286 * @param indexBuffer - index buffer
287 * @param aIndex - number of floats already in the attribute buffer
288 * @param iIndex - number of indices already in `indexBuffer`
289 */
290 packInterleavedGeometry(element: IBatchableElement, attributeBuffer: ViewableBuffer, indexBuffer: Uint16Array, aIndex: number, iIndex: number): void;
291 /**
292 * Pool of `BatchDrawCall` objects that `flush` used
293 * to create "batches" of the objects being rendered.
294 *
295 * These are never re-allocated again.
296 * Shared between all batch renderers because it can be only one "flush" working at the moment.
297 * @member {PIXI.BatchDrawCall[]}
298 */
299 static _drawCallPool: Array<BatchDrawCall>;
300 /**
301 * Pool of `BatchDrawCall` objects that `flush` used
302 * to create "batches" of the objects being rendered.
303 *
304 * These are never re-allocated again.
305 * Shared between all batch renderers because it can be only one "flush" working at the moment.
306 * @member {PIXI.BatchTextureArray[]}
307 */
308 static _textureArrayPool: Array<BatchTextureArray>;
309}