Skip to content

Commit 952857c

Browse files
committed
Improve resource handling
1 parent 3da81a9 commit 952857c

File tree

5 files changed

+74
-57
lines changed

5 files changed

+74
-57
lines changed

src/effects/SMAAEffect.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import {
66
OrthographicCamera,
77
PerspectiveCamera,
88
Texture,
9-
Uniform,
10-
WebGLRenderTarget
9+
Uniform
1110
} from "three";
1211

12+
import { RenderTargetResource } from "../core/io/RenderTargetResource.js";
13+
import { TextureResource } from "../core/io/TextureResource.js";
1314
import { GBuffer } from "../enums/GBuffer.js";
1415
import { SMAAEdgeDetectionMode } from "../enums/SMAAEdgeDetectionMode.js";
1516
import { SMAAPredicationMode } from "../enums/SMAAPredicationMode.js";
@@ -115,8 +116,9 @@ export class SMAAEffect extends Effect {
115116
this.fragmentShader = fragmentShader;
116117
this.output.setBuffer(SMAAEffect.BUFFER_EDGES, this.createFramebuffer());
117118
this.output.setBuffer(SMAAEffect.BUFFER_WEIGHTS, this.createFramebuffer());
118-
this.input.uniforms.set("weightMap", new Uniform(this.weightsTexture));
119119
this.input.gBuffer.add(GBuffer.DEPTH);
120+
this.input.uniforms.set("weightMap", new Uniform(null));
121+
this.weightsTexture.bindUniform(this.input.uniforms.get("weightMap")!);
120122

121123
this.clearPass = new ClearPass(true, false, false);
122124
this.clearPass.output.defaultBuffer = this.renderTargetEdges;
@@ -161,39 +163,39 @@ export class SMAAEffect extends Effect {
161163
* A render target for the SMAA edge detection.
162164
*/
163165

164-
private get renderTargetEdges(): WebGLRenderTarget {
166+
private get renderTargetEdges(): RenderTargetResource {
165167

166-
return this.output.getBuffer(SMAAEffect.BUFFER_EDGES)!;
168+
return this.output.buffers.get(SMAAEffect.BUFFER_EDGES)!;
167169

168170
}
169171

170172
/**
171173
* A render target for the SMAA edge weights.
172174
*/
173175

174-
private get renderTargetWeights(): WebGLRenderTarget {
176+
private get renderTargetWeights(): RenderTargetResource {
175177

176-
return this.output.getBuffer(SMAAEffect.BUFFER_WEIGHTS)!;
178+
return this.output.buffers.get(SMAAEffect.BUFFER_WEIGHTS)!;
177179

178180
}
179181

180182
/**
181183
* The edges texture.
182184
*/
183185

184-
get edgesTexture(): Texture {
186+
get edgesTexture(): TextureResource {
185187

186-
return this.renderTargetEdges.texture;
188+
return this.output.buffers.get(SMAAEffect.BUFFER_EDGES)!.texture;
187189

188190
}
189191

190192
/**
191193
* The edge weights texture.
192194
*/
193195

194-
get weightsTexture(): Texture {
196+
get weightsTexture(): TextureResource {
195197

196-
return this.renderTargetWeights.texture;
198+
return this.output.buffers.get(SMAAEffect.BUFFER_WEIGHTS)!.texture;
197199

198200
}
199201

@@ -330,8 +332,8 @@ export class SMAAEffect extends Effect {
330332
const { width, height } = this.resolution;
331333
this.edgeDetectionMaterial.setSize(width, height);
332334
this.weightsMaterial.setSize(width, height);
333-
this.renderTargetEdges.setSize(width, height);
334-
this.renderTargetWeights.setSize(width, height);
335+
this.renderTargetEdges.value!.setSize(width, height);
336+
this.renderTargetWeights.value!.setSize(width, height);
335337

336338
}
337339

src/passes/DepthCopyPass.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { FloatType, NearestFilter, Texture, WebGLRenderTarget } from "three";
1+
import { FloatType, NearestFilter, WebGLRenderTarget } from "three";
2+
import { TextureResource } from "../core/io/TextureResource.js";
23
import { Pass } from "../core/Pass.js";
34
import { GBuffer } from "../enums/GBuffer.js";
45
import { DepthCopyMaterial } from "../materials/DepthCopyMaterial.js";
@@ -51,9 +52,9 @@ export class DepthCopyPass extends Pass<DepthCopyMaterial> {
5152
* The output texture.
5253
*/
5354

54-
get texture(): Texture {
55+
get texture(): TextureResource {
5556

56-
return this.renderTarget.texture;
57+
return this.output.buffers.get(DepthCopyPass.BUFFER_DEPTH)!.texture;
5758

5859
}
5960

src/passes/DepthDownsamplingPass.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { FloatType, NearestFilter, Texture, WebGLRenderer, WebGLRenderTarget } from "three";
1+
import { FloatType, NearestFilter, WebGLRenderer, WebGLRenderTarget } from "three";
2+
import { TextureResource } from "../core/io/TextureResource.js";
23
import { Pass } from "../core/Pass.js";
34
import { GBuffer } from "../enums/GBuffer.js";
45
import { DepthDownsamplingMaterial } from "../materials/DepthDownsamplingMaterial.js";
@@ -29,12 +30,15 @@ export class DepthDownsamplingPass extends Pass<DepthDownsamplingMaterial> {
2930
this.input.gBuffer.add(GBuffer.DEPTH);
3031
this.input.gBuffer.add(GBuffer.NORMAL);
3132

32-
this.renderTarget = new WebGLRenderTarget(1, 1, {
33-
minFilter: NearestFilter,
34-
magFilter: NearestFilter,
35-
depthBuffer: false,
36-
type: FloatType
37-
});
33+
this.output.setBuffer(
34+
DepthDownsamplingPass.BUFFER_DEPTH,
35+
new WebGLRenderTarget(1, 1, {
36+
minFilter: NearestFilter,
37+
magFilter: NearestFilter,
38+
depthBuffer: false,
39+
type: FloatType
40+
})
41+
);
3842

3943
}
4044

@@ -48,19 +52,13 @@ export class DepthDownsamplingPass extends Pass<DepthDownsamplingMaterial> {
4852

4953
}
5054

51-
private set renderTarget(value: WebGLRenderTarget) {
52-
53-
this.output.setBuffer(DepthDownsamplingPass.BUFFER_DEPTH, value);
54-
55-
}
56-
5755
/**
5856
* The output texture.
5957
*/
6058

61-
get texture(): Texture {
59+
get texture(): TextureResource {
6260

63-
return this.renderTarget.texture;
61+
return this.output.buffers.get(DepthDownsamplingPass.BUFFER_DEPTH)!.texture;
6462

6563
}
6664

src/passes/GaussianBlurPass.ts

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Texture, WebGLRenderTarget } from "three";
1+
import { WebGLRenderTarget } from "three";
2+
import { TextureResource } from "../core/io/TextureResource.js";
23
import { Pass } from "../core/Pass.js";
34
import { GaussianBlurMaterial } from "../materials/GaussianBlurMaterial.js";
45

@@ -45,16 +46,16 @@ export interface GaussianBlurPassOptions {
4546
export class GaussianBlurPass extends Pass<GaussianBlurMaterial> {
4647

4748
/**
48-
* The first blur buffer.
49+
* Identifies the first blur buffer.
4950
*/
5051

51-
private readonly renderTargetA: WebGLRenderTarget;
52+
private static readonly BUFFER_A = "BUFFER_A";
5253

5354
/**
54-
* The second blur buffer.
55+
* Identifies the second blur buffer.
5556
*/
5657

57-
private readonly renderTargetB: WebGLRenderTarget;
58+
private static readonly BUFFER_B = "BUFFER_B";
5859

5960
/**
6061
* The amount of blur iterations.
@@ -72,14 +73,8 @@ export class GaussianBlurPass extends Pass<GaussianBlurMaterial> {
7273

7374
super("GaussianBlurPass");
7475

75-
const renderTargetA = this.createFramebuffer();
76-
const renderTargetB = this.createFramebuffer();
77-
renderTargetA.texture.name = "BUFFER_0";
78-
renderTargetB.texture.name = "BUFFER_1";
79-
this.output.setBuffer(renderTargetA.texture.name, renderTargetA);
80-
this.output.setBuffer(renderTargetB.texture.name, renderTargetB);
81-
this.renderTargetA = renderTargetA;
82-
this.renderTargetB = renderTargetB;
76+
this.output.setBuffer(GaussianBlurPass.BUFFER_A, this.createFramebuffer());
77+
this.output.setBuffer(GaussianBlurPass.BUFFER_B, this.createFramebuffer());
8378

8479
this.fullscreenMaterial = new GaussianBlurMaterial({ kernelSize });
8580
this.resolution.scale = resolutionScale;
@@ -97,13 +92,33 @@ export class GaussianBlurPass extends Pass<GaussianBlurMaterial> {
9792

9893
}
9994

95+
/**
96+
* The first blur render target.
97+
*/
98+
99+
private get renderTargetA(): WebGLRenderTarget {
100+
101+
return this.output.getBuffer(GaussianBlurPass.BUFFER_A)!;
102+
103+
}
104+
105+
/**
106+
* The second blur render target.
107+
*/
108+
109+
private get renderTargetB(): WebGLRenderTarget {
110+
111+
return this.output.getBuffer(GaussianBlurPass.BUFFER_B)!;
112+
113+
}
114+
100115
/**
101116
* A texture that contains the blurred result.
102117
*/
103118

104-
get texture(): Texture {
119+
get texture(): TextureResource {
105120

106-
return this.renderTargetB.texture;
121+
return this.output.buffers.get(GaussianBlurPass.BUFFER_B)!.texture;
107122

108123
}
109124

@@ -117,13 +132,13 @@ export class GaussianBlurPass extends Pass<GaussianBlurMaterial> {
117132

118133
const { type, colorSpace } = this.input.defaultBuffer.value;
119134

120-
this.renderTargetA.texture.type = type;
121-
this.renderTargetA.texture.colorSpace = colorSpace;
122-
this.renderTargetA.dispose();
135+
for(const renderTarget of [this.renderTargetA, this.renderTargetB]) {
123136

124-
this.renderTargetB.texture.type = type;
125-
this.renderTargetB.texture.colorSpace = colorSpace;
126-
this.renderTargetB.dispose();
137+
renderTarget.texture.type = type;
138+
renderTarget.texture.colorSpace = colorSpace;
139+
renderTarget.dispose();
140+
141+
}
127142

128143
if(this.input.frameBufferPrecisionHigh) {
129144

@@ -155,7 +170,7 @@ export class GaussianBlurPass extends Pass<GaussianBlurMaterial> {
155170

156171
override render(): void {
157172

158-
if(this.renderer === null || this.input.defaultBuffer?.value === null) {
173+
if(this.renderer === null || this.input.defaultBuffer === null || this.input.defaultBuffer.value === null) {
159174

160175
return;
161176

@@ -166,7 +181,7 @@ export class GaussianBlurPass extends Pass<GaussianBlurMaterial> {
166181
const renderTargetB = this.renderTargetB;
167182
const blurMaterial = this.blurMaterial;
168183

169-
let previousBuffer = this.input.defaultBuffer!.value;
184+
let previousBuffer = this.input.defaultBuffer.value;
170185

171186
for(let i = 0, l = Math.max(this.iterations, 1); i < l; ++i) {
172187

src/passes/LuminancePass.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Texture, WebGLRenderTarget } from "three";
1+
import { WebGLRenderTarget } from "three";
2+
import { TextureResource } from "../core/io/TextureResource.js";
23
import { Pass } from "../core/Pass.js";
34
import { LuminanceMaterial } from "../materials/LuminanceMaterial.js";
45

@@ -43,9 +44,9 @@ export class LuminancePass extends Pass<LuminanceMaterial> {
4344
* The output texture.
4445
*/
4546

46-
get texture(): Texture {
47+
get texture(): TextureResource {
4748

48-
return this.renderTarget.texture;
49+
return this.output.buffers.get(LuminancePass.BUFFER_LUMINANCE)!.texture;
4950

5051
}
5152

0 commit comments

Comments
 (0)