@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.
- package/README.md +2 -0
- package/package.json +2 -1
- package/templates/android/README.md +6 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AssetsModule.kt +41 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +76 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +9 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +1442 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +220 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLOps.kt +211 -0
- package/templates/ios/App/AngularLynxWebGL.h +80 -0
- package/templates/ios/App/AngularLynxWebGL.m +338 -0
- package/templates/ios/App/AngularLynxWebGLContext.m +2220 -0
- package/templates/ios/App/AngularLynxWebGLOps.h +209 -0
- package/templates/ios/App/AssetsModule.swift +53 -0
- package/templates/ios/App/LynxViewController.swift +11 -0
- package/templates/ios/App/XElementRegistry.swift +6 -1
|
@@ -0,0 +1,1442 @@
|
|
|
1
|
+
package dev.angularlynx.shell
|
|
2
|
+
|
|
3
|
+
import android.opengl.EGL14
|
|
4
|
+
import android.opengl.EGLConfig
|
|
5
|
+
import android.opengl.EGLContext
|
|
6
|
+
import android.opengl.EGLDisplay
|
|
7
|
+
import android.opengl.EGLSurface
|
|
8
|
+
import android.opengl.GLES30
|
|
9
|
+
import android.util.Log
|
|
10
|
+
import com.lynx.react.bridge.JavaOnlyArray
|
|
11
|
+
import com.lynx.react.bridge.JavaOnlyMap
|
|
12
|
+
import com.lynx.react.bridge.ReadableArray
|
|
13
|
+
import com.lynx.react.bridge.ReadableMap
|
|
14
|
+
import com.lynx.react.bridge.ReadableType
|
|
15
|
+
import java.nio.Buffer
|
|
16
|
+
import java.nio.ByteBuffer
|
|
17
|
+
import java.nio.ByteOrder
|
|
18
|
+
|
|
19
|
+
// WebGL's names for the default framebuffer's buffers, which GLES gives to
|
|
20
|
+
// window surfaces only. The default framebuffer here is an ordinary one.
|
|
21
|
+
private const val WEBGL_BACK = 0x0405
|
|
22
|
+
private const val WEBGL_COLOR = 0x1800
|
|
23
|
+
private const val WEBGL_DEPTH = 0x1801
|
|
24
|
+
private const val WEBGL_STENCIL = 0x1802
|
|
25
|
+
private const val WEBGL_UNMASKED_VENDOR = 0x9245
|
|
26
|
+
private const val WEBGL_UNMASKED_RENDERER = 0x9246
|
|
27
|
+
private const val TEXTURE_MAX_ANISOTROPY = 0x84FE
|
|
28
|
+
private const val MAX_TEXTURE_MAX_ANISOTROPY = 0x84FF
|
|
29
|
+
private const val EGL_OPENGL_ES3_BIT = 0x40
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* One WebGL context: an EGL context with OpenGL ES 3.0, and an offscreen
|
|
33
|
+
* default framebuffer the size of the canvas's `width` and `height`, which
|
|
34
|
+
* each finished frame is copied from into the canvas's TextureView.
|
|
35
|
+
*
|
|
36
|
+
* The runtime's `run` calls arrive on the UI thread, which makes the context
|
|
37
|
+
* current for each and puts back whatever was current before.
|
|
38
|
+
*/
|
|
39
|
+
class WebGLContext(val canvasId: String, attributes: ReadableMap?) {
|
|
40
|
+
private val display: EGLDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY)
|
|
41
|
+
private val config: EGLConfig
|
|
42
|
+
private var context: EGLContext
|
|
43
|
+
/** A 1x1 surface to be current on while there's no window. */
|
|
44
|
+
private var pbuffer: EGLSurface
|
|
45
|
+
private var windowSurface: EGLSurface = EGL14.EGL_NO_SURFACE
|
|
46
|
+
private var windowTexture: Any? = null
|
|
47
|
+
|
|
48
|
+
private val alpha = attributes.flag("alpha", true)
|
|
49
|
+
private val depth = attributes.flag("depth", true)
|
|
50
|
+
private val stencil = attributes.flag("stencil", false)
|
|
51
|
+
private val preserveDrawingBuffer = attributes.flag("preserveDrawingBuffer", false)
|
|
52
|
+
private var samples = 0
|
|
53
|
+
|
|
54
|
+
// The default framebuffer, multisampled when antialias is on, and the
|
|
55
|
+
// single-sampled framebuffer it resolves into before it's shown.
|
|
56
|
+
private var framebuffer = 0
|
|
57
|
+
private var colorBuffer = 0
|
|
58
|
+
private var depthStencilBuffer = 0
|
|
59
|
+
private var resolveFramebuffer = 0
|
|
60
|
+
private var resolveBuffer = 0
|
|
61
|
+
private var width = 1
|
|
62
|
+
private var height = 1
|
|
63
|
+
|
|
64
|
+
private var view: CanvasUI? = null
|
|
65
|
+
/** A frame was finished before there was a surface to show it on. */
|
|
66
|
+
private var framePending = false
|
|
67
|
+
|
|
68
|
+
// The app's bindings, which drawing the frame changes and then restores.
|
|
69
|
+
private var drawFramebuffer = 0
|
|
70
|
+
private var readFramebuffer = 0
|
|
71
|
+
private var renderbuffer = 0
|
|
72
|
+
|
|
73
|
+
// The runtime's object ids, to GL names (ids are never reused).
|
|
74
|
+
private var names = IntArray(256)
|
|
75
|
+
private val syncs = HashMap<Int, Long>()
|
|
76
|
+
private var destroyed = false
|
|
77
|
+
|
|
78
|
+
init {
|
|
79
|
+
val version = IntArray(2)
|
|
80
|
+
if (!EGL14.eglInitialize(display, version, 0, version, 1)) {
|
|
81
|
+
throw RuntimeException("EGL is not available")
|
|
82
|
+
}
|
|
83
|
+
val configs = arrayOfNulls<EGLConfig>(1)
|
|
84
|
+
val count = IntArray(1)
|
|
85
|
+
val configAttributes = intArrayOf(
|
|
86
|
+
EGL14.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
|
|
87
|
+
EGL14.EGL_SURFACE_TYPE, EGL14.EGL_WINDOW_BIT or EGL14.EGL_PBUFFER_BIT,
|
|
88
|
+
EGL14.EGL_RED_SIZE, 8,
|
|
89
|
+
EGL14.EGL_GREEN_SIZE, 8,
|
|
90
|
+
EGL14.EGL_BLUE_SIZE, 8,
|
|
91
|
+
EGL14.EGL_ALPHA_SIZE, 8,
|
|
92
|
+
EGL14.EGL_NONE,
|
|
93
|
+
)
|
|
94
|
+
if (!EGL14.eglChooseConfig(display, configAttributes, 0, configs, 0, 1, count, 0) || count[0] == 0) {
|
|
95
|
+
throw RuntimeException("This device has no OpenGL ES 3.0")
|
|
96
|
+
}
|
|
97
|
+
config = configs[0]!!
|
|
98
|
+
context = EGL14.eglCreateContext(
|
|
99
|
+
display, config, EGL14.EGL_NO_CONTEXT,
|
|
100
|
+
intArrayOf(EGL14.EGL_CONTEXT_CLIENT_VERSION, 3, EGL14.EGL_NONE), 0,
|
|
101
|
+
)
|
|
102
|
+
if (context == EGL14.EGL_NO_CONTEXT) throw RuntimeException("This device has no OpenGL ES 3.0")
|
|
103
|
+
pbuffer = EGL14.eglCreatePbufferSurface(
|
|
104
|
+
display, config, intArrayOf(EGL14.EGL_WIDTH, 1, EGL14.EGL_HEIGHT, 1, EGL14.EGL_NONE), 0,
|
|
105
|
+
)
|
|
106
|
+
withCurrent {
|
|
107
|
+
samples = if (attributes.flag("antialias", true)) {
|
|
108
|
+
val max = IntArray(1)
|
|
109
|
+
GLES30.glGetIntegerv(GLES30.GL_MAX_SAMPLES, max, 0)
|
|
110
|
+
minOf(4, max[0])
|
|
111
|
+
} else {
|
|
112
|
+
0
|
|
113
|
+
}
|
|
114
|
+
framebuffer = genFramebuffer()
|
|
115
|
+
colorBuffer = genRenderbuffer()
|
|
116
|
+
if (depth || stencil) depthStencilBuffer = genRenderbuffer()
|
|
117
|
+
if (samples > 0) {
|
|
118
|
+
resolveFramebuffer = genFramebuffer()
|
|
119
|
+
resolveBuffer = genRenderbuffer()
|
|
120
|
+
}
|
|
121
|
+
drawFramebuffer = framebuffer
|
|
122
|
+
readFramebuffer = framebuffer
|
|
123
|
+
allocateDrawingBuffer(1, 1)
|
|
124
|
+
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, framebuffer)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
fun destroy() {
|
|
129
|
+
if (destroyed) return
|
|
130
|
+
withCurrent {
|
|
131
|
+
GLES30.glDeleteFramebuffers(1, intArrayOf(framebuffer), 0)
|
|
132
|
+
GLES30.glDeleteRenderbuffers(1, intArrayOf(colorBuffer), 0)
|
|
133
|
+
if (depthStencilBuffer != 0) GLES30.glDeleteRenderbuffers(1, intArrayOf(depthStencilBuffer), 0)
|
|
134
|
+
if (resolveFramebuffer != 0) GLES30.glDeleteFramebuffers(1, intArrayOf(resolveFramebuffer), 0)
|
|
135
|
+
if (resolveBuffer != 0) GLES30.glDeleteRenderbuffers(1, intArrayOf(resolveBuffer), 0)
|
|
136
|
+
for (sync in syncs.values) GLES30.glDeleteSync(sync)
|
|
137
|
+
syncs.clear()
|
|
138
|
+
}
|
|
139
|
+
destroyed = true
|
|
140
|
+
releaseWindowSurface()
|
|
141
|
+
EGL14.eglDestroySurface(display, pbuffer)
|
|
142
|
+
EGL14.eglDestroyContext(display, context)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// --- Current context -----------------------------------------------------
|
|
146
|
+
|
|
147
|
+
/** Runs GL calls with this context current, then puts back the previous one. */
|
|
148
|
+
private inline fun <T> withCurrent(block: () -> T): T {
|
|
149
|
+
val previousContext = EGL14.eglGetCurrentContext()
|
|
150
|
+
val previousDraw = EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW)
|
|
151
|
+
val previousRead = EGL14.eglGetCurrentSurface(EGL14.EGL_READ)
|
|
152
|
+
val surface = if (windowSurface != EGL14.EGL_NO_SURFACE) windowSurface else pbuffer
|
|
153
|
+
val switched = previousContext != context || previousDraw != surface
|
|
154
|
+
if (switched) EGL14.eglMakeCurrent(display, surface, surface, context)
|
|
155
|
+
try {
|
|
156
|
+
return block()
|
|
157
|
+
} finally {
|
|
158
|
+
if (switched) {
|
|
159
|
+
if (previousContext == EGL14.EGL_NO_CONTEXT) {
|
|
160
|
+
EGL14.eglMakeCurrent(display, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT)
|
|
161
|
+
} else {
|
|
162
|
+
EGL14.eglMakeCurrent(EGL14.eglGetCurrentDisplay(), previousDraw, previousRead, previousContext)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// --- The view ------------------------------------------------------------
|
|
169
|
+
|
|
170
|
+
fun attachView(canvas: CanvasUI?) {
|
|
171
|
+
if (view === canvas) return
|
|
172
|
+
view = canvas
|
|
173
|
+
canvas?.setOpaque(!alpha)
|
|
174
|
+
surfaceChanged()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** The view's surface came, went or changed size. */
|
|
178
|
+
fun surfaceChanged() {
|
|
179
|
+
if (destroyed) return
|
|
180
|
+
val texture = view?.surface
|
|
181
|
+
if (texture !== windowTexture) {
|
|
182
|
+
releaseWindowSurface()
|
|
183
|
+
if (texture != null) {
|
|
184
|
+
windowSurface = EGL14.eglCreateWindowSurface(display, config, texture, intArrayOf(EGL14.EGL_NONE), 0)
|
|
185
|
+
if (windowSurface == EGL14.EGL_NO_SURFACE) {
|
|
186
|
+
Log.w(TAG, "Could not make an EGL surface for the canvas")
|
|
187
|
+
} else {
|
|
188
|
+
windowTexture = texture
|
|
189
|
+
withCurrent {
|
|
190
|
+
// Presenting never waits for the display: it runs on
|
|
191
|
+
// the UI thread.
|
|
192
|
+
EGL14.eglSwapInterval(display, 0)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (framePending) present()
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private fun releaseWindowSurface() {
|
|
201
|
+
if (windowSurface == EGL14.EGL_NO_SURFACE) return
|
|
202
|
+
if (EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW) == windowSurface) {
|
|
203
|
+
EGL14.eglMakeCurrent(display, pbuffer, pbuffer, context)
|
|
204
|
+
}
|
|
205
|
+
EGL14.eglDestroySurface(display, windowSurface)
|
|
206
|
+
windowSurface = EGL14.EGL_NO_SURFACE
|
|
207
|
+
windowTexture = null
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// --- Default framebuffer -------------------------------------------------
|
|
211
|
+
|
|
212
|
+
private fun colorFormat() = if (alpha) GLES30.GL_RGBA8 else GLES30.GL_RGB8
|
|
213
|
+
|
|
214
|
+
private fun depthStencilFormat() = when {
|
|
215
|
+
depth && stencil -> GLES30.GL_DEPTH24_STENCIL8
|
|
216
|
+
depth -> GLES30.GL_DEPTH_COMPONENT24
|
|
217
|
+
else -> GLES30.GL_STENCIL_INDEX8
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** (Re)allocates the default framebuffer at the canvas's size, cleared. */
|
|
221
|
+
private fun allocateDrawingBuffer(newWidth: Int, newHeight: Int) {
|
|
222
|
+
width = maxOf(1, newWidth)
|
|
223
|
+
height = maxOf(1, newHeight)
|
|
224
|
+
storage(colorBuffer, colorFormat(), samples)
|
|
225
|
+
if (depthStencilBuffer != 0) storage(depthStencilBuffer, depthStencilFormat(), samples)
|
|
226
|
+
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, framebuffer)
|
|
227
|
+
GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_RENDERBUFFER, colorBuffer)
|
|
228
|
+
if (depthStencilBuffer != 0) {
|
|
229
|
+
if (depth) {
|
|
230
|
+
GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_RENDERBUFFER, depthStencilBuffer)
|
|
231
|
+
}
|
|
232
|
+
if (stencil) {
|
|
233
|
+
GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER, GLES30.GL_STENCIL_ATTACHMENT, GLES30.GL_RENDERBUFFER, depthStencilBuffer)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (samples > 0) {
|
|
237
|
+
storage(resolveBuffer, colorFormat(), 0)
|
|
238
|
+
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, resolveFramebuffer)
|
|
239
|
+
GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_RENDERBUFFER, resolveBuffer)
|
|
240
|
+
}
|
|
241
|
+
clearDrawingBuffer()
|
|
242
|
+
GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER, renderbuffer)
|
|
243
|
+
restoreFramebuffers()
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private fun storage(buffer: Int, format: Int, sampleCount: Int) {
|
|
247
|
+
GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER, buffer)
|
|
248
|
+
if (sampleCount > 0) {
|
|
249
|
+
GLES30.glRenderbufferStorageMultisample(GLES30.GL_RENDERBUFFER, sampleCount, format, width, height)
|
|
250
|
+
} else {
|
|
251
|
+
GLES30.glRenderbufferStorage(GLES30.GL_RENDERBUFFER, format, width, height)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Clears the default framebuffer whatever the app's clear values, masks and scissor, and puts them back. */
|
|
256
|
+
private fun clearDrawingBuffer() {
|
|
257
|
+
val clearColor = FloatArray(4)
|
|
258
|
+
val clearDepth = FloatArray(1)
|
|
259
|
+
val ints = IntArray(3)
|
|
260
|
+
val colorMask = BooleanArray(4)
|
|
261
|
+
val depthMask = BooleanArray(1)
|
|
262
|
+
GLES30.glGetFloatv(GLES30.GL_COLOR_CLEAR_VALUE, clearColor, 0)
|
|
263
|
+
GLES30.glGetFloatv(GLES30.GL_DEPTH_CLEAR_VALUE, clearDepth, 0)
|
|
264
|
+
GLES30.glGetIntegerv(GLES30.GL_STENCIL_CLEAR_VALUE, ints, 0)
|
|
265
|
+
GLES30.glGetIntegerv(GLES30.GL_STENCIL_WRITEMASK, ints, 1)
|
|
266
|
+
GLES30.glGetIntegerv(GLES30.GL_STENCIL_BACK_WRITEMASK, ints, 2)
|
|
267
|
+
GLES30.glGetBooleanv(GLES30.GL_COLOR_WRITEMASK, colorMask, 0)
|
|
268
|
+
GLES30.glGetBooleanv(GLES30.GL_DEPTH_WRITEMASK, depthMask, 0)
|
|
269
|
+
val scissor = GLES30.glIsEnabled(GLES30.GL_SCISSOR_TEST)
|
|
270
|
+
val discard = GLES30.glIsEnabled(GLES30.GL_RASTERIZER_DISCARD)
|
|
271
|
+
|
|
272
|
+
GLES30.glBindFramebuffer(GLES30.GL_DRAW_FRAMEBUFFER, framebuffer)
|
|
273
|
+
GLES30.glDisable(GLES30.GL_SCISSOR_TEST)
|
|
274
|
+
GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD)
|
|
275
|
+
GLES30.glClearColor(0f, 0f, 0f, 0f)
|
|
276
|
+
GLES30.glClearDepthf(1f)
|
|
277
|
+
GLES30.glClearStencil(0)
|
|
278
|
+
GLES30.glColorMask(true, true, true, true)
|
|
279
|
+
GLES30.glDepthMask(true)
|
|
280
|
+
GLES30.glStencilMask(-1)
|
|
281
|
+
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT or GLES30.GL_DEPTH_BUFFER_BIT or GLES30.GL_STENCIL_BUFFER_BIT)
|
|
282
|
+
|
|
283
|
+
GLES30.glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3])
|
|
284
|
+
GLES30.glClearDepthf(clearDepth[0])
|
|
285
|
+
GLES30.glClearStencil(ints[0])
|
|
286
|
+
GLES30.glColorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3])
|
|
287
|
+
GLES30.glDepthMask(depthMask[0])
|
|
288
|
+
GLES30.glStencilMaskSeparate(GLES30.GL_FRONT, ints[1])
|
|
289
|
+
GLES30.glStencilMaskSeparate(GLES30.GL_BACK, ints[2])
|
|
290
|
+
if (scissor) GLES30.glEnable(GLES30.GL_SCISSOR_TEST)
|
|
291
|
+
if (discard) GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD)
|
|
292
|
+
GLES30.glBindFramebuffer(GLES30.GL_DRAW_FRAMEBUFFER, drawFramebuffer)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private fun restoreFramebuffers() {
|
|
296
|
+
GLES30.glBindFramebuffer(GLES30.GL_DRAW_FRAMEBUFFER, drawFramebuffer)
|
|
297
|
+
GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER, readFramebuffer)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Runs a read of the read framebuffer (readPixels, copyTexImage2D…).
|
|
302
|
+
* WebGL reads an antialiased default framebuffer as one image, its samples
|
|
303
|
+
* resolved, but GLES can't read a multisampled framebuffer: those calls are
|
|
304
|
+
* INVALID_OPERATION on one and read nothing. So a read of the default
|
|
305
|
+
* framebuffer resolves it, as present does, and reads the resolved copy.
|
|
306
|
+
*/
|
|
307
|
+
private inline fun <T> reading(read: () -> T): T {
|
|
308
|
+
if (samples == 0 || !defaultBound(GLES30.GL_READ_FRAMEBUFFER)) return read()
|
|
309
|
+
// A blit is clipped by the scissor test, and skipped with rasterizer
|
|
310
|
+
// discard on, so both are off while the samples are resolved.
|
|
311
|
+
val scissor = GLES30.glIsEnabled(GLES30.GL_SCISSOR_TEST)
|
|
312
|
+
val discard = GLES30.glIsEnabled(GLES30.GL_RASTERIZER_DISCARD)
|
|
313
|
+
GLES30.glDisable(GLES30.GL_SCISSOR_TEST)
|
|
314
|
+
GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD)
|
|
315
|
+
GLES30.glBindFramebuffer(GLES30.GL_DRAW_FRAMEBUFFER, resolveFramebuffer)
|
|
316
|
+
GLES30.glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GLES30.GL_COLOR_BUFFER_BIT, GLES30.GL_NEAREST)
|
|
317
|
+
if (scissor) GLES30.glEnable(GLES30.GL_SCISSOR_TEST)
|
|
318
|
+
if (discard) GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD)
|
|
319
|
+
GLES30.glBindFramebuffer(GLES30.GL_DRAW_FRAMEBUFFER, drawFramebuffer)
|
|
320
|
+
GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER, resolveFramebuffer)
|
|
321
|
+
try {
|
|
322
|
+
return read()
|
|
323
|
+
} finally {
|
|
324
|
+
GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER, readFramebuffer)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Shows the finished frame: copies the default framebuffer (resolving it
|
|
330
|
+
* when it's multisampled) into the TextureView's surface, scaled to its
|
|
331
|
+
* size, and swaps. Without a surface, the frame waits for one.
|
|
332
|
+
*/
|
|
333
|
+
private fun present() {
|
|
334
|
+
if (windowSurface == EGL14.EGL_NO_SURFACE) {
|
|
335
|
+
framePending = true
|
|
336
|
+
return
|
|
337
|
+
}
|
|
338
|
+
val size = IntArray(2)
|
|
339
|
+
EGL14.eglQuerySurface(display, windowSurface, EGL14.EGL_WIDTH, size, 0)
|
|
340
|
+
EGL14.eglQuerySurface(display, windowSurface, EGL14.EGL_HEIGHT, size, 1)
|
|
341
|
+
if (size[0] < 1 || size[1] < 1) {
|
|
342
|
+
framePending = true
|
|
343
|
+
return
|
|
344
|
+
}
|
|
345
|
+
withCurrent {
|
|
346
|
+
// A blit is clipped by the scissor test, and skipped with
|
|
347
|
+
// rasterizer discard on, so both are off while the frame is copied.
|
|
348
|
+
val scissor = GLES30.glIsEnabled(GLES30.GL_SCISSOR_TEST)
|
|
349
|
+
val discard = GLES30.glIsEnabled(GLES30.GL_RASTERIZER_DISCARD)
|
|
350
|
+
GLES30.glDisable(GLES30.GL_SCISSOR_TEST)
|
|
351
|
+
GLES30.glDisable(GLES30.GL_RASTERIZER_DISCARD)
|
|
352
|
+
var source = framebuffer
|
|
353
|
+
if (samples > 0) {
|
|
354
|
+
GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER, framebuffer)
|
|
355
|
+
GLES30.glBindFramebuffer(GLES30.GL_DRAW_FRAMEBUFFER, resolveFramebuffer)
|
|
356
|
+
GLES30.glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GLES30.GL_COLOR_BUFFER_BIT, GLES30.GL_NEAREST)
|
|
357
|
+
source = resolveFramebuffer
|
|
358
|
+
}
|
|
359
|
+
GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER, source)
|
|
360
|
+
GLES30.glReadBuffer(GLES30.GL_COLOR_ATTACHMENT0)
|
|
361
|
+
GLES30.glBindFramebuffer(GLES30.GL_DRAW_FRAMEBUFFER, 0)
|
|
362
|
+
val sameSize = size[0] == width && size[1] == height
|
|
363
|
+
GLES30.glBlitFramebuffer(
|
|
364
|
+
0, 0, width, height, 0, 0, size[0], size[1],
|
|
365
|
+
GLES30.GL_COLOR_BUFFER_BIT, if (sameSize) GLES30.GL_NEAREST else GLES30.GL_LINEAR,
|
|
366
|
+
)
|
|
367
|
+
EGL14.eglSwapBuffers(display, windowSurface)
|
|
368
|
+
framePending = false
|
|
369
|
+
if (scissor) GLES30.glEnable(GLES30.GL_SCISSOR_TEST)
|
|
370
|
+
if (discard) GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD)
|
|
371
|
+
// WebGL clears the drawing buffer once it's been shown, unless the
|
|
372
|
+
// app asked to keep it.
|
|
373
|
+
if (!preserveDrawingBuffer) clearDrawingBuffer()
|
|
374
|
+
restoreFramebuffers()
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// --- Objects -------------------------------------------------------------
|
|
379
|
+
|
|
380
|
+
private fun nameOf(id: Int): Int = if (id in names.indices) names[id] else 0
|
|
381
|
+
|
|
382
|
+
private fun setName(id: Int, name: Int) {
|
|
383
|
+
if (id >= names.size) {
|
|
384
|
+
var capacity = names.size
|
|
385
|
+
while (capacity <= id) capacity *= 2
|
|
386
|
+
names = names.copyOf(capacity)
|
|
387
|
+
}
|
|
388
|
+
names[id] = name
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
private fun takeName(id: Int): Int {
|
|
392
|
+
val name = nameOf(id)
|
|
393
|
+
if (id in names.indices) names[id] = 0
|
|
394
|
+
return name
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
private fun genFramebuffer(): Int = IntArray(1).also { GLES30.glGenFramebuffers(1, it, 0) }[0]
|
|
398
|
+
private fun genRenderbuffer(): Int = IntArray(1).also { GLES30.glGenRenderbuffers(1, it, 0) }[0]
|
|
399
|
+
|
|
400
|
+
private fun defaultBound(target: Int): Boolean =
|
|
401
|
+
(if (target == GLES30.GL_READ_FRAMEBUFFER) readFramebuffer else drawFramebuffer) == framebuffer
|
|
402
|
+
|
|
403
|
+
/** WebGL's BACK, COLOR, DEPTH and STENCIL name the default framebuffer's buffers, here ordinary attachments. */
|
|
404
|
+
private fun attachmentFor(attachment: Int, target: Int): Int {
|
|
405
|
+
if (!defaultBound(target)) return attachment
|
|
406
|
+
return when (attachment) {
|
|
407
|
+
WEBGL_BACK, WEBGL_COLOR -> GLES30.GL_COLOR_ATTACHMENT0
|
|
408
|
+
WEBGL_DEPTH -> GLES30.GL_DEPTH_ATTACHMENT
|
|
409
|
+
WEBGL_STENCIL -> GLES30.GL_STENCIL_ATTACHMENT
|
|
410
|
+
else -> attachment
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// --- Pixels --------------------------------------------------------------
|
|
415
|
+
|
|
416
|
+
private class Pixels(val buffer: Buffer?, val offset: Int, val isImage: Boolean)
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* An upload's pixels (see #pixels in webgl2-context.ts): none, an offset
|
|
420
|
+
* into the bound PIXEL_UNPACK_BUFFER, bytes, or a decoded image converted
|
|
421
|
+
* to the format and type.
|
|
422
|
+
*/
|
|
423
|
+
private fun pixels(reader: Reader, format: Int, type: Int): Pixels {
|
|
424
|
+
return when (reader.peekType()) {
|
|
425
|
+
ReadableType.Array -> Pixels(bytesFromWords(reader.array()), 0, false)
|
|
426
|
+
ReadableType.Map -> {
|
|
427
|
+
val source = reader.map()
|
|
428
|
+
val image = DecodedImages.get(source.getDouble("image").toInt())
|
|
429
|
+
?: return Pixels(null, 0, false)
|
|
430
|
+
Pixels(
|
|
431
|
+
convertImage(image, format, type, source.bool("flipY"), source.bool("premultiply")),
|
|
432
|
+
0,
|
|
433
|
+
true,
|
|
434
|
+
)
|
|
435
|
+
}
|
|
436
|
+
ReadableType.Int, ReadableType.Long, ReadableType.Number -> Pixels(null, reader.int(), false)
|
|
437
|
+
else -> {
|
|
438
|
+
reader.skip()
|
|
439
|
+
Pixels(null, 0, false)
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
private fun ReadableMap.bool(key: String): Boolean =
|
|
445
|
+
hasKey(key) && when (getType(key)) {
|
|
446
|
+
ReadableType.Boolean -> getBoolean(key)
|
|
447
|
+
in NUMBER_TYPES -> getDouble(key) != 0.0
|
|
448
|
+
else -> false
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** A decoded image's RGBA bytes as the format and type an upload names, tightly packed, flipped and premultiplied as asked. */
|
|
452
|
+
private fun convertImage(image: DecodedImage, format: Int, type: Int, flipY: Boolean, premultiply: Boolean): ByteBuffer {
|
|
453
|
+
val components = when (format) {
|
|
454
|
+
GLES30.GL_RGBA, GLES30.GL_RGBA_INTEGER -> 4
|
|
455
|
+
GLES30.GL_RGB, GLES30.GL_RGB_INTEGER -> 3
|
|
456
|
+
GLES30.GL_RG, GLES30.GL_RG_INTEGER, GLES30.GL_LUMINANCE_ALPHA -> 2
|
|
457
|
+
else -> 1
|
|
458
|
+
}
|
|
459
|
+
val packed = type == GLES30.GL_UNSIGNED_SHORT_5_6_5 || type == GLES30.GL_UNSIGNED_SHORT_4_4_4_4 ||
|
|
460
|
+
type == GLES30.GL_UNSIGNED_SHORT_5_5_5_1
|
|
461
|
+
val componentSize = when (type) {
|
|
462
|
+
GLES30.GL_FLOAT -> 4
|
|
463
|
+
GLES30.GL_HALF_FLOAT -> 2
|
|
464
|
+
else -> 1
|
|
465
|
+
}
|
|
466
|
+
val pixelSize = if (packed) 2 else components * componentSize
|
|
467
|
+
val out = ByteBuffer.allocateDirect(image.width * image.height * pixelSize).order(ByteOrder.nativeOrder())
|
|
468
|
+
val rgba = image.pixels
|
|
469
|
+
val multiply = premultiply && !image.premultiplied
|
|
470
|
+
val channels = FloatArray(4)
|
|
471
|
+
for (y in 0 until image.height) {
|
|
472
|
+
val row = (if (flipY) image.height - 1 - y else y) * image.width * 4
|
|
473
|
+
for (x in 0 until image.width) {
|
|
474
|
+
val p = row + x * 4
|
|
475
|
+
var r = (rgba.get(p).toInt() and 0xff).toFloat()
|
|
476
|
+
var g = (rgba.get(p + 1).toInt() and 0xff).toFloat()
|
|
477
|
+
var b = (rgba.get(p + 2).toInt() and 0xff).toFloat()
|
|
478
|
+
val a = (rgba.get(p + 3).toInt() and 0xff).toFloat()
|
|
479
|
+
if (multiply) {
|
|
480
|
+
r = r * a / 255
|
|
481
|
+
g = g * a / 255
|
|
482
|
+
b = b * a / 255
|
|
483
|
+
}
|
|
484
|
+
when (format) {
|
|
485
|
+
GLES30.GL_LUMINANCE -> channels[0] = r
|
|
486
|
+
GLES30.GL_ALPHA -> channels[0] = a
|
|
487
|
+
GLES30.GL_LUMINANCE_ALPHA -> { channels[0] = r; channels[1] = a }
|
|
488
|
+
else -> { channels[0] = r; channels[1] = g; channels[2] = b; channels[3] = a }
|
|
489
|
+
}
|
|
490
|
+
if (packed) {
|
|
491
|
+
val value = when (type) {
|
|
492
|
+
GLES30.GL_UNSIGNED_SHORT_5_6_5 ->
|
|
493
|
+
((r / 255 * 31 + 0.5f).toInt() shl 11) or ((g / 255 * 63 + 0.5f).toInt() shl 5) or
|
|
494
|
+
(b / 255 * 31 + 0.5f).toInt()
|
|
495
|
+
GLES30.GL_UNSIGNED_SHORT_4_4_4_4 ->
|
|
496
|
+
((r / 255 * 15 + 0.5f).toInt() shl 12) or ((g / 255 * 15 + 0.5f).toInt() shl 8) or
|
|
497
|
+
((b / 255 * 15 + 0.5f).toInt() shl 4) or (a / 255 * 15 + 0.5f).toInt()
|
|
498
|
+
else ->
|
|
499
|
+
((r / 255 * 31 + 0.5f).toInt() shl 11) or ((g / 255 * 31 + 0.5f).toInt() shl 6) or
|
|
500
|
+
((b / 255 * 31 + 0.5f).toInt() shl 1) or (if (a >= 128) 1 else 0)
|
|
501
|
+
}
|
|
502
|
+
out.putShort(value.toShort())
|
|
503
|
+
continue
|
|
504
|
+
}
|
|
505
|
+
for (c in 0 until components) {
|
|
506
|
+
when (type) {
|
|
507
|
+
GLES30.GL_FLOAT -> out.putFloat(channels[c] / 255)
|
|
508
|
+
GLES30.GL_HALF_FLOAT -> out.putShort(halfFromFloat(channels[c] / 255))
|
|
509
|
+
else -> out.put((channels[c] + 0.5f).toInt().toByte())
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
out.rewind()
|
|
515
|
+
return out
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
private fun halfFromFloat(value: Float): Short {
|
|
519
|
+
val bits = java.lang.Float.floatToIntBits(value)
|
|
520
|
+
val sign = (bits ushr 16) and 0x8000
|
|
521
|
+
val exponent = ((bits ushr 23) and 0xff) - 127 + 15
|
|
522
|
+
val mantissa = bits and 0x7fffff
|
|
523
|
+
return when {
|
|
524
|
+
exponent <= 0 -> sign.toShort()
|
|
525
|
+
exponent >= 31 -> (sign or 0x7c00).toShort()
|
|
526
|
+
else -> (sign or (exponent shl 10) or (mantissa ushr 13)).toShort()
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/** Runs an upload of converted image bytes with tightly packed unpacking, whatever the app set. */
|
|
531
|
+
private inline fun withTightUnpacking(tight: Boolean, upload: () -> Unit) {
|
|
532
|
+
if (!tight) {
|
|
533
|
+
upload()
|
|
534
|
+
return
|
|
535
|
+
}
|
|
536
|
+
val names = intArrayOf(
|
|
537
|
+
GLES30.GL_UNPACK_ALIGNMENT, GLES30.GL_UNPACK_ROW_LENGTH, GLES30.GL_UNPACK_IMAGE_HEIGHT,
|
|
538
|
+
GLES30.GL_UNPACK_SKIP_PIXELS, GLES30.GL_UNPACK_SKIP_ROWS, GLES30.GL_UNPACK_SKIP_IMAGES,
|
|
539
|
+
)
|
|
540
|
+
val saved = IntArray(names.size)
|
|
541
|
+
for (i in names.indices) GLES30.glGetIntegerv(names[i], saved, i)
|
|
542
|
+
GLES30.glPixelStorei(GLES30.GL_UNPACK_ALIGNMENT, 1)
|
|
543
|
+
for (i in 1 until names.size) GLES30.glPixelStorei(names[i], 0)
|
|
544
|
+
upload()
|
|
545
|
+
for (i in names.indices) GLES30.glPixelStorei(names[i], saved[i])
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// --- Running a batch -----------------------------------------------------
|
|
549
|
+
|
|
550
|
+
/** Reads a batch: the commands' arguments, in order. */
|
|
551
|
+
private class Reader(val items: ReadableArray) {
|
|
552
|
+
var index = 0
|
|
553
|
+
val size = items.size()
|
|
554
|
+
fun hasNext() = index < size
|
|
555
|
+
fun peekType(): ReadableType = if (index < size) items.getType(index) else ReadableType.Null
|
|
556
|
+
fun number(): Double {
|
|
557
|
+
if (index >= size) return 0.0
|
|
558
|
+
val i = index++
|
|
559
|
+
return when (items.getType(i)) {
|
|
560
|
+
ReadableType.Int, ReadableType.Long, ReadableType.Number -> items.getDouble(i)
|
|
561
|
+
ReadableType.Boolean -> if (items.getBoolean(i)) 1.0 else 0.0
|
|
562
|
+
else -> 0.0
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
fun int(): Int = number().toLong().toInt()
|
|
566
|
+
fun long(): Long = number().toLong()
|
|
567
|
+
fun float(): Float = number().toFloat()
|
|
568
|
+
fun bool(): Boolean = number() != 0.0
|
|
569
|
+
fun string(): String {
|
|
570
|
+
if (index >= size) return ""
|
|
571
|
+
val i = index++
|
|
572
|
+
return if (items.getType(i) == ReadableType.String) items.getString(i) else ""
|
|
573
|
+
}
|
|
574
|
+
fun array(): ReadableArray {
|
|
575
|
+
if (index >= size) return JavaOnlyArray()
|
|
576
|
+
val i = index++
|
|
577
|
+
return if (items.getType(i) == ReadableType.Array) items.getArray(i) else JavaOnlyArray()
|
|
578
|
+
}
|
|
579
|
+
fun map(): ReadableMap {
|
|
580
|
+
val i = index++
|
|
581
|
+
return items.getMap(i)
|
|
582
|
+
}
|
|
583
|
+
fun skip() { index++ }
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
fun run(commands: ReadableArray): JavaOnlyArray {
|
|
587
|
+
val results = JavaOnlyArray()
|
|
588
|
+
if (destroyed) return results
|
|
589
|
+
withCurrent {
|
|
590
|
+
val reader = Reader(commands)
|
|
591
|
+
while (reader.hasNext()) {
|
|
592
|
+
val op = reader.int()
|
|
593
|
+
if (op >= 200) {
|
|
594
|
+
pushResult(results, query(op, reader))
|
|
595
|
+
} else {
|
|
596
|
+
command(op, reader)
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
return results
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
private fun pushResult(results: JavaOnlyArray, value: Any?) {
|
|
604
|
+
when (value) {
|
|
605
|
+
null -> results.pushNull()
|
|
606
|
+
is Boolean -> results.pushBoolean(value)
|
|
607
|
+
is Int -> results.pushInt(value)
|
|
608
|
+
is Long -> results.pushDouble(value.toDouble())
|
|
609
|
+
is Float -> results.pushDouble(value.toDouble())
|
|
610
|
+
is Double -> results.pushDouble(value)
|
|
611
|
+
is String -> results.pushString(value)
|
|
612
|
+
is JavaOnlyArray -> results.pushArray(value)
|
|
613
|
+
is JavaOnlyMap -> results.pushMap(value)
|
|
614
|
+
else -> results.pushNull()
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
private fun floats(values: ReadableArray): FloatArray = FloatArray(values.size()) { values.getDouble(it).toFloat() }
|
|
619
|
+
private fun ints(values: ReadableArray): IntArray = IntArray(values.size()) { values.getDouble(it).toLong().toInt() }
|
|
620
|
+
private fun padded(values: FloatArray) = if (values.size >= 4) values else values.copyOf(4)
|
|
621
|
+
private fun padded(values: IntArray) = if (values.size >= 4) values else values.copyOf(4)
|
|
622
|
+
|
|
623
|
+
private fun intArrayResult(values: IntArray, count: Int = values.size): JavaOnlyArray =
|
|
624
|
+
JavaOnlyArray().apply { for (i in 0 until count) pushInt(values[i]) }
|
|
625
|
+
|
|
626
|
+
private fun floatArrayResult(values: FloatArray, count: Int = values.size): JavaOnlyArray =
|
|
627
|
+
JavaOnlyArray().apply { for (i in 0 until count) pushDouble(values[i].toDouble()) }
|
|
628
|
+
|
|
629
|
+
private fun command(op: Int, r: Reader) {
|
|
630
|
+
when (op) {
|
|
631
|
+
WebGLOps.ACTIVE_TEXTURE -> GLES30.glActiveTexture(r.int())
|
|
632
|
+
WebGLOps.ATTACH_SHADER -> {
|
|
633
|
+
val program = nameOf(r.int())
|
|
634
|
+
GLES30.glAttachShader(program, nameOf(r.int()))
|
|
635
|
+
}
|
|
636
|
+
WebGLOps.BIND_ATTRIB_LOCATION -> {
|
|
637
|
+
val program = nameOf(r.int())
|
|
638
|
+
val index = r.int()
|
|
639
|
+
GLES30.glBindAttribLocation(program, index, r.string())
|
|
640
|
+
}
|
|
641
|
+
WebGLOps.BIND_BUFFER -> {
|
|
642
|
+
val target = r.int()
|
|
643
|
+
GLES30.glBindBuffer(target, nameOf(r.int()))
|
|
644
|
+
}
|
|
645
|
+
WebGLOps.BIND_BUFFER_BASE -> {
|
|
646
|
+
val target = r.int()
|
|
647
|
+
val index = r.int()
|
|
648
|
+
GLES30.glBindBufferBase(target, index, nameOf(r.int()))
|
|
649
|
+
}
|
|
650
|
+
WebGLOps.BIND_BUFFER_RANGE -> {
|
|
651
|
+
val target = r.int()
|
|
652
|
+
val index = r.int()
|
|
653
|
+
val buffer = nameOf(r.int())
|
|
654
|
+
val offset = r.int()
|
|
655
|
+
GLES30.glBindBufferRange(target, index, buffer, offset, r.int())
|
|
656
|
+
}
|
|
657
|
+
WebGLOps.BIND_FRAMEBUFFER -> {
|
|
658
|
+
val target = r.int()
|
|
659
|
+
val id = r.int()
|
|
660
|
+
// null is the default framebuffer, which is one of ours.
|
|
661
|
+
val name = if (id == 0) framebuffer else nameOf(id)
|
|
662
|
+
if (target == GLES30.GL_FRAMEBUFFER || target == GLES30.GL_DRAW_FRAMEBUFFER) drawFramebuffer = name
|
|
663
|
+
if (target == GLES30.GL_FRAMEBUFFER || target == GLES30.GL_READ_FRAMEBUFFER) readFramebuffer = name
|
|
664
|
+
GLES30.glBindFramebuffer(target, name)
|
|
665
|
+
}
|
|
666
|
+
WebGLOps.BIND_RENDERBUFFER -> {
|
|
667
|
+
val target = r.int()
|
|
668
|
+
renderbuffer = nameOf(r.int())
|
|
669
|
+
GLES30.glBindRenderbuffer(target, renderbuffer)
|
|
670
|
+
}
|
|
671
|
+
WebGLOps.BIND_SAMPLER -> {
|
|
672
|
+
val unit = r.int()
|
|
673
|
+
GLES30.glBindSampler(unit, nameOf(r.int()))
|
|
674
|
+
}
|
|
675
|
+
WebGLOps.BIND_TEXTURE -> {
|
|
676
|
+
val target = r.int()
|
|
677
|
+
GLES30.glBindTexture(target, nameOf(r.int()))
|
|
678
|
+
}
|
|
679
|
+
WebGLOps.BIND_TRANSFORM_FEEDBACK -> {
|
|
680
|
+
val target = r.int()
|
|
681
|
+
GLES30.glBindTransformFeedback(target, nameOf(r.int()))
|
|
682
|
+
}
|
|
683
|
+
WebGLOps.BIND_VERTEX_ARRAY -> GLES30.glBindVertexArray(nameOf(r.int()))
|
|
684
|
+
WebGLOps.BEGIN_QUERY -> {
|
|
685
|
+
val target = r.int()
|
|
686
|
+
GLES30.glBeginQuery(target, nameOf(r.int()))
|
|
687
|
+
}
|
|
688
|
+
WebGLOps.BEGIN_TRANSFORM_FEEDBACK -> GLES30.glBeginTransformFeedback(r.int())
|
|
689
|
+
WebGLOps.BLEND_COLOR -> GLES30.glBlendColor(r.float(), r.float(), r.float(), r.float())
|
|
690
|
+
WebGLOps.BLEND_EQUATION -> GLES30.glBlendEquation(r.int())
|
|
691
|
+
WebGLOps.BLEND_EQUATION_SEPARATE -> GLES30.glBlendEquationSeparate(r.int(), r.int())
|
|
692
|
+
WebGLOps.BLEND_FUNC -> GLES30.glBlendFunc(r.int(), r.int())
|
|
693
|
+
WebGLOps.BLEND_FUNC_SEPARATE -> GLES30.glBlendFuncSeparate(r.int(), r.int(), r.int(), r.int())
|
|
694
|
+
WebGLOps.BLIT_FRAMEBUFFER -> GLES30.glBlitFramebuffer(
|
|
695
|
+
r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int(),
|
|
696
|
+
)
|
|
697
|
+
WebGLOps.BUFFER_DATA -> {
|
|
698
|
+
val target = r.int()
|
|
699
|
+
if (r.peekType() == ReadableType.Array) {
|
|
700
|
+
val bytes = bytesFromWords(r.array())
|
|
701
|
+
GLES30.glBufferData(target, bytes.remaining(), bytes, r.int())
|
|
702
|
+
} else {
|
|
703
|
+
val size = r.int()
|
|
704
|
+
GLES30.glBufferData(target, size, null, r.int())
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
WebGLOps.BUFFER_SUB_DATA -> {
|
|
708
|
+
val target = r.int()
|
|
709
|
+
val offset = r.int()
|
|
710
|
+
val bytes = bytesFromWords(r.array())
|
|
711
|
+
GLES30.glBufferSubData(target, offset, bytes.remaining(), bytes)
|
|
712
|
+
}
|
|
713
|
+
WebGLOps.CLEAR -> GLES30.glClear(r.int())
|
|
714
|
+
WebGLOps.CLEAR_BUFFERFV -> {
|
|
715
|
+
val buffer = r.int()
|
|
716
|
+
val drawbuffer = r.int()
|
|
717
|
+
GLES30.glClearBufferfv(buffer, drawbuffer, padded(floats(r.array())), 0)
|
|
718
|
+
}
|
|
719
|
+
WebGLOps.CLEAR_BUFFERIV -> {
|
|
720
|
+
val buffer = r.int()
|
|
721
|
+
val drawbuffer = r.int()
|
|
722
|
+
GLES30.glClearBufferiv(buffer, drawbuffer, padded(ints(r.array())), 0)
|
|
723
|
+
}
|
|
724
|
+
WebGLOps.CLEAR_BUFFERUIV -> {
|
|
725
|
+
val buffer = r.int()
|
|
726
|
+
val drawbuffer = r.int()
|
|
727
|
+
GLES30.glClearBufferuiv(buffer, drawbuffer, padded(ints(r.array())), 0)
|
|
728
|
+
}
|
|
729
|
+
WebGLOps.CLEAR_BUFFERFI -> GLES30.glClearBufferfi(r.int(), r.int(), r.float(), r.int())
|
|
730
|
+
WebGLOps.CLEAR_COLOR -> GLES30.glClearColor(r.float(), r.float(), r.float(), r.float())
|
|
731
|
+
WebGLOps.CLEAR_DEPTH -> GLES30.glClearDepthf(r.float())
|
|
732
|
+
WebGLOps.CLEAR_STENCIL -> GLES30.glClearStencil(r.int())
|
|
733
|
+
WebGLOps.COLOR_MASK -> GLES30.glColorMask(r.bool(), r.bool(), r.bool(), r.bool())
|
|
734
|
+
WebGLOps.COMPILE_SHADER -> GLES30.glCompileShader(nameOf(r.int()))
|
|
735
|
+
WebGLOps.COMPRESSED_TEX_IMAGE2D -> {
|
|
736
|
+
val target = r.int(); val level = r.int(); val internalformat = r.int()
|
|
737
|
+
val width = r.int(); val height = r.int(); val border = r.int()
|
|
738
|
+
if (r.peekType() == ReadableType.Array) {
|
|
739
|
+
val bytes = bytesFromWords(r.array())
|
|
740
|
+
val size = r.int()
|
|
741
|
+
GLES30.glCompressedTexImage2D(target, level, internalformat, width, height, border, size, bytes)
|
|
742
|
+
} else {
|
|
743
|
+
r.skip(); r.skip()
|
|
744
|
+
Log.w(TAG, "compressedTexImage2D from a PIXEL_UNPACK_BUFFER is not supported on Android")
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
WebGLOps.COMPRESSED_TEX_IMAGE3D -> {
|
|
748
|
+
val target = r.int(); val level = r.int(); val internalformat = r.int()
|
|
749
|
+
val width = r.int(); val height = r.int(); val depth = r.int(); val border = r.int()
|
|
750
|
+
if (r.peekType() == ReadableType.Array) {
|
|
751
|
+
val bytes = bytesFromWords(r.array())
|
|
752
|
+
val size = r.int()
|
|
753
|
+
GLES30.glCompressedTexImage3D(target, level, internalformat, width, height, depth, border, size, bytes)
|
|
754
|
+
} else {
|
|
755
|
+
val offset = r.int()
|
|
756
|
+
val size = r.int()
|
|
757
|
+
GLES30.glCompressedTexImage3D(target, level, internalformat, width, height, depth, border, size, offset)
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
WebGLOps.COMPRESSED_TEX_SUB_IMAGE2D -> {
|
|
761
|
+
val target = r.int(); val level = r.int(); val x = r.int(); val y = r.int()
|
|
762
|
+
val width = r.int(); val height = r.int(); val format = r.int()
|
|
763
|
+
if (r.peekType() == ReadableType.Array) {
|
|
764
|
+
val bytes = bytesFromWords(r.array())
|
|
765
|
+
val size = r.int()
|
|
766
|
+
GLES30.glCompressedTexSubImage2D(target, level, x, y, width, height, format, size, bytes)
|
|
767
|
+
} else {
|
|
768
|
+
r.skip(); r.skip()
|
|
769
|
+
Log.w(TAG, "compressedTexSubImage2D from a PIXEL_UNPACK_BUFFER is not supported on Android")
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
WebGLOps.COMPRESSED_TEX_SUB_IMAGE3D -> {
|
|
773
|
+
val target = r.int(); val level = r.int(); val x = r.int(); val y = r.int(); val z = r.int()
|
|
774
|
+
val width = r.int(); val height = r.int(); val depth = r.int(); val format = r.int()
|
|
775
|
+
if (r.peekType() == ReadableType.Array) {
|
|
776
|
+
val bytes = bytesFromWords(r.array())
|
|
777
|
+
val size = r.int()
|
|
778
|
+
GLES30.glCompressedTexSubImage3D(target, level, x, y, z, width, height, depth, format, size, bytes)
|
|
779
|
+
} else {
|
|
780
|
+
val offset = r.int()
|
|
781
|
+
val size = r.int()
|
|
782
|
+
GLES30.glCompressedTexSubImage3D(target, level, x, y, z, width, height, depth, format, size, offset)
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
WebGLOps.COPY_BUFFER_SUB_DATA -> GLES30.glCopyBufferSubData(r.int(), r.int(), r.int(), r.int(), r.int())
|
|
786
|
+
WebGLOps.COPY_TEX_IMAGE2D -> reading {
|
|
787
|
+
GLES30.glCopyTexImage2D(r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int())
|
|
788
|
+
}
|
|
789
|
+
WebGLOps.COPY_TEX_SUB_IMAGE2D -> reading {
|
|
790
|
+
GLES30.glCopyTexSubImage2D(r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int())
|
|
791
|
+
}
|
|
792
|
+
WebGLOps.COPY_TEX_SUB_IMAGE3D -> reading {
|
|
793
|
+
GLES30.glCopyTexSubImage3D(
|
|
794
|
+
r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int(),
|
|
795
|
+
)
|
|
796
|
+
}
|
|
797
|
+
WebGLOps.CREATE_BUFFER -> setName(r.int(), IntArray(1).also { GLES30.glGenBuffers(1, it, 0) }[0])
|
|
798
|
+
WebGLOps.CREATE_FRAMEBUFFER -> setName(r.int(), genFramebuffer())
|
|
799
|
+
WebGLOps.CREATE_PROGRAM -> setName(r.int(), GLES30.glCreateProgram())
|
|
800
|
+
WebGLOps.CREATE_QUERY -> setName(r.int(), IntArray(1).also { GLES30.glGenQueries(1, it, 0) }[0])
|
|
801
|
+
WebGLOps.CREATE_RENDERBUFFER -> setName(r.int(), genRenderbuffer())
|
|
802
|
+
WebGLOps.CREATE_SAMPLER -> setName(r.int(), IntArray(1).also { GLES30.glGenSamplers(1, it, 0) }[0])
|
|
803
|
+
WebGLOps.CREATE_SHADER -> {
|
|
804
|
+
val id = r.int()
|
|
805
|
+
setName(id, GLES30.glCreateShader(r.int()))
|
|
806
|
+
}
|
|
807
|
+
WebGLOps.CREATE_TEXTURE -> setName(r.int(), IntArray(1).also { GLES30.glGenTextures(1, it, 0) }[0])
|
|
808
|
+
WebGLOps.CREATE_TRANSFORM_FEEDBACK ->
|
|
809
|
+
setName(r.int(), IntArray(1).also { GLES30.glGenTransformFeedbacks(1, it, 0) }[0])
|
|
810
|
+
WebGLOps.CREATE_VERTEX_ARRAY -> setName(r.int(), IntArray(1).also { GLES30.glGenVertexArrays(1, it, 0) }[0])
|
|
811
|
+
WebGLOps.CULL_FACE -> GLES30.glCullFace(r.int())
|
|
812
|
+
WebGLOps.DELETE_BUFFER -> takeName(r.int()).let { if (it != 0) GLES30.glDeleteBuffers(1, intArrayOf(it), 0) }
|
|
813
|
+
WebGLOps.DELETE_FRAMEBUFFER -> {
|
|
814
|
+
val name = takeName(r.int())
|
|
815
|
+
if (name != 0) {
|
|
816
|
+
// Deleting the bound framebuffer binds the default one, as in WebGL.
|
|
817
|
+
if (drawFramebuffer == name) drawFramebuffer = framebuffer
|
|
818
|
+
if (readFramebuffer == name) readFramebuffer = framebuffer
|
|
819
|
+
GLES30.glDeleteFramebuffers(1, intArrayOf(name), 0)
|
|
820
|
+
restoreFramebuffers()
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
WebGLOps.DELETE_PROGRAM -> takeName(r.int()).let { if (it != 0) GLES30.glDeleteProgram(it) }
|
|
824
|
+
WebGLOps.DELETE_QUERY -> takeName(r.int()).let { if (it != 0) GLES30.glDeleteQueries(1, intArrayOf(it), 0) }
|
|
825
|
+
WebGLOps.DELETE_RENDERBUFFER -> {
|
|
826
|
+
val name = takeName(r.int())
|
|
827
|
+
if (name != 0) {
|
|
828
|
+
if (renderbuffer == name) renderbuffer = 0
|
|
829
|
+
GLES30.glDeleteRenderbuffers(1, intArrayOf(name), 0)
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
WebGLOps.DELETE_SAMPLER -> takeName(r.int()).let { if (it != 0) GLES30.glDeleteSamplers(1, intArrayOf(it), 0) }
|
|
833
|
+
WebGLOps.DELETE_SHADER -> takeName(r.int()).let { if (it != 0) GLES30.glDeleteShader(it) }
|
|
834
|
+
WebGLOps.DELETE_SYNC -> syncs.remove(r.int())?.let { GLES30.glDeleteSync(it) }
|
|
835
|
+
WebGLOps.DELETE_TEXTURE -> takeName(r.int()).let { if (it != 0) GLES30.glDeleteTextures(1, intArrayOf(it), 0) }
|
|
836
|
+
WebGLOps.DELETE_TRANSFORM_FEEDBACK ->
|
|
837
|
+
takeName(r.int()).let { if (it != 0) GLES30.glDeleteTransformFeedbacks(1, intArrayOf(it), 0) }
|
|
838
|
+
WebGLOps.DELETE_VERTEX_ARRAY ->
|
|
839
|
+
takeName(r.int()).let { if (it != 0) GLES30.glDeleteVertexArrays(1, intArrayOf(it), 0) }
|
|
840
|
+
WebGLOps.DEPTH_FUNC -> GLES30.glDepthFunc(r.int())
|
|
841
|
+
WebGLOps.DEPTH_MASK -> GLES30.glDepthMask(r.bool())
|
|
842
|
+
WebGLOps.DEPTH_RANGE -> GLES30.glDepthRangef(r.float(), r.float())
|
|
843
|
+
WebGLOps.DETACH_SHADER -> {
|
|
844
|
+
val program = nameOf(r.int())
|
|
845
|
+
GLES30.glDetachShader(program, nameOf(r.int()))
|
|
846
|
+
}
|
|
847
|
+
WebGLOps.DISABLE -> GLES30.glDisable(r.int())
|
|
848
|
+
WebGLOps.DISABLE_VERTEX_ATTRIB_ARRAY -> GLES30.glDisableVertexAttribArray(r.int())
|
|
849
|
+
WebGLOps.DRAW_ARRAYS -> GLES30.glDrawArrays(r.int(), r.int(), r.int())
|
|
850
|
+
WebGLOps.DRAW_ARRAYS_INSTANCED -> GLES30.glDrawArraysInstanced(r.int(), r.int(), r.int(), r.int())
|
|
851
|
+
WebGLOps.DRAW_BUFFERS -> {
|
|
852
|
+
val buffers = ints(r.array())
|
|
853
|
+
val defaultBound = defaultBound(GLES30.GL_DRAW_FRAMEBUFFER)
|
|
854
|
+
for (i in buffers.indices) {
|
|
855
|
+
if (defaultBound && buffers[i] == WEBGL_BACK) buffers[i] = GLES30.GL_COLOR_ATTACHMENT0
|
|
856
|
+
}
|
|
857
|
+
GLES30.glDrawBuffers(buffers.size, buffers, 0)
|
|
858
|
+
}
|
|
859
|
+
WebGLOps.DRAW_ELEMENTS -> GLES30.glDrawElements(r.int(), r.int(), r.int(), r.int())
|
|
860
|
+
WebGLOps.DRAW_ELEMENTS_INSTANCED -> GLES30.glDrawElementsInstanced(r.int(), r.int(), r.int(), r.int(), r.int())
|
|
861
|
+
WebGLOps.DRAW_RANGE_ELEMENTS -> GLES30.glDrawRangeElements(r.int(), r.int(), r.int(), r.int(), r.int(), r.int())
|
|
862
|
+
WebGLOps.ENABLE -> GLES30.glEnable(r.int())
|
|
863
|
+
WebGLOps.ENABLE_VERTEX_ATTRIB_ARRAY -> GLES30.glEnableVertexAttribArray(r.int())
|
|
864
|
+
WebGLOps.END_QUERY -> GLES30.glEndQuery(r.int())
|
|
865
|
+
WebGLOps.END_TRANSFORM_FEEDBACK -> GLES30.glEndTransformFeedback()
|
|
866
|
+
WebGLOps.FENCE_SYNC -> {
|
|
867
|
+
val id = r.int()
|
|
868
|
+
val condition = r.int()
|
|
869
|
+
val sync = GLES30.glFenceSync(condition, r.int())
|
|
870
|
+
if (sync != 0L) syncs[id] = sync
|
|
871
|
+
}
|
|
872
|
+
WebGLOps.FINISH -> GLES30.glFinish()
|
|
873
|
+
WebGLOps.FLUSH -> GLES30.glFlush()
|
|
874
|
+
WebGLOps.FRAMEBUFFER_RENDERBUFFER -> {
|
|
875
|
+
val target = r.int(); val attachment = r.int(); val renderbufferTarget = r.int()
|
|
876
|
+
GLES30.glFramebufferRenderbuffer(target, attachment, renderbufferTarget, nameOf(r.int()))
|
|
877
|
+
}
|
|
878
|
+
WebGLOps.FRAMEBUFFER_TEXTURE2D -> {
|
|
879
|
+
val target = r.int(); val attachment = r.int(); val textarget = r.int()
|
|
880
|
+
val texture = nameOf(r.int())
|
|
881
|
+
GLES30.glFramebufferTexture2D(target, attachment, textarget, texture, r.int())
|
|
882
|
+
}
|
|
883
|
+
WebGLOps.FRAMEBUFFER_TEXTURE_LAYER -> {
|
|
884
|
+
val target = r.int(); val attachment = r.int()
|
|
885
|
+
val texture = nameOf(r.int())
|
|
886
|
+
GLES30.glFramebufferTextureLayer(target, attachment, texture, r.int(), r.int())
|
|
887
|
+
}
|
|
888
|
+
WebGLOps.FRONT_FACE -> GLES30.glFrontFace(r.int())
|
|
889
|
+
WebGLOps.GENERATE_MIPMAP -> GLES30.glGenerateMipmap(r.int())
|
|
890
|
+
WebGLOps.HINT -> GLES30.glHint(r.int(), r.int())
|
|
891
|
+
WebGLOps.INVALIDATE_FRAMEBUFFER, WebGLOps.INVALIDATE_SUB_FRAMEBUFFER -> {
|
|
892
|
+
val target = r.int()
|
|
893
|
+
val attachments = ints(r.array())
|
|
894
|
+
for (i in attachments.indices) attachments[i] = attachmentFor(attachments[i], target)
|
|
895
|
+
if (op == WebGLOps.INVALIDATE_FRAMEBUFFER) {
|
|
896
|
+
GLES30.glInvalidateFramebuffer(target, attachments.size, attachments, 0)
|
|
897
|
+
} else {
|
|
898
|
+
GLES30.glInvalidateSubFramebuffer(target, attachments.size, attachments, 0, r.int(), r.int(), r.int(), r.int())
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
WebGLOps.LINE_WIDTH -> GLES30.glLineWidth(r.float())
|
|
902
|
+
WebGLOps.LINK_PROGRAM -> GLES30.glLinkProgram(nameOf(r.int()))
|
|
903
|
+
WebGLOps.PAUSE_TRANSFORM_FEEDBACK -> GLES30.glPauseTransformFeedback()
|
|
904
|
+
WebGLOps.PIXEL_STOREI -> GLES30.glPixelStorei(r.int(), r.int())
|
|
905
|
+
WebGLOps.POLYGON_OFFSET -> GLES30.glPolygonOffset(r.float(), r.float())
|
|
906
|
+
WebGLOps.READ_BUFFER -> {
|
|
907
|
+
val source = r.int()
|
|
908
|
+
GLES30.glReadBuffer(
|
|
909
|
+
if (defaultBound(GLES30.GL_READ_FRAMEBUFFER) && source == WEBGL_BACK) GLES30.GL_COLOR_ATTACHMENT0 else source,
|
|
910
|
+
)
|
|
911
|
+
}
|
|
912
|
+
WebGLOps.READ_PIXELS_TO_BUFFER -> reading {
|
|
913
|
+
GLES30.glReadPixels(r.int(), r.int(), r.int(), r.int(), r.int(), r.int(), r.int())
|
|
914
|
+
}
|
|
915
|
+
WebGLOps.RENDERBUFFER_STORAGE -> GLES30.glRenderbufferStorage(r.int(), r.int(), r.int(), r.int())
|
|
916
|
+
WebGLOps.RENDERBUFFER_STORAGE_MULTISAMPLE ->
|
|
917
|
+
GLES30.glRenderbufferStorageMultisample(r.int(), r.int(), r.int(), r.int(), r.int())
|
|
918
|
+
WebGLOps.RESUME_TRANSFORM_FEEDBACK -> GLES30.glResumeTransformFeedback()
|
|
919
|
+
WebGLOps.SAMPLE_COVERAGE -> GLES30.glSampleCoverage(r.float(), r.bool())
|
|
920
|
+
WebGLOps.SAMPLER_PARAMETERF -> {
|
|
921
|
+
val sampler = nameOf(r.int())
|
|
922
|
+
GLES30.glSamplerParameterf(sampler, r.int(), r.float())
|
|
923
|
+
}
|
|
924
|
+
WebGLOps.SAMPLER_PARAMETERI -> {
|
|
925
|
+
val sampler = nameOf(r.int())
|
|
926
|
+
GLES30.glSamplerParameteri(sampler, r.int(), r.int())
|
|
927
|
+
}
|
|
928
|
+
WebGLOps.SCISSOR -> GLES30.glScissor(r.int(), r.int(), r.int(), r.int())
|
|
929
|
+
WebGLOps.SHADER_SOURCE -> {
|
|
930
|
+
val shader = nameOf(r.int())
|
|
931
|
+
GLES30.glShaderSource(shader, r.string())
|
|
932
|
+
}
|
|
933
|
+
WebGLOps.STENCIL_FUNC -> GLES30.glStencilFunc(r.int(), r.int(), r.long().toInt())
|
|
934
|
+
WebGLOps.STENCIL_FUNC_SEPARATE -> GLES30.glStencilFuncSeparate(r.int(), r.int(), r.int(), r.long().toInt())
|
|
935
|
+
WebGLOps.STENCIL_MASK -> GLES30.glStencilMask(r.long().toInt())
|
|
936
|
+
WebGLOps.STENCIL_MASK_SEPARATE -> GLES30.glStencilMaskSeparate(r.int(), r.long().toInt())
|
|
937
|
+
WebGLOps.STENCIL_OP -> GLES30.glStencilOp(r.int(), r.int(), r.int())
|
|
938
|
+
WebGLOps.STENCIL_OP_SEPARATE -> GLES30.glStencilOpSeparate(r.int(), r.int(), r.int(), r.int())
|
|
939
|
+
WebGLOps.TEX_IMAGE2D -> {
|
|
940
|
+
val target = r.int(); val level = r.int(); val internalformat = r.int()
|
|
941
|
+
val width = r.int(); val height = r.int(); val border = r.int()
|
|
942
|
+
val format = r.int(); val type = r.int()
|
|
943
|
+
val pixels = pixels(r, format, type)
|
|
944
|
+
withTightUnpacking(pixels.isImage) {
|
|
945
|
+
if (pixels.buffer == null && pixels.offset != 0) {
|
|
946
|
+
Log.w(TAG, "texImage2D from a PIXEL_UNPACK_BUFFER is not supported on Android")
|
|
947
|
+
} else {
|
|
948
|
+
GLES30.glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels.buffer)
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
WebGLOps.TEX_IMAGE3D -> {
|
|
953
|
+
val target = r.int(); val level = r.int(); val internalformat = r.int()
|
|
954
|
+
val width = r.int(); val height = r.int(); val depth = r.int(); val border = r.int()
|
|
955
|
+
val format = r.int(); val type = r.int()
|
|
956
|
+
val pixels = pixels(r, format, type)
|
|
957
|
+
withTightUnpacking(pixels.isImage) {
|
|
958
|
+
if (pixels.buffer == null && pixels.offset != 0) {
|
|
959
|
+
GLES30.glTexImage3D(target, level, internalformat, width, height, depth, border, format, type, pixels.offset)
|
|
960
|
+
} else {
|
|
961
|
+
GLES30.glTexImage3D(target, level, internalformat, width, height, depth, border, format, type, pixels.buffer)
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
WebGLOps.TEX_PARAMETERF -> GLES30.glTexParameterf(r.int(), r.int(), r.float())
|
|
966
|
+
WebGLOps.TEX_PARAMETERI -> GLES30.glTexParameteri(r.int(), r.int(), r.int())
|
|
967
|
+
WebGLOps.TEX_STORAGE2D -> GLES30.glTexStorage2D(r.int(), r.int(), r.int(), r.int(), r.int())
|
|
968
|
+
WebGLOps.TEX_STORAGE3D -> GLES30.glTexStorage3D(r.int(), r.int(), r.int(), r.int(), r.int(), r.int())
|
|
969
|
+
WebGLOps.TEX_SUB_IMAGE2D -> {
|
|
970
|
+
val target = r.int(); val level = r.int(); val x = r.int(); val y = r.int()
|
|
971
|
+
val width = r.int(); val height = r.int(); val format = r.int(); val type = r.int()
|
|
972
|
+
val pixels = pixels(r, format, type)
|
|
973
|
+
withTightUnpacking(pixels.isImage) {
|
|
974
|
+
if (pixels.buffer == null) {
|
|
975
|
+
Log.w(TAG, "texSubImage2D from a PIXEL_UNPACK_BUFFER is not supported on Android")
|
|
976
|
+
} else {
|
|
977
|
+
GLES30.glTexSubImage2D(target, level, x, y, width, height, format, type, pixels.buffer)
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
WebGLOps.TEX_SUB_IMAGE3D -> {
|
|
982
|
+
val target = r.int(); val level = r.int(); val x = r.int(); val y = r.int(); val z = r.int()
|
|
983
|
+
val width = r.int(); val height = r.int(); val depth = r.int(); val format = r.int(); val type = r.int()
|
|
984
|
+
val pixels = pixels(r, format, type)
|
|
985
|
+
withTightUnpacking(pixels.isImage) {
|
|
986
|
+
if (pixels.buffer == null) {
|
|
987
|
+
GLES30.glTexSubImage3D(target, level, x, y, z, width, height, depth, format, type, pixels.offset)
|
|
988
|
+
} else {
|
|
989
|
+
GLES30.glTexSubImage3D(target, level, x, y, z, width, height, depth, format, type, pixels.buffer)
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
WebGLOps.TRANSFORM_FEEDBACK_VARYINGS -> {
|
|
994
|
+
val program = nameOf(r.int())
|
|
995
|
+
val varyings = r.array()
|
|
996
|
+
val names = Array(varyings.size()) { varyings.getString(it) }
|
|
997
|
+
GLES30.glTransformFeedbackVaryings(program, names, r.int())
|
|
998
|
+
}
|
|
999
|
+
WebGLOps.UNIFORM1F -> GLES30.glUniform1f(r.int(), r.float())
|
|
1000
|
+
WebGLOps.UNIFORM2F -> GLES30.glUniform2f(r.int(), r.float(), r.float())
|
|
1001
|
+
WebGLOps.UNIFORM3F -> GLES30.glUniform3f(r.int(), r.float(), r.float(), r.float())
|
|
1002
|
+
WebGLOps.UNIFORM4F -> GLES30.glUniform4f(r.int(), r.float(), r.float(), r.float(), r.float())
|
|
1003
|
+
WebGLOps.UNIFORM1I -> GLES30.glUniform1i(r.int(), r.int())
|
|
1004
|
+
WebGLOps.UNIFORM2I -> GLES30.glUniform2i(r.int(), r.int(), r.int())
|
|
1005
|
+
WebGLOps.UNIFORM3I -> GLES30.glUniform3i(r.int(), r.int(), r.int(), r.int())
|
|
1006
|
+
WebGLOps.UNIFORM4I -> GLES30.glUniform4i(r.int(), r.int(), r.int(), r.int(), r.int())
|
|
1007
|
+
WebGLOps.UNIFORM1UI -> GLES30.glUniform1ui(r.int(), r.long().toInt())
|
|
1008
|
+
WebGLOps.UNIFORM2UI -> GLES30.glUniform2ui(r.int(), r.long().toInt(), r.long().toInt())
|
|
1009
|
+
WebGLOps.UNIFORM3UI -> GLES30.glUniform3ui(r.int(), r.long().toInt(), r.long().toInt(), r.long().toInt())
|
|
1010
|
+
WebGLOps.UNIFORM4UI ->
|
|
1011
|
+
GLES30.glUniform4ui(r.int(), r.long().toInt(), r.long().toInt(), r.long().toInt(), r.long().toInt())
|
|
1012
|
+
WebGLOps.UNIFORM1FV, WebGLOps.UNIFORM2FV, WebGLOps.UNIFORM3FV, WebGLOps.UNIFORM4FV -> {
|
|
1013
|
+
val location = r.int()
|
|
1014
|
+
val values = floats(r.array())
|
|
1015
|
+
val size = op - WebGLOps.UNIFORM1FV + 1
|
|
1016
|
+
val count = values.size / size
|
|
1017
|
+
if (count > 0) {
|
|
1018
|
+
when (size) {
|
|
1019
|
+
1 -> GLES30.glUniform1fv(location, count, values, 0)
|
|
1020
|
+
2 -> GLES30.glUniform2fv(location, count, values, 0)
|
|
1021
|
+
3 -> GLES30.glUniform3fv(location, count, values, 0)
|
|
1022
|
+
else -> GLES30.glUniform4fv(location, count, values, 0)
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
WebGLOps.UNIFORM1IV, WebGLOps.UNIFORM2IV, WebGLOps.UNIFORM3IV, WebGLOps.UNIFORM4IV -> {
|
|
1027
|
+
val location = r.int()
|
|
1028
|
+
val values = ints(r.array())
|
|
1029
|
+
val size = op - WebGLOps.UNIFORM1IV + 1
|
|
1030
|
+
val count = values.size / size
|
|
1031
|
+
if (count > 0) {
|
|
1032
|
+
when (size) {
|
|
1033
|
+
1 -> GLES30.glUniform1iv(location, count, values, 0)
|
|
1034
|
+
2 -> GLES30.glUniform2iv(location, count, values, 0)
|
|
1035
|
+
3 -> GLES30.glUniform3iv(location, count, values, 0)
|
|
1036
|
+
else -> GLES30.glUniform4iv(location, count, values, 0)
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
WebGLOps.UNIFORM1UIV, WebGLOps.UNIFORM2UIV, WebGLOps.UNIFORM3UIV, WebGLOps.UNIFORM4UIV -> {
|
|
1041
|
+
val location = r.int()
|
|
1042
|
+
val values = ints(r.array())
|
|
1043
|
+
val size = op - WebGLOps.UNIFORM1UIV + 1
|
|
1044
|
+
val count = values.size / size
|
|
1045
|
+
if (count > 0) {
|
|
1046
|
+
when (size) {
|
|
1047
|
+
1 -> GLES30.glUniform1uiv(location, count, values, 0)
|
|
1048
|
+
2 -> GLES30.glUniform2uiv(location, count, values, 0)
|
|
1049
|
+
3 -> GLES30.glUniform3uiv(location, count, values, 0)
|
|
1050
|
+
else -> GLES30.glUniform4uiv(location, count, values, 0)
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
WebGLOps.UNIFORM_MATRIX2FV, WebGLOps.UNIFORM_MATRIX3FV, WebGLOps.UNIFORM_MATRIX4FV,
|
|
1055
|
+
WebGLOps.UNIFORM_MATRIX2X3FV, WebGLOps.UNIFORM_MATRIX3X2FV, WebGLOps.UNIFORM_MATRIX2X4FV,
|
|
1056
|
+
WebGLOps.UNIFORM_MATRIX4X2FV, WebGLOps.UNIFORM_MATRIX3X4FV, WebGLOps.UNIFORM_MATRIX4X3FV -> {
|
|
1057
|
+
val location = r.int()
|
|
1058
|
+
val transpose = r.bool()
|
|
1059
|
+
val v = floats(r.array())
|
|
1060
|
+
val n = v.size
|
|
1061
|
+
when (op) {
|
|
1062
|
+
WebGLOps.UNIFORM_MATRIX2FV -> if (n / 4 > 0) GLES30.glUniformMatrix2fv(location, n / 4, transpose, v, 0)
|
|
1063
|
+
WebGLOps.UNIFORM_MATRIX3FV -> if (n / 9 > 0) GLES30.glUniformMatrix3fv(location, n / 9, transpose, v, 0)
|
|
1064
|
+
WebGLOps.UNIFORM_MATRIX4FV -> if (n / 16 > 0) GLES30.glUniformMatrix4fv(location, n / 16, transpose, v, 0)
|
|
1065
|
+
WebGLOps.UNIFORM_MATRIX2X3FV -> if (n / 6 > 0) GLES30.glUniformMatrix2x3fv(location, n / 6, transpose, v, 0)
|
|
1066
|
+
WebGLOps.UNIFORM_MATRIX3X2FV -> if (n / 6 > 0) GLES30.glUniformMatrix3x2fv(location, n / 6, transpose, v, 0)
|
|
1067
|
+
WebGLOps.UNIFORM_MATRIX2X4FV -> if (n / 8 > 0) GLES30.glUniformMatrix2x4fv(location, n / 8, transpose, v, 0)
|
|
1068
|
+
WebGLOps.UNIFORM_MATRIX4X2FV -> if (n / 8 > 0) GLES30.glUniformMatrix4x2fv(location, n / 8, transpose, v, 0)
|
|
1069
|
+
WebGLOps.UNIFORM_MATRIX3X4FV -> if (n / 12 > 0) GLES30.glUniformMatrix3x4fv(location, n / 12, transpose, v, 0)
|
|
1070
|
+
else -> if (n / 12 > 0) GLES30.glUniformMatrix4x3fv(location, n / 12, transpose, v, 0)
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
WebGLOps.UNIFORM_BLOCK_BINDING -> {
|
|
1074
|
+
val program = nameOf(r.int())
|
|
1075
|
+
GLES30.glUniformBlockBinding(program, r.int(), r.int())
|
|
1076
|
+
}
|
|
1077
|
+
WebGLOps.USE_PROGRAM -> GLES30.glUseProgram(nameOf(r.int()))
|
|
1078
|
+
WebGLOps.VALIDATE_PROGRAM -> GLES30.glValidateProgram(nameOf(r.int()))
|
|
1079
|
+
WebGLOps.VERTEX_ATTRIB4F -> GLES30.glVertexAttrib4f(r.int(), r.float(), r.float(), r.float(), r.float())
|
|
1080
|
+
WebGLOps.VERTEX_ATTRIB_I4I -> GLES30.glVertexAttribI4i(r.int(), r.int(), r.int(), r.int(), r.int())
|
|
1081
|
+
WebGLOps.VERTEX_ATTRIB_I4UI ->
|
|
1082
|
+
GLES30.glVertexAttribI4ui(r.int(), r.long().toInt(), r.long().toInt(), r.long().toInt(), r.long().toInt())
|
|
1083
|
+
WebGLOps.VERTEX_ATTRIB_DIVISOR -> GLES30.glVertexAttribDivisor(r.int(), r.int())
|
|
1084
|
+
WebGLOps.VERTEX_ATTRIB_IPOINTER -> GLES30.glVertexAttribIPointer(r.int(), r.int(), r.int(), r.int(), r.int())
|
|
1085
|
+
WebGLOps.VERTEX_ATTRIB_POINTER ->
|
|
1086
|
+
GLES30.glVertexAttribPointer(r.int(), r.int(), r.int(), r.bool(), r.int(), r.int())
|
|
1087
|
+
WebGLOps.VIEWPORT -> GLES30.glViewport(r.int(), r.int(), r.int(), r.int())
|
|
1088
|
+
WebGLOps.WAIT_SYNC -> {
|
|
1089
|
+
val sync = syncs[r.int()]
|
|
1090
|
+
val flags = r.int()
|
|
1091
|
+
r.skip() // WebGL's timeout is always TIMEOUT_IGNORED (-1).
|
|
1092
|
+
if (sync != null) GLES30.glWaitSync(sync, flags, GLES30.GL_TIMEOUT_IGNORED)
|
|
1093
|
+
}
|
|
1094
|
+
WebGLOps.RESIZE -> allocateDrawingBuffer(r.int(), r.int())
|
|
1095
|
+
WebGLOps.PRESENT -> present()
|
|
1096
|
+
else -> {
|
|
1097
|
+
Log.w(TAG, "Unknown command $op")
|
|
1098
|
+
// The rest of the batch can't be read without this one's arguments.
|
|
1099
|
+
r.index = r.size
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
private fun shaderLog(shader: Int): String = GLES30.glGetShaderInfoLog(shader) ?: ""
|
|
1105
|
+
private fun programLog(program: Int): String = GLES30.glGetProgramInfoLog(program) ?: ""
|
|
1106
|
+
|
|
1107
|
+
/** The link status, log and reflection three.js reads after each link, in one answer. Built-ins are left out, as WebGL does. */
|
|
1108
|
+
private fun programInfo(program: Int): JavaOnlyMap {
|
|
1109
|
+
val status = IntArray(1)
|
|
1110
|
+
GLES30.glGetProgramiv(program, GLES30.GL_LINK_STATUS, status, 0)
|
|
1111
|
+
val linked = status[0] == GLES30.GL_TRUE
|
|
1112
|
+
val uniforms = JavaOnlyArray()
|
|
1113
|
+
val attributes = JavaOnlyArray()
|
|
1114
|
+
val counts = IntArray(2)
|
|
1115
|
+
if (linked) {
|
|
1116
|
+
val size = IntArray(1)
|
|
1117
|
+
val type = IntArray(1)
|
|
1118
|
+
GLES30.glGetProgramiv(program, GLES30.GL_ACTIVE_UNIFORMS, counts, 0)
|
|
1119
|
+
for (i in 0 until counts[0]) {
|
|
1120
|
+
val name = GLES30.glGetActiveUniform(program, i, size, 0, type, 0) ?: continue
|
|
1121
|
+
if (name.startsWith("gl_")) continue
|
|
1122
|
+
uniforms.pushArray(JavaOnlyArray.of(name, size[0], type[0], GLES30.glGetUniformLocation(program, name)))
|
|
1123
|
+
}
|
|
1124
|
+
GLES30.glGetProgramiv(program, GLES30.GL_ACTIVE_ATTRIBUTES, counts, 0)
|
|
1125
|
+
for (i in 0 until counts[0]) {
|
|
1126
|
+
val name = GLES30.glGetActiveAttrib(program, i, size, 0, type, 0) ?: continue
|
|
1127
|
+
if (name.startsWith("gl_")) continue
|
|
1128
|
+
attributes.pushArray(JavaOnlyArray.of(name, size[0], type[0], GLES30.glGetAttribLocation(program, name)))
|
|
1129
|
+
}
|
|
1130
|
+
GLES30.glGetProgramiv(program, GLES30.GL_ACTIVE_UNIFORM_BLOCKS, counts, 0)
|
|
1131
|
+
GLES30.glGetProgramiv(program, GLES30.GL_TRANSFORM_FEEDBACK_VARYINGS, counts, 1)
|
|
1132
|
+
} else {
|
|
1133
|
+
counts[0] = 0
|
|
1134
|
+
}
|
|
1135
|
+
return JavaOnlyMap().apply {
|
|
1136
|
+
putBoolean("linked", linked)
|
|
1137
|
+
putString("log", programLog(program))
|
|
1138
|
+
putArray("uniforms", uniforms)
|
|
1139
|
+
putArray("attributes", attributes)
|
|
1140
|
+
putInt("uniformBlocks", if (linked) counts[0] else 0)
|
|
1141
|
+
putInt("transformFeedbackVaryings", if (linked) counts[1] else 0)
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
private fun parameter(pname: Int): Any? {
|
|
1146
|
+
return when (pname) {
|
|
1147
|
+
WEBGL_UNMASKED_VENDOR -> GLES30.glGetString(GLES30.GL_VENDOR)
|
|
1148
|
+
WEBGL_UNMASKED_RENDERER -> GLES30.glGetString(GLES30.GL_RENDERER)
|
|
1149
|
+
GLES30.GL_VENDOR, GLES30.GL_RENDERER, GLES30.GL_VERSION, GLES30.GL_SHADING_LANGUAGE_VERSION ->
|
|
1150
|
+
GLES30.glGetString(pname)
|
|
1151
|
+
GLES30.GL_ALIASED_LINE_WIDTH_RANGE, GLES30.GL_ALIASED_POINT_SIZE_RANGE, GLES30.GL_DEPTH_RANGE ->
|
|
1152
|
+
floatArrayResult(FloatArray(2).also { GLES30.glGetFloatv(pname, it, 0) })
|
|
1153
|
+
GLES30.GL_COLOR_CLEAR_VALUE, GLES30.GL_BLEND_COLOR ->
|
|
1154
|
+
floatArrayResult(FloatArray(4).also { GLES30.glGetFloatv(pname, it, 0) })
|
|
1155
|
+
GLES30.GL_MAX_VIEWPORT_DIMS -> intArrayResult(IntArray(2).also { GLES30.glGetIntegerv(pname, it, 0) })
|
|
1156
|
+
GLES30.GL_SCISSOR_BOX, GLES30.GL_VIEWPORT -> intArrayResult(IntArray(4).also { GLES30.glGetIntegerv(pname, it, 0) })
|
|
1157
|
+
GLES30.GL_COLOR_WRITEMASK -> {
|
|
1158
|
+
val values = BooleanArray(4).also { GLES30.glGetBooleanv(pname, it, 0) }
|
|
1159
|
+
JavaOnlyArray().apply { values.forEach { pushBoolean(it) } }
|
|
1160
|
+
}
|
|
1161
|
+
GLES30.GL_COMPRESSED_TEXTURE_FORMATS -> {
|
|
1162
|
+
val count = IntArray(1).also { GLES30.glGetIntegerv(GLES30.GL_NUM_COMPRESSED_TEXTURE_FORMATS, it, 0) }[0]
|
|
1163
|
+
intArrayResult(IntArray(maxOf(count, 1)).also { GLES30.glGetIntegerv(pname, it, 0) }, count)
|
|
1164
|
+
}
|
|
1165
|
+
GLES30.GL_DEPTH_CLEAR_VALUE, GLES30.GL_LINE_WIDTH, GLES30.GL_POLYGON_OFFSET_FACTOR,
|
|
1166
|
+
GLES30.GL_POLYGON_OFFSET_UNITS, GLES30.GL_SAMPLE_COVERAGE_VALUE, GLES30.GL_MAX_TEXTURE_LOD_BIAS,
|
|
1167
|
+
MAX_TEXTURE_MAX_ANISOTROPY -> FloatArray(1).also { GLES30.glGetFloatv(pname, it, 0) }[0]
|
|
1168
|
+
GLES30.GL_MAX_ELEMENT_INDEX, GLES30.GL_MAX_SERVER_WAIT_TIMEOUT, GLES30.GL_MAX_UNIFORM_BLOCK_SIZE,
|
|
1169
|
+
GLES30.GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS, GLES30.GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS ->
|
|
1170
|
+
LongArray(1).also { GLES30.glGetInteger64v(pname, it, 0) }[0]
|
|
1171
|
+
GLES30.GL_BLEND, GLES30.GL_CULL_FACE, GLES30.GL_DEPTH_TEST, GLES30.GL_DITHER, GLES30.GL_POLYGON_OFFSET_FILL,
|
|
1172
|
+
GLES30.GL_RASTERIZER_DISCARD, GLES30.GL_SAMPLE_ALPHA_TO_COVERAGE, GLES30.GL_SAMPLE_COVERAGE,
|
|
1173
|
+
GLES30.GL_SCISSOR_TEST, GLES30.GL_STENCIL_TEST -> GLES30.glIsEnabled(pname)
|
|
1174
|
+
GLES30.GL_DEPTH_WRITEMASK, GLES30.GL_SAMPLE_COVERAGE_INVERT, GLES30.GL_TRANSFORM_FEEDBACK_ACTIVE,
|
|
1175
|
+
GLES30.GL_TRANSFORM_FEEDBACK_PAUSED -> BooleanArray(1).also { GLES30.glGetBooleanv(pname, it, 0) }[0]
|
|
1176
|
+
GLES30.GL_STENCIL_VALUE_MASK, GLES30.GL_STENCIL_WRITEMASK, GLES30.GL_STENCIL_BACK_VALUE_MASK,
|
|
1177
|
+
GLES30.GL_STENCIL_BACK_WRITEMASK ->
|
|
1178
|
+
IntArray(1).also { GLES30.glGetIntegerv(pname, it, 0) }[0].toLong() and 0xffffffffL
|
|
1179
|
+
else -> IntArray(1).also { GLES30.glGetIntegerv(pname, it, 0) }[0]
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
private fun uniformComponents(type: Int): Int = when (type) {
|
|
1184
|
+
GLES30.GL_FLOAT_VEC2, GLES30.GL_INT_VEC2, GLES30.GL_UNSIGNED_INT_VEC2, GLES30.GL_BOOL_VEC2 -> 2
|
|
1185
|
+
GLES30.GL_FLOAT_VEC3, GLES30.GL_INT_VEC3, GLES30.GL_UNSIGNED_INT_VEC3, GLES30.GL_BOOL_VEC3 -> 3
|
|
1186
|
+
GLES30.GL_FLOAT_VEC4, GLES30.GL_INT_VEC4, GLES30.GL_UNSIGNED_INT_VEC4, GLES30.GL_BOOL_VEC4,
|
|
1187
|
+
GLES30.GL_FLOAT_MAT2 -> 4
|
|
1188
|
+
GLES30.GL_FLOAT_MAT2x3, GLES30.GL_FLOAT_MAT3x2 -> 6
|
|
1189
|
+
GLES30.GL_FLOAT_MAT2x4, GLES30.GL_FLOAT_MAT4x2 -> 8
|
|
1190
|
+
GLES30.GL_FLOAT_MAT3 -> 9
|
|
1191
|
+
GLES30.GL_FLOAT_MAT3x4, GLES30.GL_FLOAT_MAT4x3 -> 12
|
|
1192
|
+
GLES30.GL_FLOAT_MAT4 -> 16
|
|
1193
|
+
else -> 1
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
private fun isFloatUniform(type: Int): Boolean = type in setOf(
|
|
1197
|
+
GLES30.GL_FLOAT, GLES30.GL_FLOAT_VEC2, GLES30.GL_FLOAT_VEC3, GLES30.GL_FLOAT_VEC4,
|
|
1198
|
+
GLES30.GL_FLOAT_MAT2, GLES30.GL_FLOAT_MAT3, GLES30.GL_FLOAT_MAT4, GLES30.GL_FLOAT_MAT2x3,
|
|
1199
|
+
GLES30.GL_FLOAT_MAT3x2, GLES30.GL_FLOAT_MAT2x4, GLES30.GL_FLOAT_MAT4x2, GLES30.GL_FLOAT_MAT3x4,
|
|
1200
|
+
GLES30.GL_FLOAT_MAT4x3,
|
|
1201
|
+
)
|
|
1202
|
+
|
|
1203
|
+
private fun query(op: Int, r: Reader): Any? {
|
|
1204
|
+
return when (op) {
|
|
1205
|
+
WebGLOps.GET_PARAMETER -> parameter(r.int())
|
|
1206
|
+
WebGLOps.GET_ERROR -> GLES30.glGetError()
|
|
1207
|
+
WebGLOps.SHADER_INFO -> {
|
|
1208
|
+
val shader = nameOf(r.int())
|
|
1209
|
+
val status = IntArray(1).also { GLES30.glGetShaderiv(shader, GLES30.GL_COMPILE_STATUS, it, 0) }
|
|
1210
|
+
JavaOnlyMap().apply {
|
|
1211
|
+
putBoolean("compiled", status[0] == GLES30.GL_TRUE)
|
|
1212
|
+
putString("log", shaderLog(shader))
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
WebGLOps.PROGRAM_INFO -> programInfo(nameOf(r.int()))
|
|
1216
|
+
WebGLOps.GET_UNIFORM_LOCATION -> {
|
|
1217
|
+
val program = nameOf(r.int())
|
|
1218
|
+
GLES30.glGetUniformLocation(program, r.string())
|
|
1219
|
+
}
|
|
1220
|
+
WebGLOps.GET_ATTRIB_LOCATION -> {
|
|
1221
|
+
val program = nameOf(r.int())
|
|
1222
|
+
GLES30.glGetAttribLocation(program, r.string())
|
|
1223
|
+
}
|
|
1224
|
+
WebGLOps.CHECK_FRAMEBUFFER_STATUS -> GLES30.glCheckFramebufferStatus(r.int())
|
|
1225
|
+
WebGLOps.GET_BUFFER_PARAMETER -> {
|
|
1226
|
+
val target = r.int()
|
|
1227
|
+
val pname = r.int()
|
|
1228
|
+
if (pname == GLES30.GL_BUFFER_SIZE) {
|
|
1229
|
+
LongArray(1).also { GLES30.glGetBufferParameteri64v(target, pname, it, 0) }[0]
|
|
1230
|
+
} else {
|
|
1231
|
+
IntArray(1).also { GLES30.glGetBufferParameteriv(target, pname, it, 0) }[0]
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
WebGLOps.GET_FRAMEBUFFER_ATTACHMENT_PARAMETER -> {
|
|
1235
|
+
val target = r.int()
|
|
1236
|
+
val attachment = attachmentFor(r.int(), target)
|
|
1237
|
+
IntArray(1).also { GLES30.glGetFramebufferAttachmentParameteriv(target, attachment, r.int(), it, 0) }[0]
|
|
1238
|
+
}
|
|
1239
|
+
WebGLOps.GET_RENDERBUFFER_PARAMETER -> {
|
|
1240
|
+
val target = r.int()
|
|
1241
|
+
IntArray(1).also { GLES30.glGetRenderbufferParameteriv(target, r.int(), it, 0) }[0]
|
|
1242
|
+
}
|
|
1243
|
+
WebGLOps.GET_TEX_PARAMETER -> {
|
|
1244
|
+
val target = r.int()
|
|
1245
|
+
val pname = r.int()
|
|
1246
|
+
if (pname == GLES30.GL_TEXTURE_MAX_LOD || pname == GLES30.GL_TEXTURE_MIN_LOD || pname == TEXTURE_MAX_ANISOTROPY) {
|
|
1247
|
+
FloatArray(1).also { GLES30.glGetTexParameterfv(target, pname, it, 0) }[0]
|
|
1248
|
+
} else {
|
|
1249
|
+
IntArray(1).also { GLES30.glGetTexParameteriv(target, pname, it, 0) }[0]
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
WebGLOps.GET_SAMPLER_PARAMETER -> {
|
|
1253
|
+
val sampler = nameOf(r.int())
|
|
1254
|
+
val pname = r.int()
|
|
1255
|
+
if (pname == GLES30.GL_TEXTURE_MAX_LOD || pname == GLES30.GL_TEXTURE_MIN_LOD) {
|
|
1256
|
+
FloatArray(1).also { GLES30.glGetSamplerParameterfv(sampler, pname, it, 0) }[0]
|
|
1257
|
+
} else {
|
|
1258
|
+
IntArray(1).also { GLES30.glGetSamplerParameteriv(sampler, pname, it, 0) }[0]
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
WebGLOps.GET_VERTEX_ATTRIB -> {
|
|
1262
|
+
val index = r.int()
|
|
1263
|
+
val pname = r.int()
|
|
1264
|
+
if (pname == GLES30.GL_CURRENT_VERTEX_ATTRIB) {
|
|
1265
|
+
floatArrayResult(FloatArray(4).also { GLES30.glGetVertexAttribfv(index, pname, it, 0) })
|
|
1266
|
+
} else {
|
|
1267
|
+
IntArray(1).also { GLES30.glGetVertexAttribiv(index, pname, it, 0) }[0]
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
WebGLOps.GET_VERTEX_ATTRIB_OFFSET -> {
|
|
1271
|
+
// Android's GLES has no glGetVertexAttribPointerv.
|
|
1272
|
+
r.skip(); r.skip()
|
|
1273
|
+
0
|
|
1274
|
+
}
|
|
1275
|
+
WebGLOps.GET_INDEXED_PARAMETER -> {
|
|
1276
|
+
val target = r.int()
|
|
1277
|
+
val index = r.int()
|
|
1278
|
+
when (target) {
|
|
1279
|
+
GLES30.GL_UNIFORM_BUFFER_START, GLES30.GL_UNIFORM_BUFFER_SIZE,
|
|
1280
|
+
GLES30.GL_TRANSFORM_FEEDBACK_BUFFER_START, GLES30.GL_TRANSFORM_FEEDBACK_BUFFER_SIZE ->
|
|
1281
|
+
LongArray(1).also { GLES30.glGetInteger64i_v(target, index, it, 0) }[0]
|
|
1282
|
+
else -> IntArray(1).also { GLES30.glGetIntegeri_v(target, index, it, 0) }[0]
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
WebGLOps.GET_INTERNALFORMAT_PARAMETER -> {
|
|
1286
|
+
val target = r.int(); val internalformat = r.int(); val pname = r.int()
|
|
1287
|
+
val count = IntArray(1).also {
|
|
1288
|
+
GLES30.glGetInternalformativ(target, internalformat, GLES30.GL_NUM_SAMPLE_COUNTS, 1, it, 0)
|
|
1289
|
+
}[0]
|
|
1290
|
+
val values = IntArray(maxOf(count, 1))
|
|
1291
|
+
GLES30.glGetInternalformativ(target, internalformat, pname, count, values, 0)
|
|
1292
|
+
intArrayResult(values, count)
|
|
1293
|
+
}
|
|
1294
|
+
WebGLOps.GET_QUERY_PARAMETER -> {
|
|
1295
|
+
val query = nameOf(r.int())
|
|
1296
|
+
IntArray(1).also { GLES30.glGetQueryObjectuiv(query, r.int(), it, 0) }[0].toLong() and 0xffffffffL
|
|
1297
|
+
}
|
|
1298
|
+
WebGLOps.GET_SYNC_PARAMETER -> {
|
|
1299
|
+
val sync = syncs[r.int()]
|
|
1300
|
+
val pname = r.int()
|
|
1301
|
+
if (sync == null) null else IntArray(1).also { GLES30.glGetSynciv(sync, pname, 1, null, 0, it, 0) }[0]
|
|
1302
|
+
}
|
|
1303
|
+
WebGLOps.CLIENT_WAIT_SYNC -> {
|
|
1304
|
+
val sync = syncs[r.int()]
|
|
1305
|
+
val flags = r.int()
|
|
1306
|
+
val timeout = maxOf(0L, r.long())
|
|
1307
|
+
if (sync == null) GLES30.GL_WAIT_FAILED else GLES30.glClientWaitSync(sync, flags, timeout)
|
|
1308
|
+
}
|
|
1309
|
+
WebGLOps.GET_UNIFORM -> {
|
|
1310
|
+
val program = nameOf(r.int())
|
|
1311
|
+
val location = r.int()
|
|
1312
|
+
val type = r.int()
|
|
1313
|
+
val count = uniformComponents(type)
|
|
1314
|
+
if (isFloatUniform(type)) {
|
|
1315
|
+
floatArrayResult(FloatArray(16).also { GLES30.glGetUniformfv(program, location, it, 0) }, count)
|
|
1316
|
+
} else if (type == GLES30.GL_UNSIGNED_INT || type == GLES30.GL_UNSIGNED_INT_VEC2 ||
|
|
1317
|
+
type == GLES30.GL_UNSIGNED_INT_VEC3 || type == GLES30.GL_UNSIGNED_INT_VEC4
|
|
1318
|
+
) {
|
|
1319
|
+
val values = IntArray(4).also { GLES30.glGetUniformuiv(program, location, it, 0) }
|
|
1320
|
+
JavaOnlyArray().apply { for (i in 0 until count) pushDouble((values[i].toLong() and 0xffffffffL).toDouble()) }
|
|
1321
|
+
} else {
|
|
1322
|
+
intArrayResult(IntArray(4).also { GLES30.glGetUniformiv(program, location, it, 0) }, count)
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
WebGLOps.GET_UNIFORM_BLOCK_INDEX -> {
|
|
1326
|
+
val program = nameOf(r.int())
|
|
1327
|
+
GLES30.glGetUniformBlockIndex(program, r.string()).toLong() and 0xffffffffL
|
|
1328
|
+
}
|
|
1329
|
+
WebGLOps.GET_ACTIVE_UNIFORM_BLOCK_NAME -> {
|
|
1330
|
+
val program = nameOf(r.int())
|
|
1331
|
+
GLES30.glGetActiveUniformBlockName(program, r.int())
|
|
1332
|
+
}
|
|
1333
|
+
WebGLOps.GET_ACTIVE_UNIFORM_BLOCK_PARAMETER -> {
|
|
1334
|
+
val program = nameOf(r.int())
|
|
1335
|
+
val index = r.int()
|
|
1336
|
+
val pname = r.int()
|
|
1337
|
+
if (pname == GLES30.GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES) {
|
|
1338
|
+
val count = IntArray(1).also {
|
|
1339
|
+
GLES30.glGetActiveUniformBlockiv(program, index, GLES30.GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, it, 0)
|
|
1340
|
+
}[0]
|
|
1341
|
+
val values = IntArray(maxOf(count, 1))
|
|
1342
|
+
GLES30.glGetActiveUniformBlockiv(program, index, pname, values, 0)
|
|
1343
|
+
intArrayResult(values, count)
|
|
1344
|
+
} else {
|
|
1345
|
+
IntArray(1).also { GLES30.glGetActiveUniformBlockiv(program, index, pname, it, 0) }[0]
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
WebGLOps.GET_ACTIVE_UNIFORMS -> {
|
|
1349
|
+
val program = nameOf(r.int())
|
|
1350
|
+
val indices = ints(r.array())
|
|
1351
|
+
val pname = r.int()
|
|
1352
|
+
val values = IntArray(maxOf(indices.size, 1))
|
|
1353
|
+
GLES30.glGetActiveUniformsiv(program, indices.size, indices, 0, pname, values, 0)
|
|
1354
|
+
intArrayResult(values, indices.size)
|
|
1355
|
+
}
|
|
1356
|
+
WebGLOps.GET_UNIFORM_INDICES -> {
|
|
1357
|
+
val program = nameOf(r.int())
|
|
1358
|
+
val names = r.array()
|
|
1359
|
+
val array = Array(names.size()) { names.getString(it) }
|
|
1360
|
+
val indices = IntArray(maxOf(array.size, 1))
|
|
1361
|
+
GLES30.glGetUniformIndices(program, array, indices, 0)
|
|
1362
|
+
JavaOnlyArray().apply {
|
|
1363
|
+
for (i in array.indices) pushDouble((indices[i].toLong() and 0xffffffffL).toDouble())
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
WebGLOps.GET_FRAG_DATA_LOCATION -> {
|
|
1367
|
+
val program = nameOf(r.int())
|
|
1368
|
+
GLES30.glGetFragDataLocation(program, r.string())
|
|
1369
|
+
}
|
|
1370
|
+
WebGLOps.GET_TRANSFORM_FEEDBACK_VARYING -> {
|
|
1371
|
+
val program = nameOf(r.int())
|
|
1372
|
+
val index = r.int()
|
|
1373
|
+
val size = IntArray(1)
|
|
1374
|
+
val type = IntArray(1)
|
|
1375
|
+
val name = GLES30.glGetTransformFeedbackVarying(program, index, size, 0, type, 0) ?: ""
|
|
1376
|
+
JavaOnlyArray.of(name, size[0], type[0])
|
|
1377
|
+
}
|
|
1378
|
+
WebGLOps.READ_PIXELS -> {
|
|
1379
|
+
val x = r.int(); val y = r.int(); val width = r.int(); val height = r.int()
|
|
1380
|
+
val format = r.int(); val type = r.int()
|
|
1381
|
+
val length = maxOf(0, r.int())
|
|
1382
|
+
val buffer = ByteBuffer.allocateDirect((length + 3) / 4 * 4).order(ByteOrder.nativeOrder())
|
|
1383
|
+
reading { GLES30.glReadPixels(x, y, width, height, format, type, buffer) }
|
|
1384
|
+
wordsFromBytes(buffer, length)
|
|
1385
|
+
}
|
|
1386
|
+
WebGLOps.GET_BUFFER_SUB_DATA -> {
|
|
1387
|
+
val target = r.int()
|
|
1388
|
+
val offset = r.int()
|
|
1389
|
+
val length = r.int()
|
|
1390
|
+
if (length <= 0) {
|
|
1391
|
+
wordsFromBytes(ByteBuffer.allocateDirect(0), 0)
|
|
1392
|
+
} else {
|
|
1393
|
+
val mapped = GLES30.glMapBufferRange(target, offset, length, GLES30.GL_MAP_READ_BIT) as? ByteBuffer
|
|
1394
|
+
if (mapped == null) {
|
|
1395
|
+
null
|
|
1396
|
+
} else {
|
|
1397
|
+
val result = wordsFromBytes(mapped, length)
|
|
1398
|
+
GLES30.glUnmapBuffer(target)
|
|
1399
|
+
result
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
WebGLOps.IS_ENABLED -> GLES30.glIsEnabled(r.int())
|
|
1404
|
+
WebGLOps.GET_SHADER_PRECISION_FORMAT -> {
|
|
1405
|
+
val shaderType = r.int()
|
|
1406
|
+
val precisionType = r.int()
|
|
1407
|
+
val range = IntArray(2)
|
|
1408
|
+
val precision = IntArray(1)
|
|
1409
|
+
GLES30.glGetShaderPrecisionFormat(shaderType, precisionType, range, 0, precision, 0)
|
|
1410
|
+
JavaOnlyArray.of(range[0], range[1], precision[0])
|
|
1411
|
+
}
|
|
1412
|
+
WebGLOps.GET_SUPPORTED_EXTENSIONS -> {
|
|
1413
|
+
val count = IntArray(1).also { GLES30.glGetIntegerv(GLES30.GL_NUM_EXTENSIONS, it, 0) }[0]
|
|
1414
|
+
JavaOnlyArray().apply {
|
|
1415
|
+
for (i in 0 until count) GLES30.glGetStringi(GLES30.GL_EXTENSIONS, i)?.let { pushString(it) }
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
WebGLOps.GET_PROGRAM_PARAMETER -> {
|
|
1419
|
+
val program = nameOf(r.int())
|
|
1420
|
+
IntArray(1).also { GLES30.glGetProgramiv(program, r.int(), it, 0) }[0]
|
|
1421
|
+
}
|
|
1422
|
+
else -> {
|
|
1423
|
+
Log.w(TAG, "Unknown query $op")
|
|
1424
|
+
r.index = r.size
|
|
1425
|
+
null
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
companion object {
|
|
1431
|
+
private const val TAG = "AngularLynxWebGL"
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
private fun ReadableMap?.flag(key: String, default: Boolean): Boolean {
|
|
1436
|
+
if (this == null || !hasKey(key)) return default
|
|
1437
|
+
return when (getType(key)) {
|
|
1438
|
+
ReadableType.Boolean -> getBoolean(key)
|
|
1439
|
+
in NUMBER_TYPES -> getDouble(key) != 0.0
|
|
1440
|
+
else -> default
|
|
1441
|
+
}
|
|
1442
|
+
}
|