MCPcopy Index your code
hub / github.com/ChartGPU/ChartGPU / prepare

Function prepare

src/renderers/createReferenceLineRenderer.ts:218–338  ·  view source on GitHub ↗
(gridArea, lines)

Source from the content-addressed store, hash-verified

216 };
217
218 const prepare: ReferenceLineRenderer['prepare'] = (gridArea, lines) => {
219 assertNotDisposed();
220
221 if (!Array.isArray(lines)) {
222 throw new Error('ReferenceLineRenderer.prepare: lines must be an array.');
223 }
224 if (!isFiniteGridArea(gridArea)) {
225 throw new Error('ReferenceLineRenderer.prepare: gridArea dimensions must be finite numbers.');
226 }
227 if (gridArea.canvasWidth <= 0 || gridArea.canvasHeight <= 0) {
228 throw new Error('ReferenceLineRenderer.prepare: canvas dimensions must be positive.');
229 }
230 if (gridArea.left < 0 || gridArea.right < 0 || gridArea.top < 0 || gridArea.bottom < 0) {
231 throw new Error('ReferenceLineRenderer.prepare: gridArea margins must be non-negative.');
232 }
233
234 // Be resilient: older call sites may omit/incorrectly pass DPR. Defaulting avoids hard crashes.
235 const dpr =
236 Number.isFinite(gridArea.devicePixelRatio) && gridArea.devicePixelRatio > 0 ? gridArea.devicePixelRatio : 1;
237
238 const plotLeftDevice = gridArea.left * dpr;
239 const plotTopDevice = gridArea.top * dpr;
240 const plotRightDevice = gridArea.canvasWidth - gridArea.right * dpr;
241 const plotBottomDevice = gridArea.canvasHeight - gridArea.bottom * dpr;
242 const plotWidthDevice = plotRightDevice - plotLeftDevice;
243 const plotHeightDevice = plotBottomDevice - plotTopDevice;
244
245 if (!(plotWidthDevice > 0) || !(plotHeightDevice > 0)) {
246 instanceCount = 0;
247 return;
248 }
249
250 // Write uniforms.
251 const uniforms = new Float32Array(8);
252 uniforms[0] = gridArea.canvasWidth;
253 uniforms[1] = gridArea.canvasHeight;
254 uniforms[2] = plotLeftDevice;
255 uniforms[3] = plotTopDevice;
256 uniforms[4] = plotWidthDevice;
257 uniforms[5] = plotHeightDevice;
258 uniforms[6] = dpr;
259 uniforms[7] = 0;
260 writeUniformBuffer(device, vsUniformBuffer, uniforms);
261
262 // Early out: no instances.
263 if (lines.length === 0) {
264 instanceCount = 0;
265 return;
266 }
267
268 // Ensure instance buffer capacity.
269 if (!instanceBuffer || instanceCapacity < lines.length) {
270 const nextCapacity = Math.max(1, Math.ceil(lines.length * 1.5));
271 const size = Math.max(4, nextCapacity * INSTANCE_STRIDE_BYTES);
272
273 if (instanceBuffer) {
274 try {
275 instanceBuffer.destroy();

Callers

nothing calls this directly

Calls 5

writeUniformBufferFunction · 0.90
normalizeDashFunction · 0.85
assertNotDisposedFunction · 0.70
isFiniteGridAreaFunction · 0.70
destroyMethod · 0.45

Tested by

no test coverage detected