-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathRenderContext.js
More file actions
281 lines (237 loc) · 5.1 KB
/
RenderContext.js
File metadata and controls
281 lines (237 loc) · 5.1 KB
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
import { Vector4 } from '../../math/Vector4.js';
import { hashArray } from '../../nodes/core/NodeUtils.js';
let _id = 0;
/**
* Any render or compute command is executed in a specific context that defines
* the state of the renderer and its backend. Typical examples for such context
* data are the current clear values or data from the active framebuffer. This
* module is used to represent these contexts as objects.
*
* @private
*/
class RenderContext {
/**
* Constructs a new render context.
*/
constructor() {
/**
* The context's ID.
*
* @type {number}
*/
this.id = _id ++;
/**
* The MRT configuration.
*
* @type {?MRTNode}
* @default null
*/
this.mrt = null;
/**
* Whether the current active framebuffer has a color attachment.
*
* @type {boolean}
* @default true
*/
this.color = true;
/**
* Whether the color attachment should be cleared or not.
*
* @type {boolean}
* @default true
*/
this.clearColor = true;
/**
* The clear color value.
*
* @type {Object}
* @default true
*/
this.clearColorValue = { r: 0, g: 0, b: 0, a: 1 };
/**
* Whether the current active framebuffer has a depth attachment.
*
* @type {boolean}
* @default true
*/
this.depth = true;
/**
* Whether the depth attachment should be cleared or not.
*
* @type {boolean}
* @default true
*/
this.clearDepth = true;
/**
* The clear depth value.
*
* @type {number}
* @default 1
*/
this.clearDepthValue = 1;
/**
* Whether the current active framebuffer has a stencil attachment.
*
* @type {boolean}
* @default false
*/
this.stencil = false;
/**
* Whether the stencil attachment should be cleared or not.
*
* @type {boolean}
* @default true
*/
this.clearStencil = true;
/**
* The clear stencil value.
*
* @type {number}
* @default 1
*/
this.clearStencilValue = 1;
/**
* By default the viewport encloses the entire framebuffer If a smaller
* viewport is manually defined, this property is to `true` by the renderer.
*
* @type {boolean}
* @default false
*/
this.viewport = false;
/**
* The viewport value. This value is in physical pixels meaning it incorporates
* the renderer's pixel ratio. The viewport property of render targets or
* the renderer is in logical pixels.
*
* @type {Vector4}
*/
this.viewportValue = new Vector4();
/**
* When the scissor test is active and scissor rectangle smaller than the
* framebuffers dimensions, this property is to `true` by the renderer.
*
* @type {boolean}
* @default false
*/
this.scissor = false;
/**
* The scissor rectangle.
*
* @type {Vector4}
*/
this.scissorValue = new Vector4();
/**
* The active render target.
*
* @type {?RenderTarget}
* @default null
*/
this.renderTarget = null;
/**
* The textures of the active render target.
* `null` when no render target is set.
*
* @type {?Array<Texture>}
* @default null
*/
this.textures = null;
/**
* The depth texture of the active render target.
* `null` when no render target is set.
*
* @type {?DepthTexture}
* @default null
*/
this.depthTexture = null;
/**
* The active cube face.
*
* @type {number}
* @default 0
*/
this.activeCubeFace = 0;
/**
* The active mipmap level.
*
* @type {number}
* @default 0
*/
this.activeMipmapLevel = 0;
/**
* The number of MSAA samples. This value is always `1` when
* MSAA isn't used.
*
* @type {number}
* @default 1
*/
this.sampleCount = 1;
/**
* The active render target's width in physical pixels.
*
* @type {number}
* @default 0
*/
this.width = 0;
/**
* The active render target's height in physical pixels.
*
* @type {number}
* @default 0
*/
this.height = 0;
/**
* The occlusion query count.
*
* @type {number}
* @default 0
*/
this.occlusionQueryCount = 0;
/**
* The current clipping context.
*
* @type {?ClippingContext}
* @default null
*/
this.clippingContext = null;
/**
* The current camera.
*
* @type {?Camera}
* @default null
*/
this.camera = null;
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isRenderContext = true;
}
/**
* Returns the cache key of this render context.
*
* @return {number} The cache key.
*/
getCacheKey() {
return getCacheKey( this );
}
}
/**
* Computes a cache key for the given render context. This key
* should identify the render target state so it is possible to
* configure the correct attachments in the respective backend.
*
* @param {RenderContext} renderContext - The render context.
* @return {number} The cache key.
*/
export function getCacheKey( renderContext ) {
const { textures, activeCubeFace, activeMipmapLevel } = renderContext;
const values = [ activeCubeFace, activeMipmapLevel ];
for ( const texture of textures ) {
values.push( texture.id );
}
return hashArray( values );
}
export default RenderContext;