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