-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkerKha.hx
491 lines (463 loc) · 17.1 KB
/
WorkerKha.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package;
import kha.graphics4.StencilValue;
import js.lib.ArrayBuffer;
import kha.graphics4.DepthStencilFormat;
import kha.graphics4.TextureFormat;
import js.Browser;
import js.html.Worker;
import kha.Assets;
import kha.audio1.Audio;
import kha.Blob;
import kha.Color;
import kha.Framebuffer;
import kha.Image;
import kha.graphics4.ConstantLocation;
import kha.graphics4.FragmentShader;
import kha.graphics4.IndexBuffer;
import kha.graphics4.TextureUnit;
import kha.graphics4.VertexBuffer;
import kha.graphics4.VertexElement;
import kha.graphics4.VertexShader;
import kha.graphics4.VertexStructure;
import kha.input.Keyboard;
import kha.input.KeyCode;
import kha.input.Mouse;
import kha.math.FastMatrix3;
import kha.math.FastMatrix4;
import kha.math.FastVector2;
import kha.math.FastVector3;
import kha.math.FastVector4;
import kha.Sound;
import kha.System;
using StringTools;
class Frame {
public var commands: Array<Dynamic>;
public function new() {
commands = [];
}
}
@:expose
class WorkerKha {
public static var instance: WorkerKha;
var worker: Worker;
var frames: Array<Frame>;
var currentFrame: Frame;
var images: Map<Int, Image>;
var lastImageId: Int;
var shaders: Map<String, Dynamic>;
var pipelines: Map<Int, Pipeline>;
var pipelinesByVertexShader: Map<String, Pipeline>;
var pipelinesByFragmentShader: Map<String, Pipeline>;
var indexBuffers: Map<Int, IndexBuffer>;
var vertexBuffers: Map<Int, VertexBuffer>;
var constantLocations: Map<Int, ConstantLocation>;
var textureUnits: Map<Int, TextureUnit>;
var renderTargets: Map<Int, Image>;
var sounds: Map<Int, Sound>;
var workerDir: String;
//var parser: Parser;
var width: Int;
var height: Int;
var renderTarget: Image;
public function new() {
instance = this;
frames = [];
currentFrame = new Frame();
images = new Map();
shaders = new Map();
pipelines = new Map();
pipelinesByVertexShader = new Map();
pipelinesByFragmentShader = new Map();
indexBuffers = new Map();
vertexBuffers = new Map();
constantLocations = new Map();
textureUnits = new Map();
renderTargets = new Map();
sounds = new Map();
lastImageId = 0;
Keyboard.get().notify(keyDown, keyUp, keyPress);
Mouse.get().notify(mouseDown, mouseUp, mouseMove, mouseWheel);
worker = null;
renderTarget = Image.createRenderTarget(Browser.window.screen.width, Browser.window.screen.height, TextureFormat.RGBA32, DepthStencilFormat.DepthAutoStencilAuto);
}
function loadText(path: String, callback: String->Void): Void {
Assets.loadBlobFromPath(path, (blob) -> {
callback(blob.toString());
}, (error) -> {
trace("Error loading " + path);
});
}
public function load(workerPath: String): Void {
loadText(workerPath, function (source: String) {
//parser = new Parser();
//parser.parse(source, null);
if (worker != null) {
worker.terminate();
}
for (image in images) {
image.unload();
}
for (pipeline in pipelines) {
pipeline.state.delete();
}
for (buffer in indexBuffers) {
buffer.delete();
}
for (buffer in vertexBuffers) {
buffer.delete();
}
for (image in renderTargets) {
image.unload();
}
for (sound in sounds) {
sound.unload();
}
images = new Map();
shaders = new Map();
pipelines = new Map();
pipelinesByVertexShader = new Map();
pipelinesByFragmentShader = new Map();
indexBuffers = new Map();
vertexBuffers = new Map();
constantLocations = new Map();
textureUnits = new Map();
renderTargets = new Map();
sounds = new Map();
width = -1;
height = -1;
frames = [];
lastImageId = 0;
workerDir = workerPath.substring(0, workerPath.lastIndexOf("/") + 1);
worker = new Worker(workerPath);
worker.addEventListener('message', onMessage, false);
});
}
public function inject(workerPath: String): Void {
loadText(workerPath, function (source: String) {
//parser.parse(source, worker);
});
}
function transformShaderName(name: String, type: String): String {
if (kha.SystemImpl.gl2) {
return name + "-webgl2." + type + ".essl";
}
else {
var highp = kha.SystemImpl.gl.getShaderPrecisionFormat(js.html.webgl.GL.FRAGMENT_SHADER, js.html.webgl.GL.HIGH_FLOAT);
var highpSupported = highp.precision != 0;
if (!highpSupported) {
return name + "-relaxed." + type + ".essl";
}
else {
return name + "." + type + ".essl";
}
}
}
public function injectShader(shaderPath: String): Void {
var localPath = shaderPath.substr(shaderPath.lastIndexOf("/") + 1);
localPath = localPath.substr(0, localPath.length - 4) + "essl";
if (shaderPath.endsWith(".frag.glsl")) {
shaderPath = transformShaderName(shaderPath.substr(0, shaderPath.length - 10), "frag");
}
else {
shaderPath = transformShaderName(shaderPath.substr(0, shaderPath.length - 10), "vert");
}
loadText(shaderPath, function (source: String) {
if (shaderPath.endsWith(".frag.essl")) {
var shader = FragmentShader.fromSource(source);
this.shaders[localPath] = shader;
var pipeline = pipelinesByFragmentShader[localPath];
if (pipeline != null) {
pipeline.state.fragmentShader = shader;
pipeline.state.compile(); // works in webgl but don't do it for portable code
pipeline.update();
}
}
else if (shaderPath.endsWith(".vert.essl")) {
var shader = VertexShader.fromSource(source);
this.shaders[localPath] = shader;
var pipeline = pipelinesByVertexShader[localPath];
if (pipeline != null) {
pipeline.state.vertexShader = shader;
pipeline.state.compile(); // works in webgl but don't do it for portable code
pipeline.update();
}
}
});
}
public function render(framebuffers: Array<Framebuffer>): Void {
var framebuffer = framebuffers[0];
if (System.windowWidth() != width || System.windowHeight() != height) {
width = System.windowWidth();
height = System.windowHeight();
if (worker != null) {
worker.postMessage({ command: 'setWindowSize', width: width, height: height });
}
}
if (frames.length > 0) {
var g = renderTarget.g4;
for (frame in frames) {
var commands = frame.commands;
for (command in commands) {
switch (command.command) {
case 'begin':
if (command.renderTarget < 0) {
g = renderTarget.g4;
g.begin();
g.viewport(0, 0, width, height);
}
else {
g = renderTargets[command.renderTarget].g4;
g.begin();
}
case 'clear':
g.clear(command.color == null ? null : Color.fromValue(command.color), command.hasDepth ? command.depth : null, command.hasStencil ? command.stencil : null);
case 'setPipeline':
g.setPipeline(pipelines[command.id].state);
case 'updateIndexBuffer':
var indexBuffer = indexBuffers[command.id];
var data: ArrayBuffer = indexBuffer.lock().buffer;
new js.lib.Uint8Array(data).set(new js.lib.Uint8Array(command.data));
indexBuffer.unlock();
case 'updateVertexBuffer':
var vertexBuffer = vertexBuffers[command.id];
var start: Int = command.start;
var count: Int = command.count;
var data: ArrayBuffer = vertexBuffer.lock(start, count).buffer;
new js.lib.Uint8Array(data).set(new js.lib.Uint8Array(command.data));
vertexBuffer.unlock();
case 'unlockImage':
var image = images[command.id];
var bytes = image.lock();
new js.lib.Uint8Array(bytes.getData()).set(new js.lib.Uint8Array(command.bytes));
image.unlock();
case 'setIndexBuffer':
g.setIndexBuffer(indexBuffers[command.id]);
case 'setVertexBuffer':
g.setVertexBuffer(vertexBuffers[command.id]);
case 'setVertexBuffers':
var buffers = new Array<VertexBuffer>();
for (i in 0...command.ids.length) {
buffers.push(vertexBuffers[command.ids[i]]);
}
g.setVertexBuffers(buffers);
case 'createConstantLocation':
constantLocations[command.id] = pipelines[command.pipeline].getConstantLocation(command.name);
case 'createTextureUnit':
textureUnits[command.id] = pipelines[command.pipeline].getTextureUnit(command.name);
case 'setTexture':
if (command.texture < 0 && command.renderTarget < 0) {
g.setTexture(textureUnits[command.stage], null);
}
else if (command.texture < 0) {
g.setTexture(textureUnits[command.stage], renderTargets[command.renderTarget]);
}
else {
g.setTexture(textureUnits[command.stage], images[command.texture]);
}
case 'setTextureParameters':
g.setTextureParameters(textureUnits[command.id], command.uAddressing, command.vAddressing,
command.minificationFilter, command.magnificationFilter, command.mipmapFilter);
case 'setMatrix3':
g.setMatrix3(constantLocations[command.location], new FastMatrix3(command._00, command._10, command._20, command._01, command._11, command._21, command._02, command._12, command._22));
case 'setMatrix4':
g.setMatrix(constantLocations[command.location], new FastMatrix4(command._00, command._10, command._20, command._30,
command._01, command._11, command._21, command._31, command._02, command._12, command._22, command._32,
command._03, command._13, command._23, command._33));
case 'setVector2':
g.setVector2(constantLocations[command.location], new FastVector2(command.x, command.y));
case 'setVector3':
g.setVector3(constantLocations[command.location], new FastVector3(command.x, command.y, command.z));
case 'setVector4':
g.setVector4(constantLocations[command.location], new FastVector4(command.x, command.y, command.z, command.w));
case 'setFloats':
g.setFloats(constantLocations[command.location], command.values);
case 'setFloat':
g.setFloat(constantLocations[command.location], command.value);
case 'setFloat2':
g.setFloat2(constantLocations[command.location], command._0, command._1);
case 'setFloat3':
g.setFloat3(constantLocations[command.location], command._0, command._1, command._2);
case 'setFloat4':
g.setFloat4(constantLocations[command.location], command._0, command._1, command._2, command._3);
case 'setInt':
g.setInt(constantLocations[command.location], command.value);
case 'setBool':
g.setBool(constantLocations[command.location], command.value);
case 'viewport':
g.viewport(command.x, command.y, command.width, command.height);
case 'scissor':
g.scissor(command.x, command.y, command.width, command.height);
case 'disableScissor':
g.disableScissor();
case 'drawIndexedVertices':
g.drawIndexedVertices(command.start, command.count);
case 'drawIndexedVerticesInstanced':
g.drawIndexedVerticesInstanced(command.instanceCount, command.start, command.count);
case 'end':
g.end();
}
}
}
frames = [];
}
if (worker != null) {
worker.postMessage( { command: 'frame' } );
}
framebuffer.g2.begin();
framebuffer.g2.clear(Color.Black);
if (Image.renderTargetsInvertedY()) {
framebuffer.g2.drawScaledSubImage(renderTarget, 0, height, width, -height, 0, 0, width, height);
}
else {
framebuffer.g2.drawImage(renderTarget, 0, 0);
}
framebuffer.g2.end();
}
function keyDown(key: KeyCode): Void {
if (worker != null) {
worker.postMessage({ command: 'keyDown', key: key });
}
}
function keyUp(key: KeyCode): Void {
if (worker != null) {
worker.postMessage({ command: 'keyUp', key: key });
}
}
function keyPress(character: String): Void {
if (worker != null) {
worker.postMessage({ command: 'keyPress', character: character });
}
}
function mouseDown(button: Int, x: Int, y: Int): Void {
if (worker != null) {
worker.postMessage({ command: 'mouseDown', button: button, x: x, y: y });
}
}
function mouseUp(button: Int, x: Int, y: Int): Void {
if (worker != null) {
worker.postMessage({ command: 'mouseUp', button: button, x: x, y: y });
}
}
function mouseMove(x: Int, y: Int, mx: Int, my: Int): Void {
if (worker != null) {
worker.postMessage({ command: 'mouseMove', x: x, y: y, mx: mx, my: my });
}
}
function mouseWheel(delta: Int): Void {
if (worker != null) {
worker.postMessage({ command: 'mouseWheel', delta: delta });
}
}
function onMessage(message: Dynamic): Void {
var data = message.data;
switch (data.command) {
case 'loadBlob':
Assets.loadBlobFromPath(workerDir + data.file, function (blob: Blob) {
if (worker != null) {
worker.postMessage( { command: 'loadedBlob', id: data.id, data: blob.bytes.getData() } );
}
});
case 'loadImage':
Assets.loadImageFromPath(workerDir + data.file, false, function (image: Image) {
images.set(data.id, image);
if (worker != null) {
worker.postMessage( { command: 'loadedImage', id: data.id, width: image.width, height: image.height, realWidth: image.realWidth, realHeight: image.realHeight } );
}
});
case 'loadSound':
Assets.loadSoundFromPath(workerDir + data.file, function (sound: Sound) {
sounds.set(data.id, sound);
if (worker != null) {
worker.postMessage( { command: 'loadedSound', id: data.id, file: data.file } );
}
});
case 'uncompressSound':
sounds[data.id].uncompress(function () {
if (worker != null) {
worker.postMessage({ command: 'uncompressedSound', id: data.id });
}
});
case 'playSound':
Audio.play(sounds[data.id], data.loop);
case 'streamSound':
Audio.stream(sounds[data.id], data.loop);
case 'setShaders':
var shaders: Array<Dynamic> = data.shaders;
for (shader in shaders) {
var name: String = shader.name;
if (name.endsWith("_frag")) {
this.shaders[shader.files[0]] = new FragmentShader(shader.sources, shader.files);
}
else if (name.endsWith("_vert")) {
this.shaders[shader.files[0]] = new VertexShader(shader.sources, shader.files);
}
}
case 'compilePipeline':
var pipe = new Pipeline();
pipe.state.fragmentShader = shaders[data.frag];
pipelinesByFragmentShader[pipe.state.fragmentShader.files[0]] = pipe;
pipe.state.vertexShader = shaders[data.vert];
pipelinesByVertexShader[pipe.state.vertexShader.files[0]] = pipe;
pipe.state.inputLayout = [];
var layout: Array<Dynamic> = data.layout;
for (structure in layout) {
var newstructure = new VertexStructure();
var elements: Array<Dynamic> = structure.elements;
for (element in elements) {
var newelement = new VertexElement(element.name, element.data);
newstructure.elements.push(newelement);
}
pipe.state.inputLayout.push(newstructure);
}
var state = data.state;
pipe.state.cullMode = state.cullMode;
pipe.state.depthWrite = state.depthWrite;
pipe.state.depthMode = state.depthMode;
pipe.state.stencilFrontMode = state.stencilFrontMode;
pipe.state.stencilFrontBothPass = state.stencilFrontBothPass;
pipe.state.stencilFrontDepthFail = state.stencilFrontDepthFail;
pipe.state.stencilFrontFail = state.stencilFrontFail;
pipe.state.stencilBackMode = state.stencilBackMode;
pipe.state.stencilBackBothPass = state.stencilBackBothPass;
pipe.state.stencilBackDepthFail = state.stencilBackDepthFail;
pipe.state.stencilBackFail = state.stencilBackFail;
pipe.state.stencilReferenceValue = state.stencilReferenceValue == -1 ? StencilValue.Dynamic : StencilValue.Static(state.stencilReferenceValue);
pipe.state.stencilReadMask = state.stencilReadMask;
pipe.state.stencilWriteMask = state.stencilWriteMask;
pipe.state.blendSource = state.blendSource;
pipe.state.blendDestination = state.blendDestination;
pipe.state.alphaBlendSource = state.alphaBlendSource;
pipe.state.alphaBlendDestination = state.alphaBlendDestination;
pipe.state.colorWriteMaskRed = state.colorWriteMaskRed;
pipe.state.colorWriteMaskGreen = state.colorWriteMaskGreen;
pipe.state.colorWriteMaskBlue = state.colorWriteMaskBlue;
pipe.state.colorWriteMaskAlpha = state.colorWriteMaskAlpha;
pipe.state.conservativeRasterization = state.conservativeRasterization;
pipe.state.compile();
pipelines[data.id] = pipe;
case 'createIndexBuffer':
indexBuffers[data.id] = new IndexBuffer(data.size, data.usage);
case 'createVertexBuffer':
var structure = new VertexStructure();
var elements: Array<Dynamic> = data.structure.elements;
for (element in elements) {
var newelement = new VertexElement(element.name, element.data);
structure.elements.push(newelement);
}
vertexBuffers[data.id] = new VertexBuffer(data.size, structure, data.usage);
case 'createImage':
images[data.id] = Image.create(data.width, data.height, data.format, data.usage);
case 'createRenderTarget':
renderTargets[data.id] = Image.createRenderTarget(data.width, data.height);
case 'begin', 'clear', 'end', 'setPipeline', 'updateIndexBuffer', 'updateVertexBuffer', 'setIndexBuffer', 'setVertexBuffer', 'drawIndexedVertices',
'createConstantLocation', 'createTextureUnit', 'setTexture', 'unlockImage', 'setTextureParameters',
'setMatrix3', 'setMatrix4', 'setVector2', 'setVector3', 'setVector4', 'setFloats', 'setFloat', 'setFloat2', 'setFloat3', 'setFloat4', 'setInt', 'setBool',
'viewport', 'scissor', 'disableScissor', 'setVertexBuffers', 'drawIndexedVerticesInstanced':
currentFrame.commands.push(data);
case 'beginFrame':
case 'endFrame':
frames.push(currentFrame);
currentFrame = new Frame();
}
}
}