@@ -30,6 +30,8 @@ public class RenderTargets {
30
30
private DepthCopyStrategy copyStrategy ;
31
31
32
32
private final List <GlFramebuffer > ownedFramebuffers ;
33
+ private final Map <Integer , PackRenderTargetDirectives .RenderTargetSettings > targetSettingsMap ;
34
+ private final PackDirectives packDirectives ;
33
35
34
36
private int cachedWidth ;
35
37
private int cachedHeight ;
@@ -43,13 +45,8 @@ public class RenderTargets {
43
45
public RenderTargets (int width , int height , int depthTexture , int depthBufferVersion , DepthBufferFormat depthFormat , Map <Integer , PackRenderTargetDirectives .RenderTargetSettings > renderTargets , PackDirectives packDirectives ) {
44
46
targets = new RenderTarget [renderTargets .size ()];
45
47
46
- renderTargets .forEach ((index , settings ) -> {
47
- // TODO: Handle mipmapping?
48
- Vector2i dimensions = packDirectives .getTextureScaleOverride (index , width , height );
49
- targets [index ] = RenderTarget .builder ().setDimensions (dimensions .x , dimensions .y )
50
- .setInternalFormat (settings .getInternalFormat ())
51
- .setPixelFormat (settings .getInternalFormat ().getPixelFormat ()).build ();
52
- });
48
+ targetSettingsMap = renderTargets ;
49
+ this .packDirectives = packDirectives ;
53
50
54
51
this .currentDepthTexture = depthTexture ;
55
52
this .currentDepthFormat = depthFormat ;
@@ -88,7 +85,9 @@ public void destroy() {
88
85
}
89
86
90
87
for (RenderTarget target : targets ) {
91
- target .destroy ();
88
+ if (target != null ) {
89
+ target .destroy ();
90
+ }
92
91
}
93
92
94
93
noTranslucents .destroy ();
@@ -104,9 +103,33 @@ public RenderTarget get(int index) {
104
103
throw new IllegalStateException ("Tried to use destroyed RenderTargets" );
105
104
}
106
105
106
+ if (targets [index ] == null ) {
107
+ return null ;
108
+ }
109
+
107
110
return targets [index ];
108
111
}
109
112
113
+ public RenderTarget getOrCreate (int index ) {
114
+ if (destroyed ) {
115
+ throw new IllegalStateException ("Tried to use destroyed RenderTargets" );
116
+ }
117
+
118
+ if (targets [index ] != null ) return targets [index ];
119
+
120
+ create (index );
121
+
122
+ return targets [index ];
123
+ }
124
+
125
+ private void create (int index ) {
126
+ PackRenderTargetDirectives .RenderTargetSettings settings = targetSettingsMap .get (index );
127
+ Vector2i dimensions = packDirectives .getTextureScaleOverride (index , cachedWidth , cachedHeight );
128
+ targets [index ] = RenderTarget .builder ().setDimensions (dimensions .x , dimensions .y )
129
+ .setInternalFormat (settings .getInternalFormat ())
130
+ .setPixelFormat (settings .getInternalFormat ().getPixelFormat ()).build ();
131
+ }
132
+
110
133
public int getDepthTexture () {
111
134
return currentDepthTexture ;
112
135
}
@@ -174,7 +197,9 @@ public boolean resizeIfNeeded(int newDepthBufferVersion, int newDepthTextureId,
174
197
cachedHeight = newHeight ;
175
198
176
199
for (int i = 0 ; i < targets .length ; i ++) {
177
- targets [i ].resize (packDirectives .getTextureScaleOverride (i , newWidth , newHeight ));
200
+ if (targets [i ] != null ) {
201
+ targets [i ].resize (packDirectives .getTextureScaleOverride (i , newWidth , newHeight ));
202
+ }
178
203
}
179
204
180
205
fullClearRequired = true ;
@@ -253,7 +278,7 @@ private GlFramebuffer createEmptyFramebuffer() {
253
278
254
279
// NB: Before OpenGL 3.0, all framebuffers are required to have a color
255
280
// attachment no matter what.
256
- framebuffer .addColorAttachment (0 , get (0 ).getMainTexture ());
281
+ framebuffer .addColorAttachment (0 , getOrCreate (0 ).getMainTexture ());
257
282
framebuffer .noDrawBuffers ();
258
283
259
284
return framebuffer ;
@@ -316,7 +341,7 @@ public GlFramebuffer createColorFramebuffer(ImmutableSet<Integer> stageWritesToM
316
341
+ getRenderTargetCount () + " render targets are supported." );
317
342
}
318
343
319
- RenderTarget target = this .get (drawBuffers [i ]);
344
+ RenderTarget target = this .getOrCreate (drawBuffers [i ]);
320
345
321
346
int textureId = stageWritesToMain .contains (drawBuffers [i ]) ? target .getMainTexture () : target .getAltTexture ();
322
347
0 commit comments