@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.mjs 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
  /******************************************************************************
@@ -644,6 +644,7 @@ var GPUCapabilityEmpty = /** @class */ (function () {
644
644
  function GPUCapabilityEmpty() {
645
645
  this.level = 0;
646
646
  this.capability = {
647
+ maxSample: 0,
647
648
  floatTexture: 0,
648
649
  halfFloatTexture: 0,
649
650
  maxVertexUniforms: 0,
@@ -2903,7 +2904,10 @@ var GLGPURenderer = /** @class */ (function () {
2903
2904
  };
2904
2905
  GLGPURenderer.prototype.deleteGLFrameBuffer = function (frameBuffer) {
2905
2906
  if (frameBuffer && !this._isDestroyed) {
2906
- this.gl.deleteFramebuffer(frameBuffer.fbo);
2907
+ var gl = this.gl;
2908
+ gl.deleteFramebuffer(frameBuffer.fbo);
2909
+ gl.deleteFramebuffer(frameBuffer.multisampleFbo);
2910
+ frameBuffer.multisampleRbs.forEach(function (rb) { return rb.destroy(); });
2907
2911
  arrRemove(this._frameBuffers, frameBuffer);
2908
2912
  delete frameBuffer.fbo;
2909
2913
  }
@@ -3036,13 +3040,14 @@ var CanvasEmpty = /** @class */ (function () {
3036
3040
 
3037
3041
  var GLRenderBuffer = /** @class */ (function () {
3038
3042
  function GLRenderBuffer(renderer, options) {
3043
+ var _a;
3039
3044
  this.storageType = options.storageType;
3040
3045
  this.renderer = renderer;
3041
3046
  this.glHandle = renderer.createGLRenderBuffer(this);
3042
3047
  this.internalFormat = this.format = options.format;
3043
3048
  this.attachment = options.attachment;
3044
3049
  this.size = [0, 0];
3045
- this.multiSample = 1;
3050
+ this.multiSample = (_a = options.multisample) !== null && _a !== void 0 ? _a : 0;
3046
3051
  }
3047
3052
  GLRenderBuffer.prototype.setSize = function (width, height) {
3048
3053
  if (width !== this.size[0] || height !== this.size[1]) {
@@ -3052,6 +3057,10 @@ var GLRenderBuffer = /** @class */ (function () {
3052
3057
  state.bindRenderBuffer(gl.RENDERBUFFER, this.glHandle);
3053
3058
  if (width && height) {
3054
3059
  gl.renderbufferStorage(gl.RENDERBUFFER, internalFormat, this.size[0] = width, this.size[1] = height);
3060
+ if (this.multiSample > 1) {
3061
+ var if2 = internalFormat === gl.DEPTH_STENCIL ? gl.DEPTH24_STENCIL8 : internalFormat;
3062
+ gl.renderbufferStorageMultisample(gl.RENDERBUFFER, this.multiSample, if2, width, height);
3063
+ }
3055
3064
  }
3056
3065
  else {
3057
3066
  consoleError("invalid render buffer size ".concat(width, "x").concat(height));
@@ -3073,6 +3082,7 @@ var seed$5 = 1;
3073
3082
  var GLFrameBuffer = /** @class */ (function () {
3074
3083
  function GLFrameBuffer(options, renderer) {
3075
3084
  var _a;
3085
+ this.multisampleRbs = [];
3076
3086
  this.renderer = renderer;
3077
3087
  this.depthStencilStorageType = ((_a = options.depthStencilAttachment) === null || _a === void 0 ? void 0 : _a.storageType) || RenderPassAttachmentStorageType.none;
3078
3088
  this.viewport = options.viewport;
@@ -3081,6 +3091,7 @@ var GLFrameBuffer = /** @class */ (function () {
3081
3091
  this.name = options.name || ('GLFrameBuffer' + seed$5++);
3082
3092
  this.storeAction = options.storeAction;
3083
3093
  this._attachmentHandles = [];
3094
+ this.multisample = renderer.level > 1 ? options.multisample || 0 : 0;
3084
3095
  this.checkOptions(options);
3085
3096
  }
3086
3097
  Object.defineProperty(GLFrameBuffer.prototype, "stencilStorage", {
@@ -3119,6 +3130,7 @@ var GLFrameBuffer = /** @class */ (function () {
3119
3130
  var capability = renderer.gpu.capability;
3120
3131
  var depthStencilAttachment = options.depthStencilAttachment || { storageType: RenderPassAttachmentStorageType.none };
3121
3132
  var willUseFbo = options.attachments.length > 0;
3133
+ var willMultisample = this.multisample > 1;
3122
3134
  this.externalStorage = false;
3123
3135
  var separateDepthStencil = true;
3124
3136
  if (options.attachments.length > 1 && !capability.drawBuffers) {
@@ -3133,10 +3145,16 @@ var GLFrameBuffer = /** @class */ (function () {
3133
3145
  if (willUseFbo) {
3134
3146
  this.fbo = renderer.createGLFrameBuffer(this, this.name);
3135
3147
  }
3148
+ if (willMultisample) {
3149
+ this.multisampleFbo = renderer.createGLFrameBuffer(this, this.name + '_multisample');
3150
+ }
3136
3151
  var storageType = depthStencilAttachment.storageType;
3137
3152
  if (storageType === RenderPassAttachmentStorageType.depth_stencil_opaque) {
3138
3153
  if (depthStencilAttachment.storage) {
3139
3154
  if (depthStencilAttachment.storage instanceof GLRenderBuffer) {
3155
+ if (this.multisample !== depthStencilAttachment.storage.multiSample && this.multisample > 1) {
3156
+ throw Error('fbo multisample not eql depth stencil attachment');
3157
+ }
3140
3158
  this.depthStencilRenderBuffer = depthStencilAttachment.storage;
3141
3159
  this.externalStorage = true;
3142
3160
  }
@@ -3148,6 +3166,7 @@ var GLFrameBuffer = /** @class */ (function () {
3148
3166
  this.depthStencilRenderBuffer = new GLRenderBuffer(renderer, {
3149
3167
  format: constants.DEPTH_STENCIL,
3150
3168
  attachment: constants.DEPTH_STENCIL_ATTACHMENT,
3169
+ multisample: this.multisample,
3151
3170
  storageType: storageType,
3152
3171
  });
3153
3172
  }
@@ -3155,6 +3174,9 @@ var GLFrameBuffer = /** @class */ (function () {
3155
3174
  }
3156
3175
  else if (storageType === RenderPassAttachmentStorageType.depth_16_opaque) {
3157
3176
  if (depthStencilAttachment.storage) {
3177
+ if (this.multisample !== depthStencilAttachment.storage.multiSample && this.multisample > 1) {
3178
+ throw Error('fbo multisample not eql depth attachment');
3179
+ }
3158
3180
  if (depthStencilAttachment.storage instanceof GLRenderBuffer) {
3159
3181
  this.depthStencilRenderBuffer = depthStencilAttachment.storage;
3160
3182
  this.externalStorage = true;
@@ -3166,6 +3188,7 @@ var GLFrameBuffer = /** @class */ (function () {
3166
3188
  else {
3167
3189
  this.depthStencilRenderBuffer = new GLRenderBuffer(renderer, {
3168
3190
  attachment: constants.DEPTH_ATTACHMENT,
3191
+ multisample: this.multisample,
3169
3192
  format: constants.DEPTH_COMPONENT16,
3170
3193
  storageType: storageType,
3171
3194
  });
@@ -3174,6 +3197,9 @@ var GLFrameBuffer = /** @class */ (function () {
3174
3197
  else if (storageType === RenderPassAttachmentStorageType.stencil_8_opaque) {
3175
3198
  if (depthStencilAttachment.storage) {
3176
3199
  if (depthStencilAttachment.storage instanceof GLRenderBuffer) {
3200
+ if (this.multisample !== depthStencilAttachment.storage.multiSample && this.multisample > 1) {
3201
+ throw Error('fbo multisample not eql stencil attachment');
3202
+ }
3177
3203
  this.depthStencilRenderBuffer = depthStencilAttachment.storage;
3178
3204
  this.externalStorage = true;
3179
3205
  }
@@ -3185,6 +3211,7 @@ var GLFrameBuffer = /** @class */ (function () {
3185
3211
  this.depthStencilRenderBuffer = new GLRenderBuffer(renderer, {
3186
3212
  attachment: constants.STENCIL_ATTACHMENT,
3187
3213
  format: constants.STENCIL_INDEX8,
3214
+ multisample: this.multisample,
3188
3215
  storageType: storageType,
3189
3216
  });
3190
3217
  }
@@ -3238,6 +3265,7 @@ var GLFrameBuffer = /** @class */ (function () {
3238
3265
  };
3239
3266
  GLFrameBuffer.prototype.unbind = function () {
3240
3267
  var att = this.storeInvalidAttachments;
3268
+ this.resolveMultiSample();
3241
3269
  if (att && att.length) {
3242
3270
  var gl = this.renderer.gl;
3243
3271
  gl.invalidateFramebuffer(gl.FRAMEBUFFER, att);
@@ -3248,9 +3276,14 @@ var GLFrameBuffer = /** @class */ (function () {
3248
3276
  GLFrameBuffer.prototype.bind = function () {
3249
3277
  var gl = this.renderer.gl;
3250
3278
  var state = this.renderer.state;
3251
- if (this.fbo) {
3252
- var FRAMEBUFFER = gl.FRAMEBUFFER;
3253
- var viewport = this.viewport;
3279
+ var FRAMEBUFFER = gl.FRAMEBUFFER;
3280
+ var viewport = this.viewport;
3281
+ var multisampleFbo = this.multisampleFbo;
3282
+ if (multisampleFbo) {
3283
+ state.bindFramebuffer(FRAMEBUFFER, multisampleFbo);
3284
+ state.viewport(viewport[0], viewport[1], viewport[2], viewport[3]);
3285
+ }
3286
+ else if (this.fbo) {
3254
3287
  state.bindFramebuffer(FRAMEBUFFER, this.fbo);
3255
3288
  state.viewport(viewport[0], viewport[1], viewport[2], viewport[3]);
3256
3289
  var r_1 = this.renderer.emptyTexture2D.glHandle;
@@ -3262,30 +3295,74 @@ var GLFrameBuffer = /** @class */ (function () {
3262
3295
  state.bindTexture(constants.TEXTURE_2D, r_1, true);
3263
3296
  }
3264
3297
  });
3265
- if (!this.ready) {
3266
- var _a = this, depthStencilRenderBuffer = _a.depthStencilRenderBuffer, depthTexture = _a.depthTexture, stencilTexture = _a.stencilTexture;
3267
- state.activeTexture(gl.TEXTURE0);
3268
- if (depthStencilRenderBuffer) {
3269
- depthStencilRenderBuffer.setSize(viewport[2], viewport[3]);
3270
- gl.framebufferRenderbuffer(FRAMEBUFFER, depthStencilRenderBuffer.attachment, gl.RENDERBUFFER, depthStencilRenderBuffer.glHandle);
3271
- }
3272
- else if (depthTexture) {
3273
- depthTexture.update({ data: { width: viewport[2], height: viewport[3], data: new Uint16Array(0) } });
3274
- var attachment = depthTexture && stencilTexture ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
3275
- gl.framebufferTexture2D(FRAMEBUFFER, attachment, gl.TEXTURE_2D, depthTexture.glHandle, 0);
3298
+ }
3299
+ if (!this.ready) {
3300
+ var _a = this, depthStencilRenderBuffer = _a.depthStencilRenderBuffer, depthTexture = _a.depthTexture, stencilTexture = _a.stencilTexture;
3301
+ state.activeTexture(gl.TEXTURE0);
3302
+ if (depthStencilRenderBuffer) {
3303
+ depthStencilRenderBuffer.setSize(viewport[2], viewport[3]);
3304
+ gl.framebufferRenderbuffer(FRAMEBUFFER, depthStencilRenderBuffer.attachment, gl.RENDERBUFFER, depthStencilRenderBuffer.glHandle);
3305
+ }
3306
+ else if (depthTexture) {
3307
+ if (multisampleFbo) {
3308
+ var attachment_1 = gl.DEPTH_ATTACHMENT;
3309
+ var format = gl.DEPTH_COMPONENT16;
3310
+ if (this.depthStencilStorageType === RenderPassAttachmentStorageType.depth_24_stencil_8_texture) {
3311
+ attachment_1 = gl.DEPTH_STENCIL_ATTACHMENT;
3312
+ format = gl.DEPTH24_STENCIL8;
3313
+ }
3314
+ var renderBuffer = new GLRenderBuffer(this.renderer, {
3315
+ storageType: this.depthStencilStorageType,
3316
+ attachment: attachment_1,
3317
+ format: format,
3318
+ multisample: this.multisample,
3319
+ });
3320
+ gl.bindFramebuffer(gl.FRAMEBUFFER, multisampleFbo);
3321
+ this.multisampleRbs.push(renderBuffer);
3322
+ renderBuffer.setSize(viewport[2], viewport[3]);
3323
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment_1, gl.RENDERBUFFER, renderBuffer.glHandle);
3324
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this.fbo);
3276
3325
  }
3277
- this.resetColorTextures(this.colorTextures);
3278
- var status_1 = gl.checkFramebufferStatus(FRAMEBUFFER);
3279
- if (status_1 !== gl.FRAMEBUFFER_COMPLETE) {
3280
- var error = gl.getError();
3281
- throw Error("framebuffer failed status ".concat(status_1, " error:").concat(error));
3326
+ depthTexture.update({ data: { width: viewport[2], height: viewport[3], data: new Uint16Array(0) } });
3327
+ var attachment = depthTexture && stencilTexture ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
3328
+ gl.framebufferTexture2D(FRAMEBUFFER, attachment, gl.TEXTURE_2D, depthTexture.glHandle, 0);
3329
+ }
3330
+ this.resetColorTextures(this.colorTextures);
3331
+ [multisampleFbo, this.fbo].forEach(function (fbo) {
3332
+ if (fbo) {
3333
+ gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
3334
+ var status_1 = gl.checkFramebufferStatus(FRAMEBUFFER);
3335
+ if (status_1 !== gl.FRAMEBUFFER_COMPLETE) {
3336
+ var error = gl.getError();
3337
+ throw Error("framebuffer failed status ".concat(status_1, " error:").concat(error));
3338
+ }
3282
3339
  }
3283
- //@ts-expect-error safe to assign
3284
- this.ready = true;
3340
+ });
3341
+ //@ts-expect-error safe to assign
3342
+ this.ready = true;
3343
+ }
3344
+ };
3345
+ GLFrameBuffer.prototype.resolveMultiSample = function () {
3346
+ var multisampleFbo = this.multisampleFbo;
3347
+ var fbo = this.fbo;
3348
+ if (multisampleFbo && fbo) {
3349
+ var gl = this.renderer.gl;
3350
+ gl.bindFramebuffer(gl.READ_FRAMEBUFFER, multisampleFbo);
3351
+ gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo);
3352
+ var vp = this.viewport;
3353
+ var bit = gl.COLOR_BUFFER_BIT;
3354
+ if (this.depthTexture) {
3355
+ bit = bit | gl.DEPTH_BUFFER_BIT;
3285
3356
  }
3357
+ gl.blitFramebuffer(0, 0, vp[2], vp[3], // Source region
3358
+ 0, 0, vp[2], vp[3], // Destination region
3359
+ bit, gl.LINEAR // Buffer mask and filter
3360
+ );
3361
+ gl.bindFramebuffer(gl.FRAMEBUFFER, multisampleFbo);
3286
3362
  }
3287
3363
  };
3288
3364
  GLFrameBuffer.prototype.resetColorTextures = function (colors) {
3365
+ var _this = this;
3289
3366
  var gl = this.renderer.gl;
3290
3367
  var gpu = this.renderer.gpu;
3291
3368
  var viewport = this.viewport;
@@ -3295,8 +3372,22 @@ var GLFrameBuffer = /** @class */ (function () {
3295
3372
  this.colorTextures = colors.slice();
3296
3373
  }
3297
3374
  this.renderer.state.activeTexture(gl.TEXTURE0);
3375
+ var multisampleFBO = this.multisampleFbo;
3298
3376
  this.colorTextures.forEach(function (tex, index) {
3299
3377
  tex.update({ data: data });
3378
+ if (multisampleFBO) {
3379
+ gl.bindFramebuffer(gl.FRAMEBUFFER, multisampleFBO);
3380
+ var renderBuffer = new GLRenderBuffer(_this.renderer, {
3381
+ format: gl.RGBA8,
3382
+ attachment: gl.COLOR_ATTACHMENT0 + index,
3383
+ storageType: RenderPassAttachmentStorageType.color,
3384
+ multisample: _this.multisample,
3385
+ });
3386
+ _this.multisampleRbs.push(renderBuffer);
3387
+ renderBuffer.setSize(tex.width, tex.height);
3388
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + index, gl.RENDERBUFFER, renderBuffer.glHandle);
3389
+ }
3390
+ gl.bindFramebuffer(gl.FRAMEBUFFER, _this.fbo);
3300
3391
  gpu.framebufferTexture2D(gl, gl.FRAMEBUFFER, index, gl.TEXTURE_2D, tex.glHandle);
3301
3392
  buffers.push(true);
3302
3393
  });
@@ -3315,7 +3406,7 @@ var GLFrameBuffer = /** @class */ (function () {
3315
3406
  (_b = this.depthTexture) === null || _b === void 0 ? void 0 : _b.destroy();
3316
3407
  }
3317
3408
  //@ts-expect-error safe to assign
3318
- this.renderer = this.stencilRenderBuffer = this.depthStencilRenderBuffer = null;
3409
+ this.renderer = this.depthStencilRenderBuffer = null;
3319
3410
  }
3320
3411
  };
3321
3412
  GLFrameBuffer.prototype.resize = function (x, y, width, height) {
@@ -3914,6 +4005,7 @@ var MarsRenderPassColorAttachment = /** @class */ (function () {
3914
4005
  }
3915
4006
  this._isDestroyed = false;
3916
4007
  this.readable = true;
4008
+ this.multisample = options.multiSample || 1;
3917
4009
  }
3918
4010
  Object.defineProperty(MarsRenderPassColorAttachment.prototype, "isDestroyed", {
3919
4011
  get: function () {
@@ -3961,6 +4053,10 @@ var MarsRenderPassColorAttachment = /** @class */ (function () {
3961
4053
  if (this.texture) {
3962
4054
  this.texture.assignRenderer(renderer);
3963
4055
  }
4056
+ if (this.multisample > 1) {
4057
+ this.multisample = Math.min(renderer.gpu.capability.maxSample, this.multisample);
4058
+ }
4059
+ this.texture.internal.multisample = this.multisample;
3964
4060
  return this;
3965
4061
  };
3966
4062
  MarsRenderPassColorAttachment.prototype.destroy = function () {
@@ -4007,6 +4103,14 @@ var MarsRenderPass = /** @class */ (function () {
4007
4103
  enumerable: false,
4008
4104
  configurable: true
4009
4105
  });
4106
+ Object.defineProperty(MarsRenderPass.prototype, "multisample", {
4107
+ get: function () {
4108
+ var _a;
4109
+ return ((_a = this.frameBuffer) === null || _a === void 0 ? void 0 : _a.multisample) || 0;
4110
+ },
4111
+ enumerable: false,
4112
+ configurable: true
4113
+ });
4010
4114
  Object.defineProperty(MarsRenderPass.prototype, "attachments", {
4011
4115
  get: function () {
4012
4116
  return this._attachments;
@@ -4171,6 +4275,7 @@ var MarsRenderPass = /** @class */ (function () {
4171
4275
  storeAction: this.storeAction,
4172
4276
  name: name,
4173
4277
  viewport: viewport,
4278
+ multisample: options.multiSample,
4174
4279
  viewportScale: this.viewportScale,
4175
4280
  isCustomViewport: this._isCustomViewport,
4176
4281
  attachments: attachments.map(function (att) { return (att.texture).internal; }),
@@ -5603,7 +5708,7 @@ var MarsSharedGeometry = /** @class */ (function (_super) {
5603
5708
  return MarsSharedGeometry;
5604
5709
  }(MarsGeometry));
5605
5710
 
5606
- consoleLog('version: ' + "0.1.10");
5711
+ consoleLog('version: ' + "0.1.12");
5607
5712
 
5608
5713
  export { DestroyOptions, MarsGeometry as Geometry, MarsInstancedMesh as InstancedMesh, MarsTextureFactory, MarsMaterial as Material, MarsMaterialDataBlock as MaterialDataBlock, MarsMesh as Mesh, MarsRenderFrame as RenderFrame, MarsRenderPass as RenderPass, RenderPassAttachmentStorageType, RenderPassDestroyAttachmentType, RenderPassMeshOrder, RenderPassPriorityNormal, RenderPassPriorityPostprocess, RenderPassPriorityPrepare, MarsRenderer as Renderer, ShaderCompileResultStatus, ShaderLibraryEmpty, MarsSharedGeometry as SharedGeometry, MarsTexture as Texture, TextureLoadAction, TextureSourceType, TextureStoreAction, constants, getDefaultGPUCapability, getDefaultTextureFactory, setDefaultTextureFactory };
5609
5714
  //# sourceMappingURL=index.mjs.map