@blotch/angular-lynx-native 22.2.8 → 22.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2220 @@
1
+ #import "AngularLynxWebGL.h"
2
+
3
+ #import <OpenGLES/ES3/gl.h>
4
+ #import <OpenGLES/ES3/glext.h>
5
+ #import <QuartzCore/QuartzCore.h>
6
+
7
+ #import "AngularLynxWebGLOps.h"
8
+
9
+ NSData *AngularLynxBytesFromWords(NSArray *words);
10
+
11
+ // WebGL's names for the default framebuffer's buffers, which GLES gives to
12
+ // window surfaces only. The default framebuffer here is an ordinary one.
13
+ #define WEBGL_BACK 0x0405
14
+ #define WEBGL_COLOR 0x1800
15
+ #define WEBGL_DEPTH 0x1801
16
+ #define WEBGL_STENCIL 0x1802
17
+ #define WEBGL_UNMASKED_VENDOR 0x9245
18
+ #define WEBGL_UNMASKED_RENDERER 0x9246
19
+ #define GL_TEXTURE_MAX_ANISOTROPY 0x84FE
20
+ #define GL_MAX_TEXTURE_MAX_ANISOTROPY 0x84FF
21
+
22
+ /// Reads a batch: the commands' arguments, in order.
23
+ typedef struct {
24
+ __unsafe_unretained id *items;
25
+ NSUInteger count;
26
+ NSUInteger index;
27
+ } Reader;
28
+
29
+ static inline id nextItem(Reader *r) {
30
+ return r->index < r->count ? r->items[r->index++] : nil;
31
+ }
32
+
33
+ static inline double nextNumber(Reader *r) {
34
+ id value = nextItem(r);
35
+ return [value isKindOfClass:NSNumber.class] ? [(NSNumber *)value doubleValue] : 0;
36
+ }
37
+
38
+ static inline GLint nextInt(Reader *r) { return (GLint)(int64_t)nextNumber(r); }
39
+ static inline GLuint nextUInt(Reader *r) { return (GLuint)(int64_t)nextNumber(r); }
40
+ static inline GLenum nextEnum(Reader *r) { return (GLenum)(int64_t)nextNumber(r); }
41
+ static inline GLfloat nextFloat(Reader *r) { return (GLfloat)nextNumber(r); }
42
+ static inline GLintptr nextOffset(Reader *r) { return (GLintptr)(int64_t)nextNumber(r); }
43
+
44
+ static inline NSString *nextString(Reader *r) {
45
+ id value = nextItem(r);
46
+ return [value isKindOfClass:NSString.class] ? value : @"";
47
+ }
48
+
49
+ static inline NSArray *nextArray(Reader *r) {
50
+ id value = nextItem(r);
51
+ return [value isKindOfClass:NSArray.class] ? value : @[];
52
+ }
53
+
54
+ static NSMutableData *floatsFrom(NSArray *values) {
55
+ NSMutableData *data = [NSMutableData dataWithLength:values.count * sizeof(GLfloat)];
56
+ GLfloat *out = data.mutableBytes;
57
+ NSUInteger i = 0;
58
+ for (NSNumber *value in values) out[i++] = value.floatValue;
59
+ return data;
60
+ }
61
+
62
+ static NSMutableData *intsFrom(NSArray *values) {
63
+ NSMutableData *data = [NSMutableData dataWithLength:values.count * sizeof(GLint)];
64
+ GLint *out = data.mutableBytes;
65
+ NSUInteger i = 0;
66
+ for (NSNumber *value in values) out[i++] = (GLint)value.longLongValue;
67
+ return data;
68
+ }
69
+
70
+ static NSMutableData *uintsFrom(NSArray *values) {
71
+ NSMutableData *data = [NSMutableData dataWithLength:values.count * sizeof(GLuint)];
72
+ GLuint *out = data.mutableBytes;
73
+ NSUInteger i = 0;
74
+ for (NSNumber *value in values) out[i++] = (GLuint)value.longLongValue;
75
+ return data;
76
+ }
77
+
78
+ static NSArray *numbersFromFloats(const GLfloat *values, NSUInteger count) {
79
+ NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
80
+ for (NSUInteger i = 0; i < count; i++) [result addObject:@(values[i])];
81
+ return result;
82
+ }
83
+
84
+ static NSArray *numbersFromInts(const GLint *values, NSUInteger count) {
85
+ NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
86
+ for (NSUInteger i = 0; i < count; i++) [result addObject:@(values[i])];
87
+ return result;
88
+ }
89
+
90
+ /// Bytes as words for the trip back, the byte count last (see fromWords in
91
+ /// webgl2-context.ts).
92
+ static NSArray *wordsFromBytes(const void *bytes, NSUInteger length) {
93
+ NSUInteger wordCount = (length + 3) / 4;
94
+ NSMutableArray *words = [NSMutableArray arrayWithCapacity:wordCount + 1];
95
+ uint32_t word;
96
+ const uint8_t *p = bytes;
97
+ for (NSUInteger i = 0; i < wordCount; i++) {
98
+ word = 0;
99
+ memcpy(&word, p + i * 4, MIN(4, length - i * 4));
100
+ [words addObject:@(word)];
101
+ }
102
+ [words addObject:@(length)];
103
+ return words;
104
+ }
105
+
106
+ static NSString *glString(GLenum name) {
107
+ const GLubyte *value = glGetString(name);
108
+ return value ? [NSString stringWithUTF8String:(const char *)value] : @"";
109
+ }
110
+
111
+ /// Half floats for HALF_FLOAT uploads of decoded images.
112
+ static uint16_t halfFromFloat(float value) {
113
+ uint32_t bits;
114
+ memcpy(&bits, &value, 4);
115
+ uint32_t sign = (bits >> 16) & 0x8000;
116
+ int32_t exponent = (int32_t)((bits >> 23) & 0xff) - 127 + 15;
117
+ uint32_t mantissa = bits & 0x7fffff;
118
+ if (exponent <= 0) return (uint16_t)sign;
119
+ if (exponent >= 31) return (uint16_t)(sign | 0x7c00);
120
+ return (uint16_t)(sign | (exponent << 10) | (mantissa >> 13));
121
+ }
122
+
123
+ static NSUInteger componentsOf(GLenum format) {
124
+ switch (format) {
125
+ case GL_RGBA:
126
+ case GL_RGBA_INTEGER:
127
+ return 4;
128
+ case GL_RGB:
129
+ case GL_RGB_INTEGER:
130
+ return 3;
131
+ case GL_RG:
132
+ case GL_RG_INTEGER:
133
+ case GL_LUMINANCE_ALPHA:
134
+ return 2;
135
+ default:
136
+ return 1;
137
+ }
138
+ }
139
+
140
+ @implementation AngularLynxWebGLContext {
141
+ EAGLContext *_context;
142
+ BOOL _alpha;
143
+ BOOL _depth;
144
+ BOOL _stencil;
145
+ BOOL _preserveDrawingBuffer;
146
+ GLint _samples;
147
+
148
+ // The default framebuffer, multisampled when antialias is on, and the
149
+ // single-sampled framebuffer it resolves into before it's shown.
150
+ GLuint _framebuffer;
151
+ GLuint _colorBuffer;
152
+ GLuint _depthStencilBuffer;
153
+ GLuint _resolveFramebuffer;
154
+ GLuint _resolveBuffer;
155
+ GLsizei _width;
156
+ GLsizei _height;
157
+
158
+ // The view's layer, as a framebuffer each frame is copied into.
159
+ __weak AngularLynxCanvasView *_view;
160
+ GLuint _layerFramebuffer;
161
+ GLuint _layerBuffer;
162
+ GLint _layerWidth;
163
+ GLint _layerHeight;
164
+ BOOL _layerStale;
165
+ /// A frame was finished before there was a view to show it in.
166
+ BOOL _framePending;
167
+
168
+ // The app's bindings, which drawing the frame changes and then restores.
169
+ GLuint _drawFramebuffer;
170
+ GLuint _readFramebuffer;
171
+ GLuint _renderbuffer;
172
+
173
+ // The runtime's object ids, to GL names (ids are never reused).
174
+ GLuint *_names;
175
+ NSUInteger _namesCapacity;
176
+ NSMutableDictionary<NSNumber *, NSValue *> *_syncs;
177
+ }
178
+
179
+ - (instancetype)initWithCanvasId:(NSString *)canvasId
180
+ attributes:(NSDictionary *)attributes
181
+ error:(NSString **)error {
182
+ if (!(self = [super init])) return nil;
183
+ _canvasId = [canvasId copy];
184
+ _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
185
+ if (!_context) {
186
+ *error = @"This device has no OpenGL ES 3.0";
187
+ return nil;
188
+ }
189
+ _alpha = ![attributes[@"alpha"] isEqual:@NO];
190
+ _depth = ![attributes[@"depth"] isEqual:@NO];
191
+ _stencil = [attributes[@"stencil"] isEqual:@YES];
192
+ _preserveDrawingBuffer = [attributes[@"preserveDrawingBuffer"] isEqual:@YES];
193
+ _syncs = [NSMutableDictionary dictionary];
194
+ _namesCapacity = 256;
195
+ _names = calloc(_namesCapacity, sizeof(GLuint));
196
+
197
+ EAGLContext *previous = [EAGLContext currentContext];
198
+ [EAGLContext setCurrentContext:_context];
199
+ if ([attributes[@"antialias"] isEqual:@NO]) {
200
+ _samples = 0;
201
+ } else {
202
+ GLint maxSamples = 0;
203
+ glGetIntegerv(GL_MAX_SAMPLES, &maxSamples);
204
+ _samples = MIN(4, maxSamples);
205
+ }
206
+ glGenFramebuffers(1, &_framebuffer);
207
+ glGenRenderbuffers(1, &_colorBuffer);
208
+ if (_depth || _stencil) glGenRenderbuffers(1, &_depthStencilBuffer);
209
+ if (_samples > 0) {
210
+ glGenFramebuffers(1, &_resolveFramebuffer);
211
+ glGenRenderbuffers(1, &_resolveBuffer);
212
+ }
213
+ glGenFramebuffers(1, &_layerFramebuffer);
214
+ glGenRenderbuffers(1, &_layerBuffer);
215
+ _layerStale = YES;
216
+ _drawFramebuffer = _framebuffer;
217
+ _readFramebuffer = _framebuffer;
218
+ [self allocateDrawingBufferWidth:1 height:1];
219
+ glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
220
+ [EAGLContext setCurrentContext:previous];
221
+ return self;
222
+ }
223
+
224
+ - (void)dealloc {
225
+ [self destroy];
226
+ free(_names);
227
+ }
228
+
229
+ - (void)destroy {
230
+ if (!_context) return;
231
+ EAGLContext *previous = [EAGLContext currentContext];
232
+ [EAGLContext setCurrentContext:_context];
233
+ glDeleteFramebuffers(1, &_framebuffer);
234
+ glDeleteRenderbuffers(1, &_colorBuffer);
235
+ if (_depthStencilBuffer) glDeleteRenderbuffers(1, &_depthStencilBuffer);
236
+ if (_resolveFramebuffer) glDeleteFramebuffers(1, &_resolveFramebuffer);
237
+ if (_resolveBuffer) glDeleteRenderbuffers(1, &_resolveBuffer);
238
+ glDeleteFramebuffers(1, &_layerFramebuffer);
239
+ glDeleteRenderbuffers(1, &_layerBuffer);
240
+ for (NSValue *sync in _syncs.allValues) glDeleteSync(sync.pointerValue);
241
+ [_syncs removeAllObjects];
242
+ [EAGLContext setCurrentContext:previous == _context ? nil : previous];
243
+ _context = nil;
244
+ }
245
+
246
+ #pragma mark - Default framebuffer
247
+
248
+ - (GLenum)colorFormat {
249
+ return _alpha ? GL_RGBA8 : GL_RGB8;
250
+ }
251
+
252
+ - (GLenum)depthStencilFormat {
253
+ if (_depth && _stencil) return GL_DEPTH24_STENCIL8;
254
+ return _depth ? GL_DEPTH_COMPONENT24 : GL_STENCIL_INDEX8;
255
+ }
256
+
257
+ /// (Re)allocates the default framebuffer at the canvas's size, cleared, as
258
+ /// WebGL's is when the canvas resizes.
259
+ - (void)allocateDrawingBufferWidth:(GLsizei)width height:(GLsizei)height {
260
+ _width = MAX(1, width);
261
+ _height = MAX(1, height);
262
+ glBindRenderbuffer(GL_RENDERBUFFER, _colorBuffer);
263
+ if (_samples > 0) {
264
+ glRenderbufferStorageMultisample(GL_RENDERBUFFER, _samples, [self colorFormat], _width,
265
+ _height);
266
+ } else {
267
+ glRenderbufferStorage(GL_RENDERBUFFER, [self colorFormat], _width, _height);
268
+ }
269
+ if (_depthStencilBuffer) {
270
+ glBindRenderbuffer(GL_RENDERBUFFER, _depthStencilBuffer);
271
+ if (_samples > 0) {
272
+ glRenderbufferStorageMultisample(GL_RENDERBUFFER, _samples, [self depthStencilFormat],
273
+ _width, _height);
274
+ } else {
275
+ glRenderbufferStorage(GL_RENDERBUFFER, [self depthStencilFormat], _width, _height);
276
+ }
277
+ }
278
+ glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer);
279
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorBuffer);
280
+ if (_depthStencilBuffer) {
281
+ if (_depth) {
282
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
283
+ _depthStencilBuffer);
284
+ }
285
+ if (_stencil) {
286
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
287
+ _depthStencilBuffer);
288
+ }
289
+ }
290
+ if (_samples > 0) {
291
+ glBindRenderbuffer(GL_RENDERBUFFER, _resolveBuffer);
292
+ glRenderbufferStorage(GL_RENDERBUFFER, [self colorFormat], _width, _height);
293
+ glBindFramebuffer(GL_FRAMEBUFFER, _resolveFramebuffer);
294
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
295
+ _resolveBuffer);
296
+ }
297
+ [self clearDrawingBuffer];
298
+ glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
299
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _drawFramebuffer);
300
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, _readFramebuffer);
301
+ }
302
+
303
+ /// Clears the default framebuffer whatever the app's clear values, masks and
304
+ /// scissor, and puts them back.
305
+ - (void)clearDrawingBuffer {
306
+ GLfloat clearColor[4];
307
+ GLfloat clearDepth;
308
+ GLint clearStencil;
309
+ GLboolean colorMask[4];
310
+ GLboolean depthMask;
311
+ GLint stencilMask, stencilBackMask;
312
+ glGetFloatv(GL_COLOR_CLEAR_VALUE, clearColor);
313
+ glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clearDepth);
314
+ glGetIntegerv(GL_STENCIL_CLEAR_VALUE, &clearStencil);
315
+ glGetBooleanv(GL_COLOR_WRITEMASK, colorMask);
316
+ glGetBooleanv(GL_DEPTH_WRITEMASK, &depthMask);
317
+ glGetIntegerv(GL_STENCIL_WRITEMASK, &stencilMask);
318
+ glGetIntegerv(GL_STENCIL_BACK_WRITEMASK, &stencilBackMask);
319
+ GLboolean scissor = glIsEnabled(GL_SCISSOR_TEST);
320
+ GLboolean discard = glIsEnabled(GL_RASTERIZER_DISCARD);
321
+
322
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _framebuffer);
323
+ glDisable(GL_SCISSOR_TEST);
324
+ glDisable(GL_RASTERIZER_DISCARD);
325
+ glClearColor(0, 0, 0, 0);
326
+ glClearDepthf(1);
327
+ glClearStencil(0);
328
+ glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
329
+ glDepthMask(GL_TRUE);
330
+ glStencilMask(0xffffffff);
331
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
332
+
333
+ glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
334
+ glClearDepthf(clearDepth);
335
+ glClearStencil(clearStencil);
336
+ glColorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
337
+ glDepthMask(depthMask);
338
+ glStencilMaskSeparate(GL_FRONT, (GLuint)stencilMask);
339
+ glStencilMaskSeparate(GL_BACK, (GLuint)stencilBackMask);
340
+ if (scissor) glEnable(GL_SCISSOR_TEST);
341
+ if (discard) glEnable(GL_RASTERIZER_DISCARD);
342
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _drawFramebuffer);
343
+ }
344
+
345
+ - (void)attachView:(AngularLynxCanvasView *)view {
346
+ if (_view == view) return;
347
+ _view = view;
348
+ _layerStale = YES;
349
+ if (view) {
350
+ CAEAGLLayer *layer = (CAEAGLLayer *)view.layer;
351
+ layer.opaque = !_alpha;
352
+ view.opaque = !_alpha;
353
+ if (_framePending) [self present];
354
+ }
355
+ }
356
+
357
+ - (void)viewDidResize {
358
+ _layerStale = YES;
359
+ }
360
+
361
+ /// Shows the finished frame: copies the default framebuffer (resolving it
362
+ /// when it's multisampled) into the view's layer, scaled to its size, and
363
+ /// presents it. Without a laid-out view, the frame waits for one.
364
+ - (void)present {
365
+ AngularLynxCanvasView *view = _view;
366
+ CGSize size = view.bounds.size;
367
+ if (!view || size.width < 1 || size.height < 1) {
368
+ _framePending = YES;
369
+ return;
370
+ }
371
+ EAGLContext *previous = [EAGLContext currentContext];
372
+ [EAGLContext setCurrentContext:_context];
373
+ CAEAGLLayer *layer = (CAEAGLLayer *)view.layer;
374
+ if (_layerStale) {
375
+ _layerStale = NO;
376
+ glBindRenderbuffer(GL_RENDERBUFFER, _layerBuffer);
377
+ [_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
378
+ glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &_layerWidth);
379
+ glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &_layerHeight);
380
+ glBindFramebuffer(GL_FRAMEBUFFER, _layerFramebuffer);
381
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _layerBuffer);
382
+ }
383
+ if (_layerWidth < 1 || _layerHeight < 1) {
384
+ _framePending = YES;
385
+ [self restoreBindings];
386
+ [EAGLContext setCurrentContext:previous];
387
+ return;
388
+ }
389
+ // A blit is clipped by the scissor test, and skipped with rasterizer
390
+ // discard on, so both are off while the frame is copied.
391
+ GLboolean scissor = glIsEnabled(GL_SCISSOR_TEST);
392
+ GLboolean discard = glIsEnabled(GL_RASTERIZER_DISCARD);
393
+ glDisable(GL_SCISSOR_TEST);
394
+ glDisable(GL_RASTERIZER_DISCARD);
395
+ GLuint source = _framebuffer;
396
+ if (_samples > 0) {
397
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, _framebuffer);
398
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _resolveFramebuffer);
399
+ glBlitFramebuffer(0, 0, _width, _height, 0, 0, _width, _height, GL_COLOR_BUFFER_BIT,
400
+ GL_NEAREST);
401
+ source = _resolveFramebuffer;
402
+ }
403
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, source);
404
+ glReadBuffer(GL_COLOR_ATTACHMENT0);
405
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _layerFramebuffer);
406
+ BOOL sameSize = _layerWidth == _width && _layerHeight == _height;
407
+ glBlitFramebuffer(0, 0, _width, _height, 0, 0, _layerWidth, _layerHeight, GL_COLOR_BUFFER_BIT,
408
+ sameSize ? GL_NEAREST : GL_LINEAR);
409
+ glBindRenderbuffer(GL_RENDERBUFFER, _layerBuffer);
410
+ [_context presentRenderbuffer:GL_RENDERBUFFER];
411
+ _framePending = NO;
412
+ if (scissor) glEnable(GL_SCISSOR_TEST);
413
+ if (discard) glEnable(GL_RASTERIZER_DISCARD);
414
+ // WebGL clears the drawing buffer once it's been shown, unless the app
415
+ // asked to keep it.
416
+ if (!_preserveDrawingBuffer) [self clearDrawingBuffer];
417
+ [self restoreBindings];
418
+ [EAGLContext setCurrentContext:previous];
419
+ }
420
+
421
+ - (void)restoreBindings {
422
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _drawFramebuffer);
423
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, _readFramebuffer);
424
+ glBindRenderbuffer(GL_RENDERBUFFER, _renderbuffer);
425
+ }
426
+
427
+ /// WebGL reads an antialiased default framebuffer as one image, its samples
428
+ /// resolved, but GLES can't read a multisampled framebuffer: readPixels and
429
+ /// copyTexImage2D on one are INVALID_OPERATION and read nothing. So a read of
430
+ /// the default framebuffer resolves it, as present does, and reads the
431
+ /// resolved copy. Returns whether it did, for endRead: to bind the app's read
432
+ /// framebuffer again.
433
+ - (BOOL)beginRead {
434
+ if (_samples == 0 || ![self defaultFramebufferBoundTo:GL_READ_FRAMEBUFFER]) return NO;
435
+ // A blit is clipped by the scissor test, and skipped with rasterizer
436
+ // discard on, so both are off while the samples are resolved.
437
+ GLboolean scissor = glIsEnabled(GL_SCISSOR_TEST);
438
+ GLboolean discard = glIsEnabled(GL_RASTERIZER_DISCARD);
439
+ glDisable(GL_SCISSOR_TEST);
440
+ glDisable(GL_RASTERIZER_DISCARD);
441
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _resolveFramebuffer);
442
+ glBlitFramebuffer(0, 0, _width, _height, 0, 0, _width, _height, GL_COLOR_BUFFER_BIT,
443
+ GL_NEAREST);
444
+ if (scissor) glEnable(GL_SCISSOR_TEST);
445
+ if (discard) glEnable(GL_RASTERIZER_DISCARD);
446
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _drawFramebuffer);
447
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, _resolveFramebuffer);
448
+ return YES;
449
+ }
450
+
451
+ - (void)endRead:(BOOL)resolved {
452
+ if (resolved) glBindFramebuffer(GL_READ_FRAMEBUFFER, _readFramebuffer);
453
+ }
454
+
455
+ #pragma mark - Objects
456
+
457
+ - (GLuint)nameOf:(GLuint)objectId {
458
+ return objectId < _namesCapacity ? _names[objectId] : 0;
459
+ }
460
+
461
+ - (void)setName:(GLuint)name forId:(GLuint)objectId {
462
+ if (objectId >= _namesCapacity) {
463
+ NSUInteger capacity = _namesCapacity;
464
+ while (capacity <= objectId) capacity *= 2;
465
+ _names = realloc(_names, capacity * sizeof(GLuint));
466
+ memset(_names + _namesCapacity, 0, (capacity - _namesCapacity) * sizeof(GLuint));
467
+ _namesCapacity = capacity;
468
+ }
469
+ _names[objectId] = name;
470
+ }
471
+
472
+ - (GLuint)takeName:(GLuint)objectId {
473
+ GLuint name = [self nameOf:objectId];
474
+ if (objectId < _namesCapacity) _names[objectId] = 0;
475
+ return name;
476
+ }
477
+
478
+ - (GLsync)syncOf:(GLuint)objectId {
479
+ return _syncs[@(objectId)].pointerValue;
480
+ }
481
+
482
+ /// The bound framebuffer for a target, as the default one or not.
483
+ - (BOOL)defaultFramebufferBoundTo:(GLenum)target {
484
+ return (target == GL_READ_FRAMEBUFFER ? _readFramebuffer : _drawFramebuffer) == _framebuffer;
485
+ }
486
+
487
+ /// WebGL's BACK, COLOR, DEPTH and STENCIL name the default framebuffer's
488
+ /// buffers, which here are ordinary attachments.
489
+ - (GLenum)attachmentFor:(GLenum)attachment target:(GLenum)target {
490
+ if (![self defaultFramebufferBoundTo:target]) return attachment;
491
+ switch (attachment) {
492
+ case WEBGL_BACK:
493
+ case WEBGL_COLOR:
494
+ return GL_COLOR_ATTACHMENT0;
495
+ case WEBGL_DEPTH:
496
+ return GL_DEPTH_ATTACHMENT;
497
+ case WEBGL_STENCIL:
498
+ return GL_STENCIL_ATTACHMENT;
499
+ default:
500
+ return attachment;
501
+ }
502
+ }
503
+
504
+ #pragma mark - Pixels
505
+
506
+ /// An upload's pixels (see #pixels in webgl2-context.ts): NULL, an offset
507
+ /// into the bound PIXEL_UNPACK_BUFFER, bytes, or a decoded image converted to
508
+ /// the format and type. `holder` keeps converted bytes alive for the call.
509
+ - (const void *)pixels:(id)source
510
+ format:(GLenum)format
511
+ type:(GLenum)type
512
+ width:(GLsizei)width
513
+ height:(GLsizei)height
514
+ holder:(NSData **)holder
515
+ isImage:(BOOL *)isImage {
516
+ *isImage = NO;
517
+ if (!source || source == (id)kCFNull) return NULL;
518
+ if ([source isKindOfClass:NSNumber.class]) {
519
+ return (const void *)(intptr_t)[source longLongValue];
520
+ }
521
+ if ([source isKindOfClass:NSArray.class]) {
522
+ *holder = AngularLynxBytesFromWords(source);
523
+ return (*holder).bytes;
524
+ }
525
+ if (![source isKindOfClass:NSDictionary.class]) return NULL;
526
+ AngularLynxDecodedImage *image =
527
+ [AngularLynxDecodedImage imageWithId:[source[@"image"] integerValue]];
528
+ if (!image) return NULL;
529
+ *isImage = YES;
530
+ *holder = [self convertImage:image
531
+ format:format
532
+ type:type
533
+ flipY:[source[@"flipY"] boolValue]
534
+ premultiply:[source[@"premultiply"] boolValue]];
535
+ return (*holder).bytes;
536
+ }
537
+
538
+ /// A decoded image's RGBA bytes as the format and type an upload names,
539
+ /// tightly packed, flipped and premultiplied as asked.
540
+ - (NSData *)convertImage:(AngularLynxDecodedImage *)image
541
+ format:(GLenum)format
542
+ type:(GLenum)type
543
+ flipY:(BOOL)flipY
544
+ premultiply:(BOOL)premultiply {
545
+ NSInteger width = image.width;
546
+ NSInteger height = image.height;
547
+ const uint8_t *rgba = image.pixels.bytes;
548
+ NSUInteger components = componentsOf(format);
549
+ BOOL packed = type == GL_UNSIGNED_SHORT_5_6_5 || type == GL_UNSIGNED_SHORT_4_4_4_4 ||
550
+ type == GL_UNSIGNED_SHORT_5_5_5_1;
551
+ NSUInteger componentSize = type == GL_FLOAT ? 4 : (type == GL_HALF_FLOAT ? 2 : 1);
552
+ NSUInteger pixelSize = packed ? 2 : components * componentSize;
553
+ NSMutableData *out = [NSMutableData dataWithLength:width * height * pixelSize];
554
+ uint8_t *bytes = out.mutableBytes;
555
+ BOOL multiply = premultiply && !image.premultiplied;
556
+ for (NSInteger y = 0; y < height; y++) {
557
+ const uint8_t *row = rgba + (flipY ? height - 1 - y : y) * width * 4;
558
+ uint8_t *outRow = bytes + y * width * pixelSize;
559
+ for (NSInteger x = 0; x < width; x++) {
560
+ float r = row[x * 4], g = row[x * 4 + 1], b = row[x * 4 + 2], a = row[x * 4 + 3];
561
+ if (multiply) {
562
+ r = r * a / 255;
563
+ g = g * a / 255;
564
+ b = b * a / 255;
565
+ }
566
+ float channels[4];
567
+ switch (format) {
568
+ case GL_LUMINANCE:
569
+ channels[0] = r;
570
+ break;
571
+ case GL_ALPHA:
572
+ channels[0] = a;
573
+ break;
574
+ case GL_LUMINANCE_ALPHA:
575
+ channels[0] = r;
576
+ channels[1] = a;
577
+ break;
578
+ default:
579
+ channels[0] = r;
580
+ channels[1] = g;
581
+ channels[2] = b;
582
+ channels[3] = a;
583
+ }
584
+ uint8_t *pixel = outRow + x * pixelSize;
585
+ if (packed) {
586
+ uint16_t value;
587
+ if (type == GL_UNSIGNED_SHORT_5_6_5) {
588
+ value = (uint16_t)(((int)(r / 255 * 31 + 0.5f) << 11) |
589
+ ((int)(g / 255 * 63 + 0.5f) << 5) | (int)(b / 255 * 31 + 0.5f));
590
+ } else if (type == GL_UNSIGNED_SHORT_4_4_4_4) {
591
+ value = (uint16_t)(((int)(r / 255 * 15 + 0.5f) << 12) |
592
+ ((int)(g / 255 * 15 + 0.5f) << 8) |
593
+ ((int)(b / 255 * 15 + 0.5f) << 4) | (int)(a / 255 * 15 + 0.5f));
594
+ } else {
595
+ value = (uint16_t)(((int)(r / 255 * 31 + 0.5f) << 11) |
596
+ ((int)(g / 255 * 31 + 0.5f) << 6) |
597
+ ((int)(b / 255 * 31 + 0.5f) << 1) | (a >= 128 ? 1 : 0));
598
+ }
599
+ memcpy(pixel, &value, 2);
600
+ continue;
601
+ }
602
+ for (NSUInteger c = 0; c < components; c++) {
603
+ if (type == GL_FLOAT) {
604
+ float value = channels[c] / 255;
605
+ memcpy(pixel + c * 4, &value, 4);
606
+ } else if (type == GL_HALF_FLOAT) {
607
+ uint16_t value = halfFromFloat(channels[c] / 255);
608
+ memcpy(pixel + c * 2, &value, 2);
609
+ } else {
610
+ pixel[c] = (uint8_t)(channels[c] + 0.5f);
611
+ }
612
+ }
613
+ }
614
+ }
615
+ return out;
616
+ }
617
+
618
+ /// Runs an upload of converted image bytes with tightly packed unpacking,
619
+ /// whatever the app set: the bytes were packed for it here.
620
+ - (void)withTightUnpacking:(BOOL)tight upload:(void (^)(void))upload {
621
+ if (!tight) {
622
+ upload();
623
+ return;
624
+ }
625
+ GLint alignment, rowLength, imageHeight, skipPixels, skipRows, skipImages;
626
+ glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
627
+ glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowLength);
628
+ glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &imageHeight);
629
+ glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skipPixels);
630
+ glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skipRows);
631
+ glGetIntegerv(GL_UNPACK_SKIP_IMAGES, &skipImages);
632
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
633
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
634
+ glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
635
+ glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
636
+ glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
637
+ glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
638
+ upload();
639
+ glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
640
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, rowLength);
641
+ glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, imageHeight);
642
+ glPixelStorei(GL_UNPACK_SKIP_PIXELS, skipPixels);
643
+ glPixelStorei(GL_UNPACK_SKIP_ROWS, skipRows);
644
+ glPixelStorei(GL_UNPACK_SKIP_IMAGES, skipImages);
645
+ }
646
+
647
+ #pragma mark - Queries
648
+
649
+ - (id)parameter:(GLenum)pname {
650
+ switch (pname) {
651
+ case WEBGL_UNMASKED_VENDOR:
652
+ return glString(GL_VENDOR);
653
+ case WEBGL_UNMASKED_RENDERER:
654
+ return glString(GL_RENDERER);
655
+ case GL_VENDOR:
656
+ case GL_RENDERER:
657
+ case GL_VERSION:
658
+ case GL_SHADING_LANGUAGE_VERSION:
659
+ return glString(pname);
660
+ case GL_ALIASED_LINE_WIDTH_RANGE:
661
+ case GL_ALIASED_POINT_SIZE_RANGE:
662
+ case GL_DEPTH_RANGE: {
663
+ GLfloat values[2] = {0, 0};
664
+ glGetFloatv(pname, values);
665
+ return numbersFromFloats(values, 2);
666
+ }
667
+ case GL_COLOR_CLEAR_VALUE:
668
+ case GL_BLEND_COLOR: {
669
+ GLfloat values[4] = {0, 0, 0, 0};
670
+ glGetFloatv(pname, values);
671
+ return numbersFromFloats(values, 4);
672
+ }
673
+ case GL_MAX_VIEWPORT_DIMS: {
674
+ GLint values[2] = {0, 0};
675
+ glGetIntegerv(pname, values);
676
+ return numbersFromInts(values, 2);
677
+ }
678
+ case GL_SCISSOR_BOX:
679
+ case GL_VIEWPORT: {
680
+ GLint values[4] = {0, 0, 0, 0};
681
+ glGetIntegerv(pname, values);
682
+ return numbersFromInts(values, 4);
683
+ }
684
+ case GL_COLOR_WRITEMASK: {
685
+ GLboolean values[4] = {0, 0, 0, 0};
686
+ glGetBooleanv(pname, values);
687
+ return @[ @(values[0]), @(values[1]), @(values[2]), @(values[3]) ];
688
+ }
689
+ case GL_COMPRESSED_TEXTURE_FORMATS: {
690
+ GLint count = 0;
691
+ glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &count);
692
+ GLint *values = calloc(MAX(count, 1), sizeof(GLint));
693
+ glGetIntegerv(pname, values);
694
+ NSArray *result = numbersFromInts(values, count);
695
+ free(values);
696
+ return result;
697
+ }
698
+ case GL_DEPTH_CLEAR_VALUE:
699
+ case GL_LINE_WIDTH:
700
+ case GL_POLYGON_OFFSET_FACTOR:
701
+ case GL_POLYGON_OFFSET_UNITS:
702
+ case GL_SAMPLE_COVERAGE_VALUE:
703
+ case GL_MAX_TEXTURE_LOD_BIAS:
704
+ case GL_MAX_TEXTURE_MAX_ANISOTROPY: {
705
+ GLfloat value = 0;
706
+ glGetFloatv(pname, &value);
707
+ return @(value);
708
+ }
709
+ case GL_MAX_ELEMENT_INDEX:
710
+ case GL_MAX_SERVER_WAIT_TIMEOUT:
711
+ case GL_MAX_UNIFORM_BLOCK_SIZE:
712
+ case GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS:
713
+ case GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS: {
714
+ GLint64 value = 0;
715
+ glGetInteger64v(pname, &value);
716
+ return @(value);
717
+ }
718
+ case GL_BLEND:
719
+ case GL_CULL_FACE:
720
+ case GL_DEPTH_TEST:
721
+ case GL_DITHER:
722
+ case GL_POLYGON_OFFSET_FILL:
723
+ case GL_RASTERIZER_DISCARD:
724
+ case GL_SAMPLE_ALPHA_TO_COVERAGE:
725
+ case GL_SAMPLE_COVERAGE:
726
+ case GL_SCISSOR_TEST:
727
+ case GL_STENCIL_TEST:
728
+ return @(glIsEnabled(pname) == GL_TRUE);
729
+ case GL_DEPTH_WRITEMASK:
730
+ case GL_SAMPLE_COVERAGE_INVERT:
731
+ case GL_TRANSFORM_FEEDBACK_ACTIVE:
732
+ case GL_TRANSFORM_FEEDBACK_PAUSED: {
733
+ GLboolean value = GL_FALSE;
734
+ glGetBooleanv(pname, &value);
735
+ return @(value == GL_TRUE);
736
+ }
737
+ case GL_STENCIL_VALUE_MASK:
738
+ case GL_STENCIL_WRITEMASK:
739
+ case GL_STENCIL_BACK_VALUE_MASK:
740
+ case GL_STENCIL_BACK_WRITEMASK: {
741
+ GLint value = 0;
742
+ glGetIntegerv(pname, &value);
743
+ return @((GLuint)value);
744
+ }
745
+ default: {
746
+ GLint value = 0;
747
+ glGetIntegerv(pname, &value);
748
+ return @(value);
749
+ }
750
+ }
751
+ }
752
+
753
+ static NSString *programLog(GLuint program) {
754
+ GLint length = 0;
755
+ glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
756
+ if (length <= 1) return @"";
757
+ char *log = malloc(length);
758
+ glGetProgramInfoLog(program, length, NULL, log);
759
+ NSString *result = [NSString stringWithUTF8String:log] ?: @"";
760
+ free(log);
761
+ return result;
762
+ }
763
+
764
+ static NSString *shaderLog(GLuint shader) {
765
+ GLint length = 0;
766
+ glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
767
+ if (length <= 1) return @"";
768
+ char *log = malloc(length);
769
+ glGetShaderInfoLog(shader, length, NULL, log);
770
+ NSString *result = [NSString stringWithUTF8String:log] ?: @"";
771
+ free(log);
772
+ return result;
773
+ }
774
+
775
+ /// The link status, log and reflection three.js reads after each link, in
776
+ /// one answer. Built-ins (`gl_*`) are left out, as WebGL does.
777
+ - (NSDictionary *)programInfo:(GLuint)program {
778
+ GLint linked = GL_FALSE;
779
+ glGetProgramiv(program, GL_LINK_STATUS, &linked);
780
+ NSMutableArray *uniforms = [NSMutableArray array];
781
+ NSMutableArray *attributes = [NSMutableArray array];
782
+ GLint blocks = 0;
783
+ GLint varyings = 0;
784
+ if (linked) {
785
+ GLint count = 0, maxLength = 0;
786
+ glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &count);
787
+ glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLength);
788
+ char *name = malloc(MAX(maxLength, 1) + 1);
789
+ for (GLint i = 0; i < count; i++) {
790
+ GLsizei length = 0;
791
+ GLint size = 0;
792
+ GLenum type = 0;
793
+ glGetActiveUniform(program, i, maxLength + 1, &length, &size, &type, name);
794
+ if (strncmp(name, "gl_", 3) == 0) continue;
795
+ GLint location = glGetUniformLocation(program, name);
796
+ [uniforms addObject:@[ @(name), @(size), @(type), @(location) ]];
797
+ }
798
+ free(name);
799
+ glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &count);
800
+ glGetProgramiv(program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength);
801
+ name = malloc(MAX(maxLength, 1) + 1);
802
+ for (GLint i = 0; i < count; i++) {
803
+ GLsizei length = 0;
804
+ GLint size = 0;
805
+ GLenum type = 0;
806
+ glGetActiveAttrib(program, i, maxLength + 1, &length, &size, &type, name);
807
+ if (strncmp(name, "gl_", 3) == 0) continue;
808
+ GLint location = glGetAttribLocation(program, name);
809
+ [attributes addObject:@[ @(name), @(size), @(type), @(location) ]];
810
+ }
811
+ free(name);
812
+ glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &blocks);
813
+ glGetProgramiv(program, GL_TRANSFORM_FEEDBACK_VARYINGS, &varyings);
814
+ }
815
+ return @{
816
+ @"linked" : @(linked == GL_TRUE),
817
+ @"log" : programLog(program),
818
+ @"uniforms" : uniforms,
819
+ @"attributes" : attributes,
820
+ @"uniformBlocks" : @(blocks),
821
+ @"transformFeedbackVaryings" : @(varyings),
822
+ };
823
+ }
824
+
825
+ /// How many values a uniform of a type holds.
826
+ static GLint uniformComponents(GLenum type) {
827
+ switch (type) {
828
+ case GL_FLOAT_VEC2:
829
+ case GL_INT_VEC2:
830
+ case GL_UNSIGNED_INT_VEC2:
831
+ case GL_BOOL_VEC2:
832
+ return 2;
833
+ case GL_FLOAT_VEC3:
834
+ case GL_INT_VEC3:
835
+ case GL_UNSIGNED_INT_VEC3:
836
+ case GL_BOOL_VEC3:
837
+ return 3;
838
+ case GL_FLOAT_VEC4:
839
+ case GL_INT_VEC4:
840
+ case GL_UNSIGNED_INT_VEC4:
841
+ case GL_BOOL_VEC4:
842
+ case GL_FLOAT_MAT2:
843
+ return 4;
844
+ case GL_FLOAT_MAT2x3:
845
+ case GL_FLOAT_MAT3x2:
846
+ return 6;
847
+ case GL_FLOAT_MAT2x4:
848
+ case GL_FLOAT_MAT4x2:
849
+ return 8;
850
+ case GL_FLOAT_MAT3:
851
+ return 9;
852
+ case GL_FLOAT_MAT3x4:
853
+ case GL_FLOAT_MAT4x3:
854
+ return 12;
855
+ case GL_FLOAT_MAT4:
856
+ return 16;
857
+ default:
858
+ return 1;
859
+ }
860
+ }
861
+
862
+ static BOOL isFloatUniform(GLenum type) {
863
+ switch (type) {
864
+ case GL_FLOAT:
865
+ case GL_FLOAT_VEC2:
866
+ case GL_FLOAT_VEC3:
867
+ case GL_FLOAT_VEC4:
868
+ case GL_FLOAT_MAT2:
869
+ case GL_FLOAT_MAT3:
870
+ case GL_FLOAT_MAT4:
871
+ case GL_FLOAT_MAT2x3:
872
+ case GL_FLOAT_MAT3x2:
873
+ case GL_FLOAT_MAT2x4:
874
+ case GL_FLOAT_MAT4x2:
875
+ case GL_FLOAT_MAT3x4:
876
+ case GL_FLOAT_MAT4x3:
877
+ return YES;
878
+ default:
879
+ return NO;
880
+ }
881
+ }
882
+
883
+ static BOOL isUnsignedUniform(GLenum type) {
884
+ return type == GL_UNSIGNED_INT || type == GL_UNSIGNED_INT_VEC2 ||
885
+ type == GL_UNSIGNED_INT_VEC3 || type == GL_UNSIGNED_INT_VEC4;
886
+ }
887
+
888
+ #pragma mark - Running a batch
889
+
890
+ - (NSArray *)run:(NSArray *)commands {
891
+ if (!_context) return @[];
892
+ EAGLContext *previous = [EAGLContext currentContext];
893
+ if (previous != _context) [EAGLContext setCurrentContext:_context];
894
+ NSUInteger count = commands.count;
895
+ __unsafe_unretained id *items = (__unsafe_unretained id *)malloc(sizeof(id) * MAX(count, 1));
896
+ [commands getObjects:items range:NSMakeRange(0, count)];
897
+ Reader reader = {items, count, 0};
898
+ Reader *r = &reader;
899
+ NSMutableArray *results = [NSMutableArray array];
900
+ while (r->index < r->count) {
901
+ NSInteger op = (NSInteger)nextNumber(r);
902
+ if (op >= 200) {
903
+ id result = [self query:op reader:r];
904
+ [results addObject:result ?: [NSNull null]];
905
+ } else {
906
+ [self command:op reader:r];
907
+ }
908
+ }
909
+ free(items);
910
+ if (previous != _context) [EAGLContext setCurrentContext:previous];
911
+ return results;
912
+ }
913
+
914
+ - (void)command:(NSInteger)op reader:(Reader *)r {
915
+ switch ((ALWebGLOp)op) {
916
+ case ALWebGLOpActiveTexture:
917
+ glActiveTexture(nextEnum(r));
918
+ break;
919
+ case ALWebGLOpAttachShader: {
920
+ GLuint program = [self nameOf:nextUInt(r)];
921
+ glAttachShader(program, [self nameOf:nextUInt(r)]);
922
+ break;
923
+ }
924
+ case ALWebGLOpBindAttribLocation: {
925
+ GLuint program = [self nameOf:nextUInt(r)];
926
+ GLuint index = nextUInt(r);
927
+ glBindAttribLocation(program, index, nextString(r).UTF8String);
928
+ break;
929
+ }
930
+ case ALWebGLOpBindBuffer: {
931
+ GLenum target = nextEnum(r);
932
+ glBindBuffer(target, [self nameOf:nextUInt(r)]);
933
+ break;
934
+ }
935
+ case ALWebGLOpBindBufferBase: {
936
+ GLenum target = nextEnum(r);
937
+ GLuint index = nextUInt(r);
938
+ glBindBufferBase(target, index, [self nameOf:nextUInt(r)]);
939
+ break;
940
+ }
941
+ case ALWebGLOpBindBufferRange: {
942
+ GLenum target = nextEnum(r);
943
+ GLuint index = nextUInt(r);
944
+ GLuint buffer = [self nameOf:nextUInt(r)];
945
+ GLintptr offset = nextOffset(r);
946
+ glBindBufferRange(target, index, buffer, offset, (GLsizeiptr)nextOffset(r));
947
+ break;
948
+ }
949
+ case ALWebGLOpBindFramebuffer: {
950
+ GLenum target = nextEnum(r);
951
+ GLuint objectId = nextUInt(r);
952
+ // null is the default framebuffer, which is one of ours.
953
+ GLuint name = objectId ? [self nameOf:objectId] : _framebuffer;
954
+ if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) _drawFramebuffer = name;
955
+ if (target == GL_FRAMEBUFFER || target == GL_READ_FRAMEBUFFER) _readFramebuffer = name;
956
+ glBindFramebuffer(target, name);
957
+ break;
958
+ }
959
+ case ALWebGLOpBindRenderbuffer: {
960
+ GLenum target = nextEnum(r);
961
+ _renderbuffer = [self nameOf:nextUInt(r)];
962
+ glBindRenderbuffer(target, _renderbuffer);
963
+ break;
964
+ }
965
+ case ALWebGLOpBindSampler: {
966
+ GLuint unit = nextUInt(r);
967
+ glBindSampler(unit, [self nameOf:nextUInt(r)]);
968
+ break;
969
+ }
970
+ case ALWebGLOpBindTexture: {
971
+ GLenum target = nextEnum(r);
972
+ glBindTexture(target, [self nameOf:nextUInt(r)]);
973
+ break;
974
+ }
975
+ case ALWebGLOpBindTransformFeedback: {
976
+ GLenum target = nextEnum(r);
977
+ glBindTransformFeedback(target, [self nameOf:nextUInt(r)]);
978
+ break;
979
+ }
980
+ case ALWebGLOpBindVertexArray:
981
+ glBindVertexArray([self nameOf:nextUInt(r)]);
982
+ break;
983
+ case ALWebGLOpBeginQuery: {
984
+ GLenum target = nextEnum(r);
985
+ glBeginQuery(target, [self nameOf:nextUInt(r)]);
986
+ break;
987
+ }
988
+ case ALWebGLOpBeginTransformFeedback:
989
+ glBeginTransformFeedback(nextEnum(r));
990
+ break;
991
+ case ALWebGLOpBlendColor: {
992
+ GLfloat red = nextFloat(r), green = nextFloat(r), blue = nextFloat(r);
993
+ glBlendColor(red, green, blue, nextFloat(r));
994
+ break;
995
+ }
996
+ case ALWebGLOpBlendEquation:
997
+ glBlendEquation(nextEnum(r));
998
+ break;
999
+ case ALWebGLOpBlendEquationSeparate: {
1000
+ GLenum rgb = nextEnum(r);
1001
+ glBlendEquationSeparate(rgb, nextEnum(r));
1002
+ break;
1003
+ }
1004
+ case ALWebGLOpBlendFunc: {
1005
+ GLenum source = nextEnum(r);
1006
+ glBlendFunc(source, nextEnum(r));
1007
+ break;
1008
+ }
1009
+ case ALWebGLOpBlendFuncSeparate: {
1010
+ GLenum srcRGB = nextEnum(r), dstRGB = nextEnum(r), srcAlpha = nextEnum(r);
1011
+ glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, nextEnum(r));
1012
+ break;
1013
+ }
1014
+ case ALWebGLOpBlitFramebuffer: {
1015
+ GLint srcX0 = nextInt(r), srcY0 = nextInt(r), srcX1 = nextInt(r), srcY1 = nextInt(r);
1016
+ GLint dstX0 = nextInt(r), dstY0 = nextInt(r), dstX1 = nextInt(r), dstY1 = nextInt(r);
1017
+ GLbitfield mask = nextUInt(r);
1018
+ glBlitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask,
1019
+ nextEnum(r));
1020
+ break;
1021
+ }
1022
+ case ALWebGLOpBufferData: {
1023
+ GLenum target = nextEnum(r);
1024
+ id data = nextItem(r);
1025
+ GLenum usage = nextEnum(r);
1026
+ if ([data isKindOfClass:NSArray.class]) {
1027
+ NSData *bytes = AngularLynxBytesFromWords(data);
1028
+ glBufferData(target, (GLsizeiptr)bytes.length, bytes.bytes, usage);
1029
+ } else {
1030
+ glBufferData(target, (GLsizeiptr)[data longLongValue], NULL, usage);
1031
+ }
1032
+ break;
1033
+ }
1034
+ case ALWebGLOpBufferSubData: {
1035
+ GLenum target = nextEnum(r);
1036
+ GLintptr offset = nextOffset(r);
1037
+ NSData *bytes = AngularLynxBytesFromWords(nextArray(r));
1038
+ glBufferSubData(target, offset, (GLsizeiptr)bytes.length, bytes.bytes);
1039
+ break;
1040
+ }
1041
+ case ALWebGLOpClear:
1042
+ glClear(nextUInt(r));
1043
+ break;
1044
+ case ALWebGLOpClearBufferfv: {
1045
+ GLenum buffer = nextEnum(r);
1046
+ GLint drawbuffer = nextInt(r);
1047
+ NSMutableData *values = floatsFrom(nextArray(r));
1048
+ if (values.length < 4 * sizeof(GLfloat)) values.length = 4 * sizeof(GLfloat);
1049
+ glClearBufferfv(buffer, drawbuffer, values.bytes);
1050
+ break;
1051
+ }
1052
+ case ALWebGLOpClearBufferiv: {
1053
+ GLenum buffer = nextEnum(r);
1054
+ GLint drawbuffer = nextInt(r);
1055
+ NSMutableData *values = intsFrom(nextArray(r));
1056
+ if (values.length < 4 * sizeof(GLint)) values.length = 4 * sizeof(GLint);
1057
+ glClearBufferiv(buffer, drawbuffer, values.bytes);
1058
+ break;
1059
+ }
1060
+ case ALWebGLOpClearBufferuiv: {
1061
+ GLenum buffer = nextEnum(r);
1062
+ GLint drawbuffer = nextInt(r);
1063
+ NSMutableData *values = uintsFrom(nextArray(r));
1064
+ if (values.length < 4 * sizeof(GLuint)) values.length = 4 * sizeof(GLuint);
1065
+ glClearBufferuiv(buffer, drawbuffer, values.bytes);
1066
+ break;
1067
+ }
1068
+ case ALWebGLOpClearBufferfi: {
1069
+ GLenum buffer = nextEnum(r);
1070
+ GLint drawbuffer = nextInt(r);
1071
+ GLfloat depth = nextFloat(r);
1072
+ glClearBufferfi(buffer, drawbuffer, depth, nextInt(r));
1073
+ break;
1074
+ }
1075
+ case ALWebGLOpClearColor: {
1076
+ GLfloat red = nextFloat(r), green = nextFloat(r), blue = nextFloat(r);
1077
+ glClearColor(red, green, blue, nextFloat(r));
1078
+ break;
1079
+ }
1080
+ case ALWebGLOpClearDepth:
1081
+ glClearDepthf(nextFloat(r));
1082
+ break;
1083
+ case ALWebGLOpClearStencil:
1084
+ glClearStencil(nextInt(r));
1085
+ break;
1086
+ case ALWebGLOpColorMask: {
1087
+ GLboolean red = nextInt(r) != 0, green = nextInt(r) != 0, blue = nextInt(r) != 0;
1088
+ glColorMask(red, green, blue, nextInt(r) != 0);
1089
+ break;
1090
+ }
1091
+ case ALWebGLOpCompileShader:
1092
+ glCompileShader([self nameOf:nextUInt(r)]);
1093
+ break;
1094
+ case ALWebGLOpCompressedTexImage2D: {
1095
+ GLenum target = nextEnum(r);
1096
+ GLint level = nextInt(r);
1097
+ GLenum internalformat = nextEnum(r);
1098
+ GLsizei width = nextInt(r), height = nextInt(r);
1099
+ GLint border = nextInt(r);
1100
+ id data = nextItem(r);
1101
+ GLsizei size = nextInt(r);
1102
+ NSData *bytes = [data isKindOfClass:NSArray.class] ? AngularLynxBytesFromWords(data) : nil;
1103
+ glCompressedTexImage2D(target, level, internalformat, width, height, border, size,
1104
+ bytes ? bytes.bytes : (const void *)(intptr_t)[data longLongValue]);
1105
+ break;
1106
+ }
1107
+ case ALWebGLOpCompressedTexImage3D: {
1108
+ GLenum target = nextEnum(r);
1109
+ GLint level = nextInt(r);
1110
+ GLenum internalformat = nextEnum(r);
1111
+ GLsizei width = nextInt(r), height = nextInt(r), depth = nextInt(r);
1112
+ GLint border = nextInt(r);
1113
+ id data = nextItem(r);
1114
+ GLsizei size = nextInt(r);
1115
+ NSData *bytes = [data isKindOfClass:NSArray.class] ? AngularLynxBytesFromWords(data) : nil;
1116
+ glCompressedTexImage3D(target, level, internalformat, width, height, depth, border, size,
1117
+ bytes ? bytes.bytes : (const void *)(intptr_t)[data longLongValue]);
1118
+ break;
1119
+ }
1120
+ case ALWebGLOpCompressedTexSubImage2D: {
1121
+ GLenum target = nextEnum(r);
1122
+ GLint level = nextInt(r), x = nextInt(r), y = nextInt(r);
1123
+ GLsizei width = nextInt(r), height = nextInt(r);
1124
+ GLenum format = nextEnum(r);
1125
+ id data = nextItem(r);
1126
+ GLsizei size = nextInt(r);
1127
+ NSData *bytes = [data isKindOfClass:NSArray.class] ? AngularLynxBytesFromWords(data) : nil;
1128
+ glCompressedTexSubImage2D(target, level, x, y, width, height, format, size,
1129
+ bytes ? bytes.bytes
1130
+ : (const void *)(intptr_t)[data longLongValue]);
1131
+ break;
1132
+ }
1133
+ case ALWebGLOpCompressedTexSubImage3D: {
1134
+ GLenum target = nextEnum(r);
1135
+ GLint level = nextInt(r), x = nextInt(r), y = nextInt(r), z = nextInt(r);
1136
+ GLsizei width = nextInt(r), height = nextInt(r), depth = nextInt(r);
1137
+ GLenum format = nextEnum(r);
1138
+ id data = nextItem(r);
1139
+ GLsizei size = nextInt(r);
1140
+ NSData *bytes = [data isKindOfClass:NSArray.class] ? AngularLynxBytesFromWords(data) : nil;
1141
+ glCompressedTexSubImage3D(target, level, x, y, z, width, height, depth, format, size,
1142
+ bytes ? bytes.bytes
1143
+ : (const void *)(intptr_t)[data longLongValue]);
1144
+ break;
1145
+ }
1146
+ case ALWebGLOpCopyBufferSubData: {
1147
+ GLenum readTarget = nextEnum(r), writeTarget = nextEnum(r);
1148
+ GLintptr readOffset = nextOffset(r), writeOffset = nextOffset(r);
1149
+ glCopyBufferSubData(readTarget, writeTarget, readOffset, writeOffset,
1150
+ (GLsizeiptr)nextOffset(r));
1151
+ break;
1152
+ }
1153
+ case ALWebGLOpCopyTexImage2D: {
1154
+ GLenum target = nextEnum(r);
1155
+ GLint level = nextInt(r);
1156
+ GLenum internalformat = nextEnum(r);
1157
+ GLint x = nextInt(r), y = nextInt(r);
1158
+ GLsizei width = nextInt(r), height = nextInt(r);
1159
+ GLint border = nextInt(r);
1160
+ BOOL resolved = [self beginRead];
1161
+ glCopyTexImage2D(target, level, internalformat, x, y, width, height, border);
1162
+ [self endRead:resolved];
1163
+ break;
1164
+ }
1165
+ case ALWebGLOpCopyTexSubImage2D: {
1166
+ GLenum target = nextEnum(r);
1167
+ GLint level = nextInt(r), xoffset = nextInt(r), yoffset = nextInt(r);
1168
+ GLint x = nextInt(r), y = nextInt(r);
1169
+ GLsizei width = nextInt(r);
1170
+ GLsizei height = nextInt(r);
1171
+ BOOL resolved = [self beginRead];
1172
+ glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height);
1173
+ [self endRead:resolved];
1174
+ break;
1175
+ }
1176
+ case ALWebGLOpCopyTexSubImage3D: {
1177
+ GLenum target = nextEnum(r);
1178
+ GLint level = nextInt(r), xoffset = nextInt(r), yoffset = nextInt(r), zoffset = nextInt(r);
1179
+ GLint x = nextInt(r), y = nextInt(r);
1180
+ GLsizei width = nextInt(r);
1181
+ GLsizei height = nextInt(r);
1182
+ BOOL resolved = [self beginRead];
1183
+ glCopyTexSubImage3D(target, level, xoffset, yoffset, zoffset, x, y, width, height);
1184
+ [self endRead:resolved];
1185
+ break;
1186
+ }
1187
+ case ALWebGLOpCreateBuffer: {
1188
+ GLuint name = 0;
1189
+ glGenBuffers(1, &name);
1190
+ [self setName:name forId:nextUInt(r)];
1191
+ break;
1192
+ }
1193
+ case ALWebGLOpCreateFramebuffer: {
1194
+ GLuint name = 0;
1195
+ glGenFramebuffers(1, &name);
1196
+ [self setName:name forId:nextUInt(r)];
1197
+ break;
1198
+ }
1199
+ case ALWebGLOpCreateProgram:
1200
+ [self setName:glCreateProgram() forId:nextUInt(r)];
1201
+ break;
1202
+ case ALWebGLOpCreateQuery: {
1203
+ GLuint name = 0;
1204
+ glGenQueries(1, &name);
1205
+ [self setName:name forId:nextUInt(r)];
1206
+ break;
1207
+ }
1208
+ case ALWebGLOpCreateRenderbuffer: {
1209
+ GLuint name = 0;
1210
+ glGenRenderbuffers(1, &name);
1211
+ [self setName:name forId:nextUInt(r)];
1212
+ break;
1213
+ }
1214
+ case ALWebGLOpCreateSampler: {
1215
+ GLuint name = 0;
1216
+ glGenSamplers(1, &name);
1217
+ [self setName:name forId:nextUInt(r)];
1218
+ break;
1219
+ }
1220
+ case ALWebGLOpCreateShader: {
1221
+ GLuint objectId = nextUInt(r);
1222
+ [self setName:glCreateShader(nextEnum(r)) forId:objectId];
1223
+ break;
1224
+ }
1225
+ case ALWebGLOpCreateTexture: {
1226
+ GLuint name = 0;
1227
+ glGenTextures(1, &name);
1228
+ [self setName:name forId:nextUInt(r)];
1229
+ break;
1230
+ }
1231
+ case ALWebGLOpCreateTransformFeedback: {
1232
+ GLuint name = 0;
1233
+ glGenTransformFeedbacks(1, &name);
1234
+ [self setName:name forId:nextUInt(r)];
1235
+ break;
1236
+ }
1237
+ case ALWebGLOpCreateVertexArray: {
1238
+ GLuint name = 0;
1239
+ glGenVertexArrays(1, &name);
1240
+ [self setName:name forId:nextUInt(r)];
1241
+ break;
1242
+ }
1243
+ case ALWebGLOpCullFace:
1244
+ glCullFace(nextEnum(r));
1245
+ break;
1246
+ case ALWebGLOpDeleteBuffer: {
1247
+ GLuint name = [self takeName:nextUInt(r)];
1248
+ if (name) glDeleteBuffers(1, &name);
1249
+ break;
1250
+ }
1251
+ case ALWebGLOpDeleteFramebuffer: {
1252
+ GLuint name = [self takeName:nextUInt(r)];
1253
+ if (!name) break;
1254
+ // Deleting the bound framebuffer binds the default one, as in WebGL.
1255
+ if (_drawFramebuffer == name) _drawFramebuffer = _framebuffer;
1256
+ if (_readFramebuffer == name) _readFramebuffer = _framebuffer;
1257
+ glDeleteFramebuffers(1, &name);
1258
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _drawFramebuffer);
1259
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, _readFramebuffer);
1260
+ break;
1261
+ }
1262
+ case ALWebGLOpDeleteProgram: {
1263
+ GLuint name = [self takeName:nextUInt(r)];
1264
+ if (name) glDeleteProgram(name);
1265
+ break;
1266
+ }
1267
+ case ALWebGLOpDeleteQuery: {
1268
+ GLuint name = [self takeName:nextUInt(r)];
1269
+ if (name) glDeleteQueries(1, &name);
1270
+ break;
1271
+ }
1272
+ case ALWebGLOpDeleteRenderbuffer: {
1273
+ GLuint name = [self takeName:nextUInt(r)];
1274
+ if (!name) break;
1275
+ if (_renderbuffer == name) _renderbuffer = 0;
1276
+ glDeleteRenderbuffers(1, &name);
1277
+ break;
1278
+ }
1279
+ case ALWebGLOpDeleteSampler: {
1280
+ GLuint name = [self takeName:nextUInt(r)];
1281
+ if (name) glDeleteSamplers(1, &name);
1282
+ break;
1283
+ }
1284
+ case ALWebGLOpDeleteShader: {
1285
+ GLuint name = [self takeName:nextUInt(r)];
1286
+ if (name) glDeleteShader(name);
1287
+ break;
1288
+ }
1289
+ case ALWebGLOpDeleteSync: {
1290
+ NSNumber *key = @(nextUInt(r));
1291
+ GLsync sync = _syncs[key].pointerValue;
1292
+ if (sync) glDeleteSync(sync);
1293
+ [_syncs removeObjectForKey:key];
1294
+ break;
1295
+ }
1296
+ case ALWebGLOpDeleteTexture: {
1297
+ GLuint name = [self takeName:nextUInt(r)];
1298
+ if (name) glDeleteTextures(1, &name);
1299
+ break;
1300
+ }
1301
+ case ALWebGLOpDeleteTransformFeedback: {
1302
+ GLuint name = [self takeName:nextUInt(r)];
1303
+ if (name) glDeleteTransformFeedbacks(1, &name);
1304
+ break;
1305
+ }
1306
+ case ALWebGLOpDeleteVertexArray: {
1307
+ GLuint name = [self takeName:nextUInt(r)];
1308
+ if (name) glDeleteVertexArrays(1, &name);
1309
+ break;
1310
+ }
1311
+ case ALWebGLOpDepthFunc:
1312
+ glDepthFunc(nextEnum(r));
1313
+ break;
1314
+ case ALWebGLOpDepthMask:
1315
+ glDepthMask(nextInt(r) != 0);
1316
+ break;
1317
+ case ALWebGLOpDepthRange: {
1318
+ GLfloat zNear = nextFloat(r);
1319
+ glDepthRangef(zNear, nextFloat(r));
1320
+ break;
1321
+ }
1322
+ case ALWebGLOpDetachShader: {
1323
+ GLuint program = [self nameOf:nextUInt(r)];
1324
+ glDetachShader(program, [self nameOf:nextUInt(r)]);
1325
+ break;
1326
+ }
1327
+ case ALWebGLOpDisable:
1328
+ glDisable(nextEnum(r));
1329
+ break;
1330
+ case ALWebGLOpDisableVertexAttribArray:
1331
+ glDisableVertexAttribArray(nextUInt(r));
1332
+ break;
1333
+ case ALWebGLOpDrawArrays: {
1334
+ GLenum mode = nextEnum(r);
1335
+ GLint first = nextInt(r);
1336
+ glDrawArrays(mode, first, nextInt(r));
1337
+ break;
1338
+ }
1339
+ case ALWebGLOpDrawArraysInstanced: {
1340
+ GLenum mode = nextEnum(r);
1341
+ GLint first = nextInt(r);
1342
+ GLsizei count = nextInt(r);
1343
+ glDrawArraysInstanced(mode, first, count, nextInt(r));
1344
+ break;
1345
+ }
1346
+ case ALWebGLOpDrawBuffers: {
1347
+ NSArray *buffers = nextArray(r);
1348
+ GLenum *values = calloc(MAX(buffers.count, 1), sizeof(GLenum));
1349
+ BOOL defaultBound = [self defaultFramebufferBoundTo:GL_DRAW_FRAMEBUFFER];
1350
+ for (NSUInteger i = 0; i < buffers.count; i++) {
1351
+ GLenum value = (GLenum)[buffers[i] longLongValue];
1352
+ values[i] = defaultBound && value == WEBGL_BACK ? GL_COLOR_ATTACHMENT0 : value;
1353
+ }
1354
+ glDrawBuffers((GLsizei)buffers.count, values);
1355
+ free(values);
1356
+ break;
1357
+ }
1358
+ case ALWebGLOpDrawElements: {
1359
+ GLenum mode = nextEnum(r);
1360
+ GLsizei count = nextInt(r);
1361
+ GLenum type = nextEnum(r);
1362
+ glDrawElements(mode, count, type, (const void *)nextOffset(r));
1363
+ break;
1364
+ }
1365
+ case ALWebGLOpDrawElementsInstanced: {
1366
+ GLenum mode = nextEnum(r);
1367
+ GLsizei count = nextInt(r);
1368
+ GLenum type = nextEnum(r);
1369
+ const void *offset = (const void *)nextOffset(r);
1370
+ glDrawElementsInstanced(mode, count, type, offset, nextInt(r));
1371
+ break;
1372
+ }
1373
+ case ALWebGLOpDrawRangeElements: {
1374
+ GLenum mode = nextEnum(r);
1375
+ GLuint start = nextUInt(r), end = nextUInt(r);
1376
+ GLsizei count = nextInt(r);
1377
+ GLenum type = nextEnum(r);
1378
+ glDrawRangeElements(mode, start, end, count, type, (const void *)nextOffset(r));
1379
+ break;
1380
+ }
1381
+ case ALWebGLOpEnable:
1382
+ glEnable(nextEnum(r));
1383
+ break;
1384
+ case ALWebGLOpEnableVertexAttribArray:
1385
+ glEnableVertexAttribArray(nextUInt(r));
1386
+ break;
1387
+ case ALWebGLOpEndQuery:
1388
+ glEndQuery(nextEnum(r));
1389
+ break;
1390
+ case ALWebGLOpEndTransformFeedback:
1391
+ glEndTransformFeedback();
1392
+ break;
1393
+ case ALWebGLOpFenceSync: {
1394
+ GLuint objectId = nextUInt(r);
1395
+ GLenum condition = nextEnum(r);
1396
+ GLsync sync = glFenceSync(condition, nextUInt(r));
1397
+ if (sync) _syncs[@(objectId)] = [NSValue valueWithPointer:sync];
1398
+ break;
1399
+ }
1400
+ case ALWebGLOpFinish:
1401
+ glFinish();
1402
+ break;
1403
+ case ALWebGLOpFlush:
1404
+ glFlush();
1405
+ break;
1406
+ case ALWebGLOpFramebufferRenderbuffer: {
1407
+ GLenum target = nextEnum(r), attachment = nextEnum(r), renderbuffertarget = nextEnum(r);
1408
+ glFramebufferRenderbuffer(target, attachment, renderbuffertarget,
1409
+ [self nameOf:nextUInt(r)]);
1410
+ break;
1411
+ }
1412
+ case ALWebGLOpFramebufferTexture2D: {
1413
+ GLenum target = nextEnum(r), attachment = nextEnum(r), textarget = nextEnum(r);
1414
+ GLuint texture = [self nameOf:nextUInt(r)];
1415
+ glFramebufferTexture2D(target, attachment, textarget, texture, nextInt(r));
1416
+ break;
1417
+ }
1418
+ case ALWebGLOpFramebufferTextureLayer: {
1419
+ GLenum target = nextEnum(r), attachment = nextEnum(r);
1420
+ GLuint texture = [self nameOf:nextUInt(r)];
1421
+ GLint level = nextInt(r);
1422
+ glFramebufferTextureLayer(target, attachment, texture, level, nextInt(r));
1423
+ break;
1424
+ }
1425
+ case ALWebGLOpFrontFace:
1426
+ glFrontFace(nextEnum(r));
1427
+ break;
1428
+ case ALWebGLOpGenerateMipmap:
1429
+ glGenerateMipmap(nextEnum(r));
1430
+ break;
1431
+ case ALWebGLOpHint: {
1432
+ GLenum target = nextEnum(r);
1433
+ glHint(target, nextEnum(r));
1434
+ break;
1435
+ }
1436
+ case ALWebGLOpInvalidateFramebuffer:
1437
+ case ALWebGLOpInvalidateSubFramebuffer: {
1438
+ GLenum target = nextEnum(r);
1439
+ NSArray *attachments = nextArray(r);
1440
+ GLenum *values = calloc(MAX(attachments.count, 1), sizeof(GLenum));
1441
+ for (NSUInteger i = 0; i < attachments.count; i++) {
1442
+ values[i] = [self attachmentFor:(GLenum)[attachments[i] longLongValue] target:target];
1443
+ }
1444
+ if (op == ALWebGLOpInvalidateFramebuffer) {
1445
+ glInvalidateFramebuffer(target, (GLsizei)attachments.count, values);
1446
+ } else {
1447
+ GLint x = nextInt(r), y = nextInt(r);
1448
+ GLsizei width = nextInt(r);
1449
+ glInvalidateSubFramebuffer(target, (GLsizei)attachments.count, values, x, y, width,
1450
+ nextInt(r));
1451
+ }
1452
+ free(values);
1453
+ break;
1454
+ }
1455
+ case ALWebGLOpLineWidth:
1456
+ glLineWidth(nextFloat(r));
1457
+ break;
1458
+ case ALWebGLOpLinkProgram:
1459
+ glLinkProgram([self nameOf:nextUInt(r)]);
1460
+ break;
1461
+ case ALWebGLOpPauseTransformFeedback:
1462
+ glPauseTransformFeedback();
1463
+ break;
1464
+ case ALWebGLOpPixelStorei: {
1465
+ GLenum pname = nextEnum(r);
1466
+ glPixelStorei(pname, nextInt(r));
1467
+ break;
1468
+ }
1469
+ case ALWebGLOpPolygonOffset: {
1470
+ GLfloat factor = nextFloat(r);
1471
+ glPolygonOffset(factor, nextFloat(r));
1472
+ break;
1473
+ }
1474
+ case ALWebGLOpReadBuffer: {
1475
+ GLenum source = nextEnum(r);
1476
+ glReadBuffer([self defaultFramebufferBoundTo:GL_READ_FRAMEBUFFER] && source == WEBGL_BACK
1477
+ ? GL_COLOR_ATTACHMENT0
1478
+ : source);
1479
+ break;
1480
+ }
1481
+ case ALWebGLOpReadPixelsToBuffer: {
1482
+ GLint x = nextInt(r), y = nextInt(r);
1483
+ GLsizei width = nextInt(r), height = nextInt(r);
1484
+ GLenum format = nextEnum(r), type = nextEnum(r);
1485
+ void *offset = (void *)nextOffset(r);
1486
+ BOOL resolved = [self beginRead];
1487
+ glReadPixels(x, y, width, height, format, type, offset);
1488
+ [self endRead:resolved];
1489
+ break;
1490
+ }
1491
+ case ALWebGLOpRenderbufferStorage: {
1492
+ GLenum target = nextEnum(r), internalformat = nextEnum(r);
1493
+ GLsizei width = nextInt(r);
1494
+ glRenderbufferStorage(target, internalformat, width, nextInt(r));
1495
+ break;
1496
+ }
1497
+ case ALWebGLOpRenderbufferStorageMultisample: {
1498
+ GLenum target = nextEnum(r);
1499
+ GLsizei samples = nextInt(r);
1500
+ GLenum internalformat = nextEnum(r);
1501
+ GLsizei width = nextInt(r);
1502
+ glRenderbufferStorageMultisample(target, samples, internalformat, width, nextInt(r));
1503
+ break;
1504
+ }
1505
+ case ALWebGLOpResumeTransformFeedback:
1506
+ glResumeTransformFeedback();
1507
+ break;
1508
+ case ALWebGLOpSampleCoverage: {
1509
+ GLfloat value = nextFloat(r);
1510
+ glSampleCoverage(value, nextInt(r) != 0);
1511
+ break;
1512
+ }
1513
+ case ALWebGLOpSamplerParameterf: {
1514
+ GLuint sampler = [self nameOf:nextUInt(r)];
1515
+ GLenum pname = nextEnum(r);
1516
+ glSamplerParameterf(sampler, pname, nextFloat(r));
1517
+ break;
1518
+ }
1519
+ case ALWebGLOpSamplerParameteri: {
1520
+ GLuint sampler = [self nameOf:nextUInt(r)];
1521
+ GLenum pname = nextEnum(r);
1522
+ glSamplerParameteri(sampler, pname, nextInt(r));
1523
+ break;
1524
+ }
1525
+ case ALWebGLOpScissor: {
1526
+ GLint x = nextInt(r), y = nextInt(r);
1527
+ GLsizei width = nextInt(r);
1528
+ glScissor(x, y, width, nextInt(r));
1529
+ break;
1530
+ }
1531
+ case ALWebGLOpShaderSource: {
1532
+ GLuint shader = [self nameOf:nextUInt(r)];
1533
+ const char *source = nextString(r).UTF8String;
1534
+ glShaderSource(shader, 1, &source, NULL);
1535
+ break;
1536
+ }
1537
+ case ALWebGLOpStencilFunc: {
1538
+ GLenum func = nextEnum(r);
1539
+ GLint ref = nextInt(r);
1540
+ glStencilFunc(func, ref, nextUInt(r));
1541
+ break;
1542
+ }
1543
+ case ALWebGLOpStencilFuncSeparate: {
1544
+ GLenum face = nextEnum(r), func = nextEnum(r);
1545
+ GLint ref = nextInt(r);
1546
+ glStencilFuncSeparate(face, func, ref, nextUInt(r));
1547
+ break;
1548
+ }
1549
+ case ALWebGLOpStencilMask:
1550
+ glStencilMask(nextUInt(r));
1551
+ break;
1552
+ case ALWebGLOpStencilMaskSeparate: {
1553
+ GLenum face = nextEnum(r);
1554
+ glStencilMaskSeparate(face, nextUInt(r));
1555
+ break;
1556
+ }
1557
+ case ALWebGLOpStencilOp: {
1558
+ GLenum fail = nextEnum(r), zfail = nextEnum(r);
1559
+ glStencilOp(fail, zfail, nextEnum(r));
1560
+ break;
1561
+ }
1562
+ case ALWebGLOpStencilOpSeparate: {
1563
+ GLenum face = nextEnum(r), fail = nextEnum(r), zfail = nextEnum(r);
1564
+ glStencilOpSeparate(face, fail, zfail, nextEnum(r));
1565
+ break;
1566
+ }
1567
+ case ALWebGLOpTexImage2D: {
1568
+ GLenum target = nextEnum(r);
1569
+ GLint level = nextInt(r), internalformat = nextInt(r);
1570
+ GLsizei width = nextInt(r), height = nextInt(r);
1571
+ GLint border = nextInt(r);
1572
+ GLenum format = nextEnum(r), type = nextEnum(r);
1573
+ NSData *holder = nil;
1574
+ BOOL isImage = NO;
1575
+ const void *pixels = [self pixels:nextItem(r)
1576
+ format:format
1577
+ type:type
1578
+ width:width
1579
+ height:height
1580
+ holder:&holder
1581
+ isImage:&isImage];
1582
+ [self withTightUnpacking:isImage
1583
+ upload:^{
1584
+ glTexImage2D(target, level, internalformat, width, height, border,
1585
+ format, type, pixels);
1586
+ }];
1587
+ break;
1588
+ }
1589
+ case ALWebGLOpTexImage3D: {
1590
+ GLenum target = nextEnum(r);
1591
+ GLint level = nextInt(r), internalformat = nextInt(r);
1592
+ GLsizei width = nextInt(r), height = nextInt(r), depth = nextInt(r);
1593
+ GLint border = nextInt(r);
1594
+ GLenum format = nextEnum(r), type = nextEnum(r);
1595
+ NSData *holder = nil;
1596
+ BOOL isImage = NO;
1597
+ const void *pixels = [self pixels:nextItem(r)
1598
+ format:format
1599
+ type:type
1600
+ width:width
1601
+ height:height
1602
+ holder:&holder
1603
+ isImage:&isImage];
1604
+ [self withTightUnpacking:isImage
1605
+ upload:^{
1606
+ glTexImage3D(target, level, internalformat, width, height, depth,
1607
+ border, format, type, pixels);
1608
+ }];
1609
+ break;
1610
+ }
1611
+ case ALWebGLOpTexParameterf: {
1612
+ GLenum target = nextEnum(r), pname = nextEnum(r);
1613
+ glTexParameterf(target, pname, nextFloat(r));
1614
+ break;
1615
+ }
1616
+ case ALWebGLOpTexParameteri: {
1617
+ GLenum target = nextEnum(r), pname = nextEnum(r);
1618
+ glTexParameteri(target, pname, nextInt(r));
1619
+ break;
1620
+ }
1621
+ case ALWebGLOpTexStorage2D: {
1622
+ GLenum target = nextEnum(r);
1623
+ GLsizei levels = nextInt(r);
1624
+ GLenum internalformat = nextEnum(r);
1625
+ GLsizei width = nextInt(r);
1626
+ glTexStorage2D(target, levels, internalformat, width, nextInt(r));
1627
+ break;
1628
+ }
1629
+ case ALWebGLOpTexStorage3D: {
1630
+ GLenum target = nextEnum(r);
1631
+ GLsizei levels = nextInt(r);
1632
+ GLenum internalformat = nextEnum(r);
1633
+ GLsizei width = nextInt(r), height = nextInt(r);
1634
+ glTexStorage3D(target, levels, internalformat, width, height, nextInt(r));
1635
+ break;
1636
+ }
1637
+ case ALWebGLOpTexSubImage2D: {
1638
+ GLenum target = nextEnum(r);
1639
+ GLint level = nextInt(r), x = nextInt(r), y = nextInt(r);
1640
+ GLsizei width = nextInt(r), height = nextInt(r);
1641
+ GLenum format = nextEnum(r), type = nextEnum(r);
1642
+ NSData *holder = nil;
1643
+ BOOL isImage = NO;
1644
+ const void *pixels = [self pixels:nextItem(r)
1645
+ format:format
1646
+ type:type
1647
+ width:width
1648
+ height:height
1649
+ holder:&holder
1650
+ isImage:&isImage];
1651
+ [self withTightUnpacking:isImage
1652
+ upload:^{
1653
+ glTexSubImage2D(target, level, x, y, width, height, format, type,
1654
+ pixels);
1655
+ }];
1656
+ break;
1657
+ }
1658
+ case ALWebGLOpTexSubImage3D: {
1659
+ GLenum target = nextEnum(r);
1660
+ GLint level = nextInt(r), x = nextInt(r), y = nextInt(r), z = nextInt(r);
1661
+ GLsizei width = nextInt(r), height = nextInt(r), depth = nextInt(r);
1662
+ GLenum format = nextEnum(r), type = nextEnum(r);
1663
+ NSData *holder = nil;
1664
+ BOOL isImage = NO;
1665
+ const void *pixels = [self pixels:nextItem(r)
1666
+ format:format
1667
+ type:type
1668
+ width:width
1669
+ height:height
1670
+ holder:&holder
1671
+ isImage:&isImage];
1672
+ [self withTightUnpacking:isImage
1673
+ upload:^{
1674
+ glTexSubImage3D(target, level, x, y, z, width, height, depth, format,
1675
+ type, pixels);
1676
+ }];
1677
+ break;
1678
+ }
1679
+ case ALWebGLOpTransformFeedbackVaryings: {
1680
+ GLuint program = [self nameOf:nextUInt(r)];
1681
+ NSArray *varyings = nextArray(r);
1682
+ GLenum mode = nextEnum(r);
1683
+ const char **names = calloc(MAX(varyings.count, 1), sizeof(char *));
1684
+ for (NSUInteger i = 0; i < varyings.count; i++) {
1685
+ names[i] = [varyings[i] isKindOfClass:NSString.class] ? [varyings[i] UTF8String] : "";
1686
+ }
1687
+ glTransformFeedbackVaryings(program, (GLsizei)varyings.count, names, mode);
1688
+ free(names);
1689
+ break;
1690
+ }
1691
+ case ALWebGLOpUniform1f: {
1692
+ GLint location = nextInt(r);
1693
+ glUniform1f(location, nextFloat(r));
1694
+ break;
1695
+ }
1696
+ case ALWebGLOpUniform2f: {
1697
+ GLint location = nextInt(r);
1698
+ GLfloat x = nextFloat(r);
1699
+ glUniform2f(location, x, nextFloat(r));
1700
+ break;
1701
+ }
1702
+ case ALWebGLOpUniform3f: {
1703
+ GLint location = nextInt(r);
1704
+ GLfloat x = nextFloat(r), y = nextFloat(r);
1705
+ glUniform3f(location, x, y, nextFloat(r));
1706
+ break;
1707
+ }
1708
+ case ALWebGLOpUniform4f: {
1709
+ GLint location = nextInt(r);
1710
+ GLfloat x = nextFloat(r), y = nextFloat(r), z = nextFloat(r);
1711
+ glUniform4f(location, x, y, z, nextFloat(r));
1712
+ break;
1713
+ }
1714
+ case ALWebGLOpUniform1i: {
1715
+ GLint location = nextInt(r);
1716
+ glUniform1i(location, nextInt(r));
1717
+ break;
1718
+ }
1719
+ case ALWebGLOpUniform2i: {
1720
+ GLint location = nextInt(r);
1721
+ GLint x = nextInt(r);
1722
+ glUniform2i(location, x, nextInt(r));
1723
+ break;
1724
+ }
1725
+ case ALWebGLOpUniform3i: {
1726
+ GLint location = nextInt(r);
1727
+ GLint x = nextInt(r), y = nextInt(r);
1728
+ glUniform3i(location, x, y, nextInt(r));
1729
+ break;
1730
+ }
1731
+ case ALWebGLOpUniform4i: {
1732
+ GLint location = nextInt(r);
1733
+ GLint x = nextInt(r), y = nextInt(r), z = nextInt(r);
1734
+ glUniform4i(location, x, y, z, nextInt(r));
1735
+ break;
1736
+ }
1737
+ case ALWebGLOpUniform1ui: {
1738
+ GLint location = nextInt(r);
1739
+ glUniform1ui(location, nextUInt(r));
1740
+ break;
1741
+ }
1742
+ case ALWebGLOpUniform2ui: {
1743
+ GLint location = nextInt(r);
1744
+ GLuint x = nextUInt(r);
1745
+ glUniform2ui(location, x, nextUInt(r));
1746
+ break;
1747
+ }
1748
+ case ALWebGLOpUniform3ui: {
1749
+ GLint location = nextInt(r);
1750
+ GLuint x = nextUInt(r), y = nextUInt(r);
1751
+ glUniform3ui(location, x, y, nextUInt(r));
1752
+ break;
1753
+ }
1754
+ case ALWebGLOpUniform4ui: {
1755
+ GLint location = nextInt(r);
1756
+ GLuint x = nextUInt(r), y = nextUInt(r), z = nextUInt(r);
1757
+ glUniform4ui(location, x, y, z, nextUInt(r));
1758
+ break;
1759
+ }
1760
+ case ALWebGLOpUniform1fv:
1761
+ case ALWebGLOpUniform2fv:
1762
+ case ALWebGLOpUniform3fv:
1763
+ case ALWebGLOpUniform4fv: {
1764
+ GLint location = nextInt(r);
1765
+ NSArray *values = nextArray(r);
1766
+ GLsizei size = (GLsizei)(op - ALWebGLOpUniform1fv + 1);
1767
+ GLsizei count = (GLsizei)values.count / size;
1768
+ if (count == 0) break;
1769
+ NSData *data = floatsFrom(values);
1770
+ if (size == 1) glUniform1fv(location, count, data.bytes);
1771
+ else if (size == 2) glUniform2fv(location, count, data.bytes);
1772
+ else if (size == 3) glUniform3fv(location, count, data.bytes);
1773
+ else glUniform4fv(location, count, data.bytes);
1774
+ break;
1775
+ }
1776
+ case ALWebGLOpUniform1iv:
1777
+ case ALWebGLOpUniform2iv:
1778
+ case ALWebGLOpUniform3iv:
1779
+ case ALWebGLOpUniform4iv: {
1780
+ GLint location = nextInt(r);
1781
+ NSArray *values = nextArray(r);
1782
+ GLsizei size = (GLsizei)(op - ALWebGLOpUniform1iv + 1);
1783
+ GLsizei count = (GLsizei)values.count / size;
1784
+ if (count == 0) break;
1785
+ NSData *data = intsFrom(values);
1786
+ if (size == 1) glUniform1iv(location, count, data.bytes);
1787
+ else if (size == 2) glUniform2iv(location, count, data.bytes);
1788
+ else if (size == 3) glUniform3iv(location, count, data.bytes);
1789
+ else glUniform4iv(location, count, data.bytes);
1790
+ break;
1791
+ }
1792
+ case ALWebGLOpUniform1uiv:
1793
+ case ALWebGLOpUniform2uiv:
1794
+ case ALWebGLOpUniform3uiv:
1795
+ case ALWebGLOpUniform4uiv: {
1796
+ GLint location = nextInt(r);
1797
+ NSArray *values = nextArray(r);
1798
+ GLsizei size = (GLsizei)(op - ALWebGLOpUniform1uiv + 1);
1799
+ GLsizei count = (GLsizei)values.count / size;
1800
+ if (count == 0) break;
1801
+ NSData *data = uintsFrom(values);
1802
+ if (size == 1) glUniform1uiv(location, count, data.bytes);
1803
+ else if (size == 2) glUniform2uiv(location, count, data.bytes);
1804
+ else if (size == 3) glUniform3uiv(location, count, data.bytes);
1805
+ else glUniform4uiv(location, count, data.bytes);
1806
+ break;
1807
+ }
1808
+ case ALWebGLOpUniformMatrix2fv:
1809
+ case ALWebGLOpUniformMatrix3fv:
1810
+ case ALWebGLOpUniformMatrix4fv:
1811
+ case ALWebGLOpUniformMatrix2x3fv:
1812
+ case ALWebGLOpUniformMatrix3x2fv:
1813
+ case ALWebGLOpUniformMatrix2x4fv:
1814
+ case ALWebGLOpUniformMatrix4x2fv:
1815
+ case ALWebGLOpUniformMatrix3x4fv:
1816
+ case ALWebGLOpUniformMatrix4x3fv: {
1817
+ GLint location = nextInt(r);
1818
+ GLboolean transpose = nextInt(r) != 0;
1819
+ NSArray *values = nextArray(r);
1820
+ NSData *data = floatsFrom(values);
1821
+ const GLfloat *v = data.bytes;
1822
+ GLsizei n = (GLsizei)values.count;
1823
+ switch ((ALWebGLOp)op) {
1824
+ case ALWebGLOpUniformMatrix2fv:
1825
+ if (n / 4) glUniformMatrix2fv(location, n / 4, transpose, v);
1826
+ break;
1827
+ case ALWebGLOpUniformMatrix3fv:
1828
+ if (n / 9) glUniformMatrix3fv(location, n / 9, transpose, v);
1829
+ break;
1830
+ case ALWebGLOpUniformMatrix4fv:
1831
+ if (n / 16) glUniformMatrix4fv(location, n / 16, transpose, v);
1832
+ break;
1833
+ case ALWebGLOpUniformMatrix2x3fv:
1834
+ if (n / 6) glUniformMatrix2x3fv(location, n / 6, transpose, v);
1835
+ break;
1836
+ case ALWebGLOpUniformMatrix3x2fv:
1837
+ if (n / 6) glUniformMatrix3x2fv(location, n / 6, transpose, v);
1838
+ break;
1839
+ case ALWebGLOpUniformMatrix2x4fv:
1840
+ if (n / 8) glUniformMatrix2x4fv(location, n / 8, transpose, v);
1841
+ break;
1842
+ case ALWebGLOpUniformMatrix4x2fv:
1843
+ if (n / 8) glUniformMatrix4x2fv(location, n / 8, transpose, v);
1844
+ break;
1845
+ case ALWebGLOpUniformMatrix3x4fv:
1846
+ if (n / 12) glUniformMatrix3x4fv(location, n / 12, transpose, v);
1847
+ break;
1848
+ default:
1849
+ if (n / 12) glUniformMatrix4x3fv(location, n / 12, transpose, v);
1850
+ break;
1851
+ }
1852
+ break;
1853
+ }
1854
+ case ALWebGLOpUniformBlockBinding: {
1855
+ GLuint program = [self nameOf:nextUInt(r)];
1856
+ GLuint index = nextUInt(r);
1857
+ glUniformBlockBinding(program, index, nextUInt(r));
1858
+ break;
1859
+ }
1860
+ case ALWebGLOpUseProgram:
1861
+ glUseProgram([self nameOf:nextUInt(r)]);
1862
+ break;
1863
+ case ALWebGLOpValidateProgram:
1864
+ glValidateProgram([self nameOf:nextUInt(r)]);
1865
+ break;
1866
+ case ALWebGLOpVertexAttrib4f: {
1867
+ GLuint index = nextUInt(r);
1868
+ GLfloat x = nextFloat(r), y = nextFloat(r), z = nextFloat(r);
1869
+ glVertexAttrib4f(index, x, y, z, nextFloat(r));
1870
+ break;
1871
+ }
1872
+ case ALWebGLOpVertexAttribI4i: {
1873
+ GLuint index = nextUInt(r);
1874
+ GLint x = nextInt(r), y = nextInt(r), z = nextInt(r);
1875
+ glVertexAttribI4i(index, x, y, z, nextInt(r));
1876
+ break;
1877
+ }
1878
+ case ALWebGLOpVertexAttribI4ui: {
1879
+ GLuint index = nextUInt(r);
1880
+ GLuint x = nextUInt(r), y = nextUInt(r), z = nextUInt(r);
1881
+ glVertexAttribI4ui(index, x, y, z, nextUInt(r));
1882
+ break;
1883
+ }
1884
+ case ALWebGLOpVertexAttribDivisor: {
1885
+ GLuint index = nextUInt(r);
1886
+ glVertexAttribDivisor(index, nextUInt(r));
1887
+ break;
1888
+ }
1889
+ case ALWebGLOpVertexAttribIPointer: {
1890
+ GLuint index = nextUInt(r);
1891
+ GLint size = nextInt(r);
1892
+ GLenum type = nextEnum(r);
1893
+ GLsizei stride = nextInt(r);
1894
+ glVertexAttribIPointer(index, size, type, stride, (const void *)nextOffset(r));
1895
+ break;
1896
+ }
1897
+ case ALWebGLOpVertexAttribPointer: {
1898
+ GLuint index = nextUInt(r);
1899
+ GLint size = nextInt(r);
1900
+ GLenum type = nextEnum(r);
1901
+ GLboolean normalized = nextInt(r) != 0;
1902
+ GLsizei stride = nextInt(r);
1903
+ glVertexAttribPointer(index, size, type, normalized, stride, (const void *)nextOffset(r));
1904
+ break;
1905
+ }
1906
+ case ALWebGLOpViewport: {
1907
+ GLint x = nextInt(r), y = nextInt(r);
1908
+ GLsizei width = nextInt(r);
1909
+ glViewport(x, y, width, nextInt(r));
1910
+ break;
1911
+ }
1912
+ case ALWebGLOpWaitSync: {
1913
+ GLsync sync = [self syncOf:nextUInt(r)];
1914
+ GLbitfield flags = nextUInt(r);
1915
+ nextNumber(r); // WebGL's timeout is always TIMEOUT_IGNORED (-1).
1916
+ if (sync) glWaitSync(sync, flags, GL_TIMEOUT_IGNORED);
1917
+ break;
1918
+ }
1919
+ case ALWebGLOpResize: {
1920
+ GLsizei width = nextInt(r);
1921
+ [self allocateDrawingBufferWidth:width height:nextInt(r)];
1922
+ break;
1923
+ }
1924
+ case ALWebGLOpPresent:
1925
+ [self present];
1926
+ break;
1927
+ default:
1928
+ NSLog(@"[AngularLynxWebGL] Unknown command %ld", (long)op);
1929
+ // The rest of the batch can't be read without this one's arguments.
1930
+ r->index = r->count;
1931
+ break;
1932
+ }
1933
+ }
1934
+
1935
+ - (id)query:(NSInteger)op reader:(Reader *)r {
1936
+ switch ((ALWebGLOp)op) {
1937
+ case ALWebGLOpGetParameter:
1938
+ return [self parameter:nextEnum(r)];
1939
+ case ALWebGLOpGetError:
1940
+ return @(glGetError());
1941
+ case ALWebGLOpShaderInfo: {
1942
+ GLuint shader = [self nameOf:nextUInt(r)];
1943
+ GLint compiled = GL_FALSE;
1944
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
1945
+ return @{@"compiled" : @(compiled == GL_TRUE), @"log" : shaderLog(shader)};
1946
+ }
1947
+ case ALWebGLOpProgramInfo:
1948
+ return [self programInfo:[self nameOf:nextUInt(r)]];
1949
+ case ALWebGLOpGetUniformLocation: {
1950
+ GLuint program = [self nameOf:nextUInt(r)];
1951
+ return @(glGetUniformLocation(program, nextString(r).UTF8String));
1952
+ }
1953
+ case ALWebGLOpGetAttribLocation: {
1954
+ GLuint program = [self nameOf:nextUInt(r)];
1955
+ return @(glGetAttribLocation(program, nextString(r).UTF8String));
1956
+ }
1957
+ case ALWebGLOpCheckFramebufferStatus:
1958
+ return @(glCheckFramebufferStatus(nextEnum(r)));
1959
+ case ALWebGLOpGetBufferParameter: {
1960
+ GLenum target = nextEnum(r), pname = nextEnum(r);
1961
+ if (pname == GL_BUFFER_SIZE) {
1962
+ GLint64 value = 0;
1963
+ glGetBufferParameteri64v(target, pname, &value);
1964
+ return @(value);
1965
+ }
1966
+ GLint value = 0;
1967
+ glGetBufferParameteriv(target, pname, &value);
1968
+ return @(value);
1969
+ }
1970
+ case ALWebGLOpGetFramebufferAttachmentParameter: {
1971
+ GLenum target = nextEnum(r);
1972
+ GLenum attachment = [self attachmentFor:nextEnum(r) target:target];
1973
+ GLint value = 0;
1974
+ glGetFramebufferAttachmentParameteriv(target, attachment, nextEnum(r), &value);
1975
+ return @(value);
1976
+ }
1977
+ case ALWebGLOpGetRenderbufferParameter: {
1978
+ GLenum target = nextEnum(r);
1979
+ GLint value = 0;
1980
+ glGetRenderbufferParameteriv(target, nextEnum(r), &value);
1981
+ return @(value);
1982
+ }
1983
+ case ALWebGLOpGetTexParameter: {
1984
+ GLenum target = nextEnum(r), pname = nextEnum(r);
1985
+ if (pname == GL_TEXTURE_MAX_LOD || pname == GL_TEXTURE_MIN_LOD ||
1986
+ pname == GL_TEXTURE_MAX_ANISOTROPY) {
1987
+ GLfloat value = 0;
1988
+ glGetTexParameterfv(target, pname, &value);
1989
+ return @(value);
1990
+ }
1991
+ GLint value = 0;
1992
+ glGetTexParameteriv(target, pname, &value);
1993
+ return @(value);
1994
+ }
1995
+ case ALWebGLOpGetSamplerParameter: {
1996
+ GLuint sampler = [self nameOf:nextUInt(r)];
1997
+ GLenum pname = nextEnum(r);
1998
+ if (pname == GL_TEXTURE_MAX_LOD || pname == GL_TEXTURE_MIN_LOD) {
1999
+ GLfloat value = 0;
2000
+ glGetSamplerParameterfv(sampler, pname, &value);
2001
+ return @(value);
2002
+ }
2003
+ GLint value = 0;
2004
+ glGetSamplerParameteriv(sampler, pname, &value);
2005
+ return @(value);
2006
+ }
2007
+ case ALWebGLOpGetVertexAttrib: {
2008
+ GLuint index = nextUInt(r);
2009
+ GLenum pname = nextEnum(r);
2010
+ if (pname == GL_CURRENT_VERTEX_ATTRIB) {
2011
+ GLfloat values[4] = {0, 0, 0, 1};
2012
+ glGetVertexAttribfv(index, pname, values);
2013
+ return numbersFromFloats(values, 4);
2014
+ }
2015
+ GLint value = 0;
2016
+ glGetVertexAttribiv(index, pname, &value);
2017
+ return @(value);
2018
+ }
2019
+ case ALWebGLOpGetVertexAttribOffset: {
2020
+ GLuint index = nextUInt(r);
2021
+ void *pointer = NULL;
2022
+ glGetVertexAttribPointerv(index, nextEnum(r), &pointer);
2023
+ return @((intptr_t)pointer);
2024
+ }
2025
+ case ALWebGLOpGetIndexedParameter: {
2026
+ GLenum target = nextEnum(r);
2027
+ GLuint index = nextUInt(r);
2028
+ if (target == GL_UNIFORM_BUFFER_START || target == GL_UNIFORM_BUFFER_SIZE ||
2029
+ target == GL_TRANSFORM_FEEDBACK_BUFFER_START ||
2030
+ target == GL_TRANSFORM_FEEDBACK_BUFFER_SIZE) {
2031
+ GLint64 value = 0;
2032
+ glGetInteger64i_v(target, index, &value);
2033
+ return @(value);
2034
+ }
2035
+ GLint value = 0;
2036
+ glGetIntegeri_v(target, index, &value);
2037
+ return @(value);
2038
+ }
2039
+ case ALWebGLOpGetInternalformatParameter: {
2040
+ GLenum target = nextEnum(r), internalformat = nextEnum(r), pname = nextEnum(r);
2041
+ GLint count = 0;
2042
+ glGetInternalformativ(target, internalformat, GL_NUM_SAMPLE_COUNTS, 1, &count);
2043
+ GLint *values = calloc(MAX(count, 1), sizeof(GLint));
2044
+ glGetInternalformativ(target, internalformat, pname, count, values);
2045
+ NSArray *result = numbersFromInts(values, count);
2046
+ free(values);
2047
+ return result;
2048
+ }
2049
+ case ALWebGLOpGetQueryParameter: {
2050
+ GLuint query = [self nameOf:nextUInt(r)];
2051
+ GLuint value = 0;
2052
+ glGetQueryObjectuiv(query, nextEnum(r), &value);
2053
+ return @(value);
2054
+ }
2055
+ case ALWebGLOpGetSyncParameter: {
2056
+ GLsync sync = [self syncOf:nextUInt(r)];
2057
+ GLenum pname = nextEnum(r);
2058
+ if (!sync) return nil;
2059
+ GLint value = 0;
2060
+ glGetSynciv(sync, pname, 1, NULL, &value);
2061
+ return @(value);
2062
+ }
2063
+ case ALWebGLOpClientWaitSync: {
2064
+ GLsync sync = [self syncOf:nextUInt(r)];
2065
+ GLbitfield flags = nextUInt(r);
2066
+ GLuint64 timeout = (GLuint64)MAX(0, nextNumber(r));
2067
+ if (!sync) return @(GL_WAIT_FAILED);
2068
+ return @(glClientWaitSync(sync, flags, timeout));
2069
+ }
2070
+ case ALWebGLOpGetUniform: {
2071
+ GLuint program = [self nameOf:nextUInt(r)];
2072
+ GLint location = nextInt(r);
2073
+ GLenum type = nextEnum(r);
2074
+ GLint count = uniformComponents(type);
2075
+ if (isFloatUniform(type)) {
2076
+ GLfloat values[16] = {0};
2077
+ glGetUniformfv(program, location, values);
2078
+ return numbersFromFloats(values, count);
2079
+ }
2080
+ if (isUnsignedUniform(type)) {
2081
+ GLuint values[4] = {0};
2082
+ glGetUniformuiv(program, location, values);
2083
+ NSMutableArray *result = [NSMutableArray array];
2084
+ for (GLint i = 0; i < count; i++) [result addObject:@(values[i])];
2085
+ return result;
2086
+ }
2087
+ GLint values[4] = {0};
2088
+ glGetUniformiv(program, location, values);
2089
+ return numbersFromInts(values, count);
2090
+ }
2091
+ case ALWebGLOpGetUniformBlockIndex: {
2092
+ GLuint program = [self nameOf:nextUInt(r)];
2093
+ return @(glGetUniformBlockIndex(program, nextString(r).UTF8String));
2094
+ }
2095
+ case ALWebGLOpGetActiveUniformBlockName: {
2096
+ GLuint program = [self nameOf:nextUInt(r)];
2097
+ GLuint index = nextUInt(r);
2098
+ GLint length = 0;
2099
+ glGetActiveUniformBlockiv(program, index, GL_UNIFORM_BLOCK_NAME_LENGTH, &length);
2100
+ char *name = calloc(MAX(length, 1) + 1, 1);
2101
+ glGetActiveUniformBlockName(program, index, length + 1, NULL, name);
2102
+ NSString *result = [NSString stringWithUTF8String:name] ?: @"";
2103
+ free(name);
2104
+ return result;
2105
+ }
2106
+ case ALWebGLOpGetActiveUniformBlockParameter: {
2107
+ GLuint program = [self nameOf:nextUInt(r)];
2108
+ GLuint index = nextUInt(r);
2109
+ GLenum pname = nextEnum(r);
2110
+ if (pname == GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES) {
2111
+ GLint count = 0;
2112
+ glGetActiveUniformBlockiv(program, index, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &count);
2113
+ GLint *values = calloc(MAX(count, 1), sizeof(GLint));
2114
+ glGetActiveUniformBlockiv(program, index, pname, values);
2115
+ NSArray *result = numbersFromInts(values, count);
2116
+ free(values);
2117
+ return result;
2118
+ }
2119
+ GLint value = 0;
2120
+ glGetActiveUniformBlockiv(program, index, pname, &value);
2121
+ return @(value);
2122
+ }
2123
+ case ALWebGLOpGetActiveUniforms: {
2124
+ GLuint program = [self nameOf:nextUInt(r)];
2125
+ NSArray *indices = nextArray(r);
2126
+ GLenum pname = nextEnum(r);
2127
+ NSData *indexData = uintsFrom(indices);
2128
+ GLint *values = calloc(MAX(indices.count, 1), sizeof(GLint));
2129
+ glGetActiveUniformsiv(program, (GLsizei)indices.count, indexData.bytes, pname, values);
2130
+ NSArray *result = numbersFromInts(values, indices.count);
2131
+ free(values);
2132
+ return result;
2133
+ }
2134
+ case ALWebGLOpGetUniformIndices: {
2135
+ GLuint program = [self nameOf:nextUInt(r)];
2136
+ NSArray *names = nextArray(r);
2137
+ const char **cNames = calloc(MAX(names.count, 1), sizeof(char *));
2138
+ for (NSUInteger i = 0; i < names.count; i++) {
2139
+ cNames[i] = [names[i] isKindOfClass:NSString.class] ? [names[i] UTF8String] : "";
2140
+ }
2141
+ GLuint *indices = calloc(MAX(names.count, 1), sizeof(GLuint));
2142
+ glGetUniformIndices(program, (GLsizei)names.count, cNames, indices);
2143
+ NSMutableArray *result = [NSMutableArray array];
2144
+ for (NSUInteger i = 0; i < names.count; i++) [result addObject:@(indices[i])];
2145
+ free(cNames);
2146
+ free(indices);
2147
+ return result;
2148
+ }
2149
+ case ALWebGLOpGetFragDataLocation: {
2150
+ GLuint program = [self nameOf:nextUInt(r)];
2151
+ return @(glGetFragDataLocation(program, nextString(r).UTF8String));
2152
+ }
2153
+ case ALWebGLOpGetTransformFeedbackVarying: {
2154
+ GLuint program = [self nameOf:nextUInt(r)];
2155
+ GLuint index = nextUInt(r);
2156
+ GLint maxLength = 0;
2157
+ glGetProgramiv(program, GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH, &maxLength);
2158
+ char *name = calloc(MAX(maxLength, 1) + 1, 1);
2159
+ GLsizei length = 0, size = 0;
2160
+ GLenum type = 0;
2161
+ glGetTransformFeedbackVarying(program, index, maxLength + 1, &length, &size, &type, name);
2162
+ NSString *result = [NSString stringWithUTF8String:name] ?: @"";
2163
+ free(name);
2164
+ return @[ result, @(size), @(type) ];
2165
+ }
2166
+ case ALWebGLOpReadPixels: {
2167
+ GLint x = nextInt(r), y = nextInt(r);
2168
+ GLsizei width = nextInt(r), height = nextInt(r);
2169
+ GLenum format = nextEnum(r), type = nextEnum(r);
2170
+ NSUInteger length = (NSUInteger)MAX(0, nextNumber(r));
2171
+ NSMutableData *data = [NSMutableData dataWithLength:(length + 3) / 4 * 4];
2172
+ BOOL resolved = [self beginRead];
2173
+ glReadPixels(x, y, width, height, format, type, data.mutableBytes);
2174
+ [self endRead:resolved];
2175
+ return wordsFromBytes(data.bytes, length);
2176
+ }
2177
+ case ALWebGLOpGetBufferSubData: {
2178
+ GLenum target = nextEnum(r);
2179
+ GLintptr offset = nextOffset(r);
2180
+ GLsizeiptr length = (GLsizeiptr)nextOffset(r);
2181
+ if (length <= 0) return wordsFromBytes(NULL, 0);
2182
+ void *mapped = glMapBufferRange(target, offset, length, GL_MAP_READ_BIT);
2183
+ if (!mapped) return nil;
2184
+ NSArray *result = wordsFromBytes(mapped, (NSUInteger)length);
2185
+ glUnmapBuffer(target);
2186
+ return result;
2187
+ }
2188
+ case ALWebGLOpIsEnabled:
2189
+ return @(glIsEnabled(nextEnum(r)) == GL_TRUE);
2190
+ case ALWebGLOpGetShaderPrecisionFormat: {
2191
+ GLenum shaderType = nextEnum(r), precisionType = nextEnum(r);
2192
+ GLint range[2] = {0, 0};
2193
+ GLint precision = 0;
2194
+ glGetShaderPrecisionFormat(shaderType, precisionType, range, &precision);
2195
+ return @[ @(range[0]), @(range[1]), @(precision) ];
2196
+ }
2197
+ case ALWebGLOpGetSupportedExtensions: {
2198
+ GLint count = 0;
2199
+ glGetIntegerv(GL_NUM_EXTENSIONS, &count);
2200
+ NSMutableArray *extensions = [NSMutableArray arrayWithCapacity:count];
2201
+ for (GLint i = 0; i < count; i++) {
2202
+ const GLubyte *name = glGetStringi(GL_EXTENSIONS, i);
2203
+ if (name) [extensions addObject:@((const char *)name)];
2204
+ }
2205
+ return extensions;
2206
+ }
2207
+ case ALWebGLOpGetProgramParameter: {
2208
+ GLuint program = [self nameOf:nextUInt(r)];
2209
+ GLint value = 0;
2210
+ glGetProgramiv(program, nextEnum(r), &value);
2211
+ return @(value);
2212
+ }
2213
+ default:
2214
+ NSLog(@"[AngularLynxWebGL] Unknown query %ld", (long)op);
2215
+ r->index = r->count;
2216
+ return nil;
2217
+ }
2218
+ }
2219
+
2220
+ @end