-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathRenderContexts.js
More file actions
121 lines (86 loc) · 2.9 KB
/
RenderContexts.js
File metadata and controls
121 lines (86 loc) · 2.9 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
import RenderContext from './RenderContext.js';
/**
* This module manages the render contexts of the renderer.
*
* @private
*/
class RenderContexts {
/**
* Constructs a new render context management component.
*
* @param {Renderer} renderer - The renderer.
*/
constructor( renderer ) {
/**
* The renderer.
*
* @type {Renderer}
*/
this.renderer = renderer;
/**
* A dictionary that manages render contexts.
*
* @type {Object<string,RenderContext>}
*/
this._renderContexts = {};
}
/**
* Returns a render context for the given scene, camera and render target.
*
* @param {?RenderTarget} [renderTarget=null] - The active render target.
* @param {?MRTNode} [mrt=null] - The MRT configuration
* @param {?number} [callDepth=0] - The call depth of the renderer.
* @return {RenderContext} The render context.
*/
get( renderTarget = null, mrt = null, callDepth = 0 ) {
//
let attachmentState;
if ( renderTarget === null ) {
attachmentState = 'default';
} else {
const format = renderTarget.texture.format;
const type = renderTarget.texture.type;
const count = renderTarget.textures.length;
const samples = renderTarget.samples;
const depthBuffer = renderTarget.depthBuffer;
const stencilBuffer = renderTarget.stencilBuffer;
if ( renderTarget._attachmentState === undefined ||
renderTarget._attachmentStateFormat !== format ||
renderTarget._attachmentStateType !== type ||
renderTarget._attachmentStateSamples !== samples ||
renderTarget._attachmentStateCount !== count ||
renderTarget._attachmentStateDepth !== depthBuffer ||
renderTarget._attachmentStateStencil !== stencilBuffer ) {
renderTarget._attachmentState = `${ count }:${ format }:${ type }:${ samples }:${ depthBuffer }:${ stencilBuffer }`;
renderTarget._attachmentStateFormat = format;
renderTarget._attachmentStateType = type;
renderTarget._attachmentStateSamples = samples;
renderTarget._attachmentStateCount = count;
renderTarget._attachmentStateDepth = depthBuffer;
renderTarget._attachmentStateStencil = stencilBuffer;
}
attachmentState = renderTarget._attachmentState;
}
//
const mrtState = ( mrt !== null ) ? mrt.id : 'default';
//
const renderStateKey = attachmentState + '-' + mrtState + '-' + callDepth;
let renderState = this._renderContexts[ renderStateKey ];
if ( renderState === undefined ) {
renderState = new RenderContext();
renderState.mrt = mrt;
this._renderContexts[ renderStateKey ] = renderState;
}
if ( renderTarget !== null ) renderState.sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;
renderState.clearDepthValue = this.renderer.getClearDepth();
renderState.clearStencilValue = this.renderer.getClearStencil();
return renderState;
}
/**
* Frees internal resources.
*/
dispose() {
this._renderContexts = {};
}
}
export default RenderContexts;