@predy-js/render-interface 0.1.10 → 0.1.12

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/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * Name: @predy-js/render-interface
3
3
  * Description: undefined
4
4
  * Author: undefined
5
- * Version: v0.1.10
5
+ * Version: v0.1.12
6
6
  */
7
7
 
8
8
  'use strict';
@@ -648,6 +648,7 @@ var GPUCapabilityEmpty = /** @class */ (function () {
648
648
  function GPUCapabilityEmpty() {
649
649
  this.level = 0;
650
650
  this.capability = {
651
+ maxSample: 0,
651
652
  floatTexture: 0,
652
653
  halfFloatTexture: 0,
653
654
  maxVertexUniforms: 0,
@@ -2907,7 +2908,10 @@ var GLGPURenderer = /** @class */ (function () {
2907
2908
  };
2908
2909
  GLGPURenderer.prototype.deleteGLFrameBuffer = function (frameBuffer) {
2909
2910
  if (frameBuffer && !this._isDestroyed) {
2910
- this.gl.deleteFramebuffer(frameBuffer.fbo);
2911
+ var gl = this.gl;
2912
+ gl.deleteFramebuffer(frameBuffer.fbo);
2913
+ gl.deleteFramebuffer(frameBuffer.multisampleFbo);
2914
+ frameBuffer.multisampleRbs.forEach(function (rb) { return rb.destroy(); });
2911
2915
  arrRemove(this._frameBuffers, frameBuffer);
2912
2916
  delete frameBuffer.fbo;
2913
2917
  }
@@ -3040,13 +3044,14 @@ var CanvasEmpty = /** @class */ (function () {
3040
3044
 
3041
3045
  var GLRenderBuffer = /** @class */ (function () {
3042
3046
  function GLRenderBuffer(renderer, options) {
3047
+ var _a;
3043
3048
  this.storageType = options.storageType;
3044
3049
  this.renderer = renderer;
3045
3050
  this.glHandle = renderer.createGLRenderBuffer(this);
3046
3051
  this.internalFormat = this.format = options.format;
3047
3052
  this.attachment = options.attachment;
3048
3053
  this.size = [0, 0];
3049
- this.multiSample = 1;
3054
+ this.multiSample = (_a = options.multisample) !== null && _a !== void 0 ? _a : 0;
3050
3055
  }
3051
3056
  GLRenderBuffer.prototype.setSize = function (width, height) {
3052
3057
  if (width !== this.size[0] || height !== this.size[1]) {
@@ -3056,6 +3061,10 @@ var GLRenderBuffer = /** @class */ (function () {
3056
3061
  state.bindRenderBuffer(gl.RENDERBUFFER, this.glHandle);
3057
3062
  if (width && height) {
3058
3063
  gl.renderbufferStorage(gl.RENDERBUFFER, internalFormat, this.size[0] = width, this.size[1] = height);
3064
+ if (this.multiSample > 1) {
3065
+ var if2 = internalFormat === gl.DEPTH_STENCIL ? gl.DEPTH24_STENCIL8 : internalFormat;
3066
+ gl.renderbufferStorageMultisample(gl.RENDERBUFFER, this.multiSample, if2, width, height);
3067
+ }
3059
3068
  }
3060
3069
  else {
3061
3070
  consoleError("invalid render buffer size ".concat(width, "x").concat(height));
@@ -3077,6 +3086,7 @@ var seed$5 = 1;
3077
3086
  var GLFrameBuffer = /** @class */ (function () {
3078
3087
  function GLFrameBuffer(options, renderer) {
3079
3088
  var _a;
3089
+ this.multisampleRbs = [];
3080
3090
  this.renderer = renderer;
3081
3091
  this.depthStencilStorageType = ((_a = options.depthStencilAttachment) === null || _a === void 0 ? void 0 : _a.storageType) || exports.RenderPassAttachmentStorageType.none;
3082
3092
  this.viewport = options.viewport;
@@ -3085,6 +3095,7 @@ var GLFrameBuffer = /** @class */ (function () {
3085
3095
  this.name = options.name || ('GLFrameBuffer' + seed$5++);
3086
3096
  this.storeAction = options.storeAction;
3087
3097
  this._attachmentHandles = [];
3098
+ this.multisample = renderer.level > 1 ? options.multisample || 0 : 0;
3088
3099
  this.checkOptions(options);
3089
3100
  }
3090
3101
  Object.defineProperty(GLFrameBuffer.prototype, "stencilStorage", {
@@ -3123,6 +3134,7 @@ var GLFrameBuffer = /** @class */ (function () {
3123
3134
  var capability = renderer.gpu.capability;
3124
3135
  var depthStencilAttachment = options.depthStencilAttachment || { storageType: exports.RenderPassAttachmentStorageType.none };
3125
3136
  var willUseFbo = options.attachments.length > 0;
3137
+ var willMultisample = this.multisample > 1;
3126
3138
  this.externalStorage = false;
3127
3139
  var separateDepthStencil = true;
3128
3140
  if (options.attachments.length > 1 && !capability.drawBuffers) {
@@ -3137,10 +3149,16 @@ var GLFrameBuffer = /** @class */ (function () {
3137
3149
  if (willUseFbo) {
3138
3150
  this.fbo = renderer.createGLFrameBuffer(this, this.name);
3139
3151
  }
3152
+ if (willMultisample) {
3153
+ this.multisampleFbo = renderer.createGLFrameBuffer(this, this.name + '_multisample');
3154
+ }
3140
3155
  var storageType = depthStencilAttachment.storageType;
3141
3156
  if (storageType === exports.RenderPassAttachmentStorageType.depth_stencil_opaque) {
3142
3157
  if (depthStencilAttachment.storage) {
3143
3158
  if (depthStencilAttachment.storage instanceof GLRenderBuffer) {
3159
+ if (this.multisample !== depthStencilAttachment.storage.multiSample && this.multisample > 1) {
3160
+ throw Error('fbo multisample not eql depth stencil attachment');
3161
+ }
3144
3162
  this.depthStencilRenderBuffer = depthStencilAttachment.storage;
3145
3163
  this.externalStorage = true;
3146
3164
  }
@@ -3152,6 +3170,7 @@ var GLFrameBuffer = /** @class */ (function () {
3152
3170
  this.depthStencilRenderBuffer = new GLRenderBuffer(renderer, {
3153
3171
  format: constants.DEPTH_STENCIL,
3154
3172
  attachment: constants.DEPTH_STENCIL_ATTACHMENT,
3173
+ multisample: this.multisample,
3155
3174
  storageType: storageType,
3156
3175
  });
3157
3176
  }
@@ -3159,6 +3178,9 @@ var GLFrameBuffer = /** @class */ (function () {
3159
3178
  }
3160
3179
  else if (storageType === exports.RenderPassAttachmentStorageType.depth_16_opaque) {
3161
3180
  if (depthStencilAttachment.storage) {
3181
+ if (this.multisample !== depthStencilAttachment.storage.multiSample && this.multisample > 1) {
3182
+ throw Error('fbo multisample not eql depth attachment');
3183
+ }
3162
3184
  if (depthStencilAttachment.storage instanceof GLRenderBuffer) {
3163
3185
  this.depthStencilRenderBuffer = depthStencilAttachment.storage;
3164
3186
  this.externalStorage = true;
@@ -3170,6 +3192,7 @@ var GLFrameBuffer = /** @class */ (function () {
3170
3192
  else {
3171
3193
  this.depthStencilRenderBuffer = new GLRenderBuffer(renderer, {
3172
3194
  attachment: constants.DEPTH_ATTACHMENT,
3195
+ multisample: this.multisample,
3173
3196
  format: constants.DEPTH_COMPONENT16,
3174
3197
  storageType: storageType,
3175
3198
  });
@@ -3178,6 +3201,9 @@ var GLFrameBuffer = /** @class */ (function () {
3178
3201
  else if (storageType === exports.RenderPassAttachmentStorageType.stencil_8_opaque) {
3179
3202
  if (depthStencilAttachment.storage) {
3180
3203
  if (depthStencilAttachment.storage instanceof GLRenderBuffer) {
3204
+ if (this.multisample !== depthStencilAttachment.storage.multiSample && this.multisample > 1) {
3205
+ throw Error('fbo multisample not eql stencil attachment');
3206
+ }
3181
3207
  this.depthStencilRenderBuffer = depthStencilAttachment.storage;
3182
3208
  this.externalStorage = true;
3183
3209
  }
@@ -3189,6 +3215,7 @@ var GLFrameBuffer = /** @class */ (function () {
3189
3215
  this.depthStencilRenderBuffer = new GLRenderBuffer(renderer, {
3190
3216
  attachment: constants.STENCIL_ATTACHMENT,
3191
3217
  format: constants.STENCIL_INDEX8,
3218
+ multisample: this.multisample,
3192
3219
  storageType: storageType,
3193
3220
  });
3194
3221
  }
@@ -3242,6 +3269,7 @@ var GLFrameBuffer = /** @class */ (function () {
3242
3269
  };
3243
3270
  GLFrameBuffer.prototype.unbind = function () {
3244
3271
  var att = this.storeInvalidAttachments;
3272
+ this.resolveMultiSample();
3245
3273
  if (att && att.length) {
3246
3274
  var gl = this.renderer.gl;
3247
3275
  gl.invalidateFramebuffer(gl.FRAMEBUFFER, att);
@@ -3252,9 +3280,14 @@ var GLFrameBuffer = /** @class */ (function () {
3252
3280
  GLFrameBuffer.prototype.bind = function () {
3253
3281
  var gl = this.renderer.gl;
3254
3282
  var state = this.renderer.state;
3255
- if (this.fbo) {
3256
- var FRAMEBUFFER = gl.FRAMEBUFFER;
3257
- var viewport = this.viewport;
3283
+ var FRAMEBUFFER = gl.FRAMEBUFFER;
3284
+ var viewport = this.viewport;
3285
+ var multisampleFbo = this.multisampleFbo;
3286
+ if (multisampleFbo) {
3287
+ state.bindFramebuffer(FRAMEBUFFER, multisampleFbo);
3288
+ state.viewport(viewport[0], viewport[1], viewport[2], viewport[3]);
3289
+ }
3290
+ else if (this.fbo) {
3258
3291
  state.bindFramebuffer(FRAMEBUFFER, this.fbo);
3259
3292
  state.viewport(viewport[0], viewport[1], viewport[2], viewport[3]);
3260
3293
  var r_1 = this.renderer.emptyTexture2D.glHandle;
@@ -3266,30 +3299,74 @@ var GLFrameBuffer = /** @class */ (function () {
3266
3299
  state.bindTexture(constants.TEXTURE_2D, r_1, true);
3267
3300
  }
3268
3301
  });
3269
- if (!this.ready) {
3270
- var _a = this, depthStencilRenderBuffer = _a.depthStencilRenderBuffer, depthTexture = _a.depthTexture, stencilTexture = _a.stencilTexture;
3271
- state.activeTexture(gl.TEXTURE0);
3272
- if (depthStencilRenderBuffer) {
3273
- depthStencilRenderBuffer.setSize(viewport[2], viewport[3]);
3274
- gl.framebufferRenderbuffer(FRAMEBUFFER, depthStencilRenderBuffer.attachment, gl.RENDERBUFFER, depthStencilRenderBuffer.glHandle);
3275
- }
3276
- else if (depthTexture) {
3277
- depthTexture.update({ data: { width: viewport[2], height: viewport[3], data: new Uint16Array(0) } });
3278
- var attachment = depthTexture && stencilTexture ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
3279
- gl.framebufferTexture2D(FRAMEBUFFER, attachment, gl.TEXTURE_2D, depthTexture.glHandle, 0);
3302
+ }
3303
+ if (!this.ready) {
3304
+ var _a = this, depthStencilRenderBuffer = _a.depthStencilRenderBuffer, depthTexture = _a.depthTexture, stencilTexture = _a.stencilTexture;
3305
+ state.activeTexture(gl.TEXTURE0);
3306
+ if (depthStencilRenderBuffer) {
3307
+ depthStencilRenderBuffer.setSize(viewport[2], viewport[3]);
3308
+ gl.framebufferRenderbuffer(FRAMEBUFFER, depthStencilRenderBuffer.attachment, gl.RENDERBUFFER, depthStencilRenderBuffer.glHandle);
3309
+ }
3310
+ else if (depthTexture) {
3311
+ if (multisampleFbo) {
3312
+ var attachment_1 = gl.DEPTH_ATTACHMENT;
3313
+ var format = gl.DEPTH_COMPONENT16;
3314
+ if (this.depthStencilStorageType === exports.RenderPassAttachmentStorageType.depth_24_stencil_8_texture) {
3315
+ attachment_1 = gl.DEPTH_STENCIL_ATTACHMENT;
3316
+ format = gl.DEPTH24_STENCIL8;
3317
+ }
3318
+ var renderBuffer = new GLRenderBuffer(this.renderer, {
3319
+ storageType: this.depthStencilStorageType,
3320
+ attachment: attachment_1,
3321
+ format: format,
3322
+ multisample: this.multisample,
3323
+ });
3324
+ gl.bindFramebuffer(gl.FRAMEBUFFER, multisampleFbo);
3325
+ this.multisampleRbs.push(renderBuffer);
3326
+ renderBuffer.setSize(viewport[2], viewport[3]);
3327
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment_1, gl.RENDERBUFFER, renderBuffer.glHandle);
3328
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this.fbo);
3280
3329
  }
3281
- this.resetColorTextures(this.colorTextures);
3282
- var status_1 = gl.checkFramebufferStatus(FRAMEBUFFER);
3283
- if (status_1 !== gl.FRAMEBUFFER_COMPLETE) {
3284
- var error = gl.getError();
3285
- throw Error("framebuffer failed status ".concat(status_1, " error:").concat(error));
3330
+ depthTexture.update({ data: { width: viewport[2], height: viewport[3], data: new Uint16Array(0) } });
3331
+ var attachment = depthTexture && stencilTexture ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
3332
+ gl.framebufferTexture2D(FRAMEBUFFER, attachment, gl.TEXTURE_2D, depthTexture.glHandle, 0);
3333
+ }
3334
+ this.resetColorTextures(this.colorTextures);
3335
+ [multisampleFbo, this.fbo].forEach(function (fbo) {
3336
+ if (fbo) {
3337
+ gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
3338
+ var status_1 = gl.checkFramebufferStatus(FRAMEBUFFER);
3339
+ if (status_1 !== gl.FRAMEBUFFER_COMPLETE) {
3340
+ var error = gl.getError();
3341
+ throw Error("framebuffer failed status ".concat(status_1, " error:").concat(error));
3342
+ }
3286
3343
  }
3287
- //@ts-expect-error safe to assign
3288
- this.ready = true;
3344
+ });
3345
+ //@ts-expect-error safe to assign
3346
+ this.ready = true;
3347
+ }
3348
+ };
3349
+ GLFrameBuffer.prototype.resolveMultiSample = function () {
3350
+ var multisampleFbo = this.multisampleFbo;
3351
+ var fbo = this.fbo;
3352
+ if (multisampleFbo && fbo) {
3353
+ var gl = this.renderer.gl;
3354
+ gl.bindFramebuffer(gl.READ_FRAMEBUFFER, multisampleFbo);
3355
+ gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo);
3356
+ var vp = this.viewport;
3357
+ var bit = gl.COLOR_BUFFER_BIT;
3358
+ if (this.depthTexture) {
3359
+ bit = bit | gl.DEPTH_BUFFER_BIT;
3289
3360
  }
3361
+ gl.blitFramebuffer(0, 0, vp[2], vp[3], // Source region
3362
+ 0, 0, vp[2], vp[3], // Destination region
3363
+ bit, gl.LINEAR // Buffer mask and filter
3364
+ );
3365
+ gl.bindFramebuffer(gl.FRAMEBUFFER, multisampleFbo);
3290
3366
  }
3291
3367
  };
3292
3368
  GLFrameBuffer.prototype.resetColorTextures = function (colors) {
3369
+ var _this = this;
3293
3370
  var gl = this.renderer.gl;
3294
3371
  var gpu = this.renderer.gpu;
3295
3372
  var viewport = this.viewport;
@@ -3299,8 +3376,22 @@ var GLFrameBuffer = /** @class */ (function () {
3299
3376
  this.colorTextures = colors.slice();
3300
3377
  }
3301
3378
  this.renderer.state.activeTexture(gl.TEXTURE0);
3379
+ var multisampleFBO = this.multisampleFbo;
3302
3380
  this.colorTextures.forEach(function (tex, index) {
3303
3381
  tex.update({ data: data });
3382
+ if (multisampleFBO) {
3383
+ gl.bindFramebuffer(gl.FRAMEBUFFER, multisampleFBO);
3384
+ var renderBuffer = new GLRenderBuffer(_this.renderer, {
3385
+ format: gl.RGBA8,
3386
+ attachment: gl.COLOR_ATTACHMENT0 + index,
3387
+ storageType: exports.RenderPassAttachmentStorageType.color,
3388
+ multisample: _this.multisample,
3389
+ });
3390
+ _this.multisampleRbs.push(renderBuffer);
3391
+ renderBuffer.setSize(tex.width, tex.height);
3392
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + index, gl.RENDERBUFFER, renderBuffer.glHandle);
3393
+ }
3394
+ gl.bindFramebuffer(gl.FRAMEBUFFER, _this.fbo);
3304
3395
  gpu.framebufferTexture2D(gl, gl.FRAMEBUFFER, index, gl.TEXTURE_2D, tex.glHandle);
3305
3396
  buffers.push(true);
3306
3397
  });
@@ -3319,7 +3410,7 @@ var GLFrameBuffer = /** @class */ (function () {
3319
3410
  (_b = this.depthTexture) === null || _b === void 0 ? void 0 : _b.destroy();
3320
3411
  }
3321
3412
  //@ts-expect-error safe to assign
3322
- this.renderer = this.stencilRenderBuffer = this.depthStencilRenderBuffer = null;
3413
+ this.renderer = this.depthStencilRenderBuffer = null;
3323
3414
  }
3324
3415
  };
3325
3416
  GLFrameBuffer.prototype.resize = function (x, y, width, height) {
@@ -3918,6 +4009,7 @@ var MarsRenderPassColorAttachment = /** @class */ (function () {
3918
4009
  }
3919
4010
  this._isDestroyed = false;
3920
4011
  this.readable = true;
4012
+ this.multisample = options.multiSample || 1;
3921
4013
  }
3922
4014
  Object.defineProperty(MarsRenderPassColorAttachment.prototype, "isDestroyed", {
3923
4015
  get: function () {
@@ -3965,6 +4057,10 @@ var MarsRenderPassColorAttachment = /** @class */ (function () {
3965
4057
  if (this.texture) {
3966
4058
  this.texture.assignRenderer(renderer);
3967
4059
  }
4060
+ if (this.multisample > 1) {
4061
+ this.multisample = Math.min(renderer.gpu.capability.maxSample, this.multisample);
4062
+ }
4063
+ this.texture.internal.multisample = this.multisample;
3968
4064
  return this;
3969
4065
  };
3970
4066
  MarsRenderPassColorAttachment.prototype.destroy = function () {
@@ -4011,6 +4107,14 @@ var MarsRenderPass = /** @class */ (function () {
4011
4107
  enumerable: false,
4012
4108
  configurable: true
4013
4109
  });
4110
+ Object.defineProperty(MarsRenderPass.prototype, "multisample", {
4111
+ get: function () {
4112
+ var _a;
4113
+ return ((_a = this.frameBuffer) === null || _a === void 0 ? void 0 : _a.multisample) || 0;
4114
+ },
4115
+ enumerable: false,
4116
+ configurable: true
4117
+ });
4014
4118
  Object.defineProperty(MarsRenderPass.prototype, "attachments", {
4015
4119
  get: function () {
4016
4120
  return this._attachments;
@@ -4175,6 +4279,7 @@ var MarsRenderPass = /** @class */ (function () {
4175
4279
  storeAction: this.storeAction,
4176
4280
  name: name,
4177
4281
  viewport: viewport,
4282
+ multisample: options.multiSample,
4178
4283
  viewportScale: this.viewportScale,
4179
4284
  isCustomViewport: this._isCustomViewport,
4180
4285
  attachments: attachments.map(function (att) { return (att.texture).internal; }),
@@ -5607,7 +5712,7 @@ var MarsSharedGeometry = /** @class */ (function (_super) {
5607
5712
  return MarsSharedGeometry;
5608
5713
  }(MarsGeometry));
5609
5714
 
5610
- consoleLog('version: ' + "0.1.10");
5715
+ consoleLog('version: ' + "0.1.12");
5611
5716
 
5612
5717
  exports.Geometry = MarsGeometry;
5613
5718
  exports.InstancedMesh = MarsInstancedMesh;