Skip to content

Commit e9b68cf

Browse files
mvaligurskyMartin Valigursky
andauthored
Extracted WebglRenderTarget class from RenderTarget and WeblGraphicsDevice (playcanvas#4086)
* Extracted WebglRenderTarget class from RenderTarget and WeblGraphicsDevice * small update Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
1 parent 43df09a commit e9b68cf

File tree

8 files changed

+259
-201
lines changed

8 files changed

+259
-201
lines changed

scripts/utils/download-texture.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function readPixels(texture, face) {
103103
var rt = new pc.RenderTarget({ colorBuffer: texture, depth: false, face: face });
104104
var data = new Uint8ClampedArray(texture.width * texture.height * 4);
105105
var device = texture.device;
106-
device.setFramebuffer(rt._glFrameBuffer);
106+
device.setFramebuffer(rt.impl._glFrameBuffer);
107107
device.initRenderTarget(rt);
108108
device.gl.readPixels(0, 0, texture.width, texture.height, device.gl.RGBA, device.gl.UNSIGNED_BYTE, data);
109109
return data;

src/deprecated/deprecated.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,19 @@ Object.keys(deprecatedChunks).forEach((chunkName) => {
434434
});
435435
});
436436

437+
// Note: This was never public interface, but has been used in external scripts
438+
Object.defineProperties(RenderTarget.prototype, {
439+
_glFrameBuffer: {
440+
get: function () {
441+
Debug.deprecated("pc.RenderTarget#_glFrameBuffer is deprecated. Use pc.RenderTarget.impl#_glFrameBuffer instead.");
442+
return this.impl._glFrameBuffer;
443+
},
444+
set: function (rgbm) {
445+
Debug.deprecated("pc.RenderTarget#_glFrameBuffer is deprecated. Use pc.RenderTarget.impl#_glFrameBuffer instead.");
446+
}
447+
}
448+
});
449+
437450
VertexFormat.prototype.update = function () {
438451
Debug.deprecated('pc.VertexFormat.update is deprecated, and VertexFormat cannot be changed after it has been created.');
439452
};

src/framework/scene-depth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class SceneDepth {
137137

138138
const gl = app.graphicsDevice.gl;
139139
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, this.srcFbo);
140-
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this.renderTarget._glFrameBuffer);
140+
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this.renderTarget.impl._glFrameBuffer);
141141
gl.blitFramebuffer(0, 0, this.renderTarget.width, this.renderTarget.height,
142142
0, 0, this.renderTarget.width, this.renderTarget.height,
143143
gl.DEPTH_BUFFER_BIT, gl.NEAREST);

src/graphics/grab-pass.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class GrabPass {
9292

9393
// render target currently being rendered to (these are null if default framebuffer is active)
9494
const renderTarget = device.renderTarget;
95-
const resolveRenderTarget = renderTarget && renderTarget._glResolveFrameBuffer;
95+
const resolveRenderTarget = renderTarget && renderTarget.impl._glResolveFrameBuffer;
9696

9797
const texture = this.texture;
9898
const width = device.width;
@@ -106,12 +106,12 @@ class GrabPass {
106106
}
107107

108108
// these are null if rendering to default framebuffer
109-
const currentFrameBuffer = renderTarget ? renderTarget._glFrameBuffer : null;
110-
const resolvedFrameBuffer = renderTarget ? renderTarget._glResolveFrameBuffer || renderTarget._glFrameBuffer : null;
109+
const currentFrameBuffer = renderTarget ? renderTarget.impl._glFrameBuffer : null;
110+
const resolvedFrameBuffer = renderTarget ? renderTarget.impl._glResolveFrameBuffer || renderTarget.impl._glFrameBuffer : null;
111111

112112
// init grab pass framebuffer (only does it once)
113113
device.initRenderTarget(this.renderTarget);
114-
const grabPassFrameBuffer = this.renderTarget._glFrameBuffer;
114+
const grabPassFrameBuffer = this.renderTarget.impl._glFrameBuffer;
115115

116116
// blit from currently used render target (or default framebuffer if null)
117117
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, resolvedFrameBuffer);
@@ -127,7 +127,7 @@ class GrabPass {
127127
} else {
128128
if (resolveRenderTarget) {
129129
renderTarget.resolve(true);
130-
gl.bindFramebuffer(gl.FRAMEBUFFER, renderTarget._glResolveFrameBuffer);
130+
gl.bindFramebuffer(gl.FRAMEBUFFER, renderTarget.impl._glResolveFrameBuffer);
131131
}
132132

133133
// this allocates texture (texture was already bound to gl)
@@ -137,7 +137,7 @@ class GrabPass {
137137
texture._height = height;
138138

139139
if (resolveRenderTarget) {
140-
gl.bindFramebuffer(gl.FRAMEBUFFER, renderTarget._glFrameBuffer);
140+
gl.bindFramebuffer(gl.FRAMEBUFFER, renderTarget.impl._glFrameBuffer);
141141
}
142142
}
143143

src/graphics/prefilter-cubemap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function shFromCubemap(device, source, dontFlipX) {
9898
drawQuadWithShader(device, targ, shader);
9999

100100
const gl = device.gl;
101-
gl.bindFramebuffer(gl.FRAMEBUFFER, targ._glFrameBuffer);
101+
gl.bindFramebuffer(gl.FRAMEBUFFER, targ.impl._glFrameBuffer);
102102

103103
const pixels = new Uint8Array(cubeSize * cubeSize * 4);
104104
gl.readPixels(0, 0, tex.width, tex.height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);

src/graphics/render-target.js

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ class RenderTarget {
8888
this._colorBuffer._isRenderTarget = true;
8989
}
9090

91-
this._glFrameBuffer = null;
92-
this._glDepthBuffer = null;
93-
9491
// Process optional arguments
9592
options = (options !== undefined) ? options : defaultOptions;
9693
this._depthBuffer = options.depthBuffer;
@@ -120,9 +117,6 @@ class RenderTarget {
120117

121118
this._samples = (options.samples !== undefined) ? Math.min(options.samples, this._device.maxSamples) : 1;
122119
this.autoResolve = (options.autoResolve !== undefined) ? options.autoResolve : true;
123-
this._glResolveFrameBuffer = null;
124-
this._glMsaaColorBuffer = null;
125-
this._glMsaaDepthBuffer = null;
126120

127121
// use specified name, otherwise get one from color or depth buffer
128122
this.name = options.name;
@@ -138,6 +132,9 @@ class RenderTarget {
138132

139133
// render image flipped in Y
140134
this.flipY = !!options.flipY;
135+
136+
// device specific implementation
137+
this.impl = this._device.createRenderTargetImpl(this);
141138
}
142139

143140
/**
@@ -157,39 +154,15 @@ class RenderTarget {
157154
}
158155

159156
/**
160-
* Free WebGL resources associated with this render target.
157+
* Free device resources associated with this render target.
161158
*
162159
* @ignore
163160
*/
164161
destroyFrameBuffers() {
165162

166163
const device = this._device;
167164
if (device) {
168-
const gl = device.gl;
169-
if (this._glFrameBuffer) {
170-
gl.deleteFramebuffer(this._glFrameBuffer);
171-
this._glFrameBuffer = null;
172-
}
173-
174-
if (this._glDepthBuffer) {
175-
gl.deleteRenderbuffer(this._glDepthBuffer);
176-
this._glDepthBuffer = null;
177-
}
178-
179-
if (this._glResolveFrameBuffer) {
180-
gl.deleteFramebuffer(this._glResolveFrameBuffer);
181-
this._glResolveFrameBuffer = null;
182-
}
183-
184-
if (this._glMsaaColorBuffer) {
185-
gl.deleteRenderbuffer(this._glMsaaColorBuffer);
186-
this._glMsaaColorBuffer = null;
187-
}
188-
189-
if (this._glMsaaDepthBuffer) {
190-
gl.deleteRenderbuffer(this._glMsaaDepthBuffer);
191-
this._glMsaaDepthBuffer = null;
192-
}
165+
this.impl.destroy(device);
193166
}
194167
}
195168

@@ -212,16 +185,21 @@ class RenderTarget {
212185
}
213186

214187
/**
215-
* Called when the WebGL context was lost. It releases all context related resources.
188+
* Initialises the resources associated with this render target.
189+
*
190+
* @ignore
191+
*/
192+
init() {
193+
this.impl.init(this._device, this);
194+
}
195+
196+
/**
197+
* Called when the device context was lost. It releases all context related resources.
216198
*
217199
* @ignore
218200
*/
219201
loseContext() {
220-
this._glFrameBuffer = undefined;
221-
this._glDepthBuffer = undefined;
222-
this._glResolveFrameBuffer = undefined;
223-
this._glMsaaColorBuffer = undefined;
224-
this._glMsaaDepthBuffer = undefined;
202+
this.impl.loseContext();
225203
}
226204

227205
/**
@@ -239,17 +217,9 @@ class RenderTarget {
239217
* depth buffer.
240218
*/
241219
resolve(color = true, depth = !!this._depthBuffer) {
242-
if (!this._device) return;
243-
if (!this._device.webgl2) return;
244-
245-
const gl = this._device.gl;
246-
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, this._glFrameBuffer);
247-
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this._glResolveFrameBuffer);
248-
gl.blitFramebuffer(0, 0, this.width, this.height,
249-
0, 0, this.width, this.height,
250-
(color ? gl.COLOR_BUFFER_BIT : 0) | (depth ? gl.DEPTH_BUFFER_BIT : 0),
251-
gl.NEAREST);
252-
gl.bindFramebuffer(gl.FRAMEBUFFER, this._glFrameBuffer);
220+
if (this._device) {
221+
this.impl.resolve(this._device, this, color, depth);
222+
}
253223
}
254224

255225
/**

0 commit comments

Comments
 (0)