Skip to content

Commit cd48f93

Browse files
committed
[#187] API changes to render pipeline to avoid creation of frame buffers, closes #187
1 parent 8ea57d5 commit cd48f93

File tree

8 files changed

+65
-15
lines changed

8 files changed

+65
-15
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Version 0.5.5
2+
3+
* [[#187](https://github.com/bitbrain/braingdx/issues/187)] do not create frame buffers when no shaders are being used
4+
* remove `RenderPipeline.getPipe` method
5+
* add `RenderPipeline.addEffects` method
6+
* add `RenderPipe.hasEffects` method
7+
18
# Version 0.5.4
29

310
* [[#185](https://github.com/bitbrain/braingdx/issues/185)] add new `setDistanceStoppingThreshold` method to `GameCamera` which allows defining a threshold to avoid flickering

core/src/main/java/de/bitbrain/braingdx/graphics/pipeline/CombinedRenderPipe.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@ public void setEffects(PostProcessorEffect[] effects) {
5959
batchPostProcessor.addEffects(effects);
6060
}
6161

62+
@Override
63+
public boolean hasEffects() {
64+
return batchPostProcessor.hasEffects();
65+
}
66+
6267
public void render(Batch batch, float delta, FrameBuffer buffer) {
6368
if (isEnabled()) {
64-
if (batchPostProcessor.hasEffects()) {
69+
if (buffer == null) {
70+
layer.render(batch, delta);
71+
} else if (batchPostProcessor.hasEffects()) {
6572
batchPostProcessor.begin();
6673
this.batch.begin();
6774
this.batch.draw(buffer.getColorBufferTexture(), 0f, 0f);

core/src/main/java/de/bitbrain/braingdx/graphics/pipeline/CombinedRenderPipeline.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public class CombinedRenderPipeline implements RenderPipeline {
5959
private final SpriteBatch internalBatch;
6060
private FrameBuffer buffer;
6161
private OrthographicCamera camera;
62+
private boolean hasEffects;
63+
private int bufferWidth;
64+
private int bufferHeight;
6265

6366
public CombinedRenderPipeline(ShaderConfig config, SpriteBatch internalBatch, OrthographicCamera camera) {
6467
this(config, new PostProcessor(true, true, isDesktop), new FrameBufferFactory() {
@@ -114,14 +117,16 @@ public void resize(int width, int height) {
114117
if (buffer != null) {
115118
buffer.dispose();
116119
}
117-
buffer = bufferFactory.create(width, height);
120+
this.bufferWidth = width;
121+
this.bufferHeight = height;
118122
camera.setToOrtho(true, width, height);
119123
}
120124

121125
@Override
122126
public void put(String id, RenderLayer layer, PostProcessorEffect... effects) {
123127
CombinedRenderPipe pipe = new CombinedRenderPipe(layer, processor, internalBatch, effects);
124128
orderedPipes.put(id, pipe);
129+
this.hasEffects = hasEffects || effects.length > 0;
125130
}
126131

127132
@Override
@@ -132,6 +137,7 @@ public void putAfter(String existing, String id, RenderLayer layer, PostProcesso
132137
return;
133138
}
134139
orderedPipes.put(index + 1, id, new CombinedRenderPipe(layer, processor, internalBatch, effects));
140+
this.hasEffects = hasEffects || effects.length > 0;
135141
}
136142

137143
@Override
@@ -142,6 +148,7 @@ public void putBefore(String existing, String id, RenderLayer layer, PostProcess
142148
return;
143149
}
144150
orderedPipes.put(index > 0 ? index - 1 : index, id, new CombinedRenderPipe(layer, processor, internalBatch, effects));
151+
this.hasEffects = hasEffects || effects.length > 0;
145152
}
146153

147154
@Override
@@ -152,6 +159,13 @@ public void remove(String existingSourceId) {
152159
return;
153160
}
154161
orderedPipes.remove(existingSourceId);
162+
for (Object o : orderedPipes.valueList()) {
163+
CombinedRenderPipe pipe = (CombinedRenderPipe)o;
164+
if (pipe.hasEffects()) {
165+
return;
166+
}
167+
}
168+
this.hasEffects = false;
155169
}
156170

157171
@Override
@@ -162,6 +176,7 @@ public void setEffects(String existingSourceId, PostProcessorEffect... effects)
162176
return;
163177
}
164178
getPipe(existingSourceId).setEffects(effects);
179+
orderedPipes.remove(existingSourceId);
165180
}
166181

167182
@Override
@@ -200,8 +215,12 @@ public void moveAfter(String existingSourceId, String existingTargetId) {
200215
}
201216

202217
@Override
203-
public RenderPipe getPipe(String id) {
204-
return (RenderPipe) (orderedPipes.containsKey(id) ? orderedPipes.get(id) : null);
218+
public void addEffects(String existingSourceId, PostProcessorEffect... effects) {
219+
RenderPipe pipe = getPipe(existingSourceId);
220+
if (pipe != null) {
221+
pipe.addEffects(effects);
222+
this.hasEffects = hasEffects || effects.length > 0;
223+
}
205224
}
206225

207226
@SuppressWarnings("unchecked")
@@ -213,19 +232,24 @@ public Collection<String> getPipeIds() {
213232
@SuppressWarnings("unchecked")
214233
@Override
215234
public void render(Batch batch, float delta) {
216-
if (buffer == null) {
217-
return;
235+
if (buffer == null && hasEffects) {
236+
buffer = bufferFactory.create(this.bufferWidth, this.bufferHeight);
237+
} else if (buffer != null && !hasEffects) {
238+
buffer.dispose();
239+
buffer = null;
218240
}
219241
clearBuffer();
220242
for (CombinedRenderPipe pipe : (Collection<CombinedRenderPipe>) orderedPipes.values()) {
221243
pipe.beforeRender();
222244
pipe.render(batch, delta, buffer);
223245
}
224-
internalBatch.setProjectionMatrix(camera.combined);
225-
internalBatch.begin();
226-
internalBatch.setColor(Color.WHITE);
227-
internalBatch.draw(buffer.getColorBufferTexture(), 0f, 0f);
228-
internalBatch.end();
246+
if (hasEffects) {
247+
internalBatch.setProjectionMatrix(camera.combined);
248+
internalBatch.begin();
249+
internalBatch.setColor(Color.WHITE);
250+
internalBatch.draw(buffer.getColorBufferTexture(), 0f, 0f);
251+
internalBatch.end();
252+
}
229253
}
230254

231255
private void clearBuffer() {
@@ -236,4 +260,8 @@ private void clearBuffer() {
236260
buffer.end();
237261
}
238262
}
263+
264+
private RenderPipe getPipe(String id) {
265+
return (RenderPipe) (orderedPipes.containsKey(id) ? orderedPipes.get(id) : null);
266+
}
239267
}

core/src/main/java/de/bitbrain/braingdx/graphics/pipeline/LayeredRenderPipe.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public void setEffects(PostProcessorEffect[] effects) {
8484
batchPostProcessor.addEffects(effects);
8585
}
8686

87+
@Override
88+
public boolean hasEffects() {
89+
return batchPostProcessor.hasEffects();
90+
}
91+
8792
@Override
8893
public void dispose() {
8994
buffer.dispose();

core/src/main/java/de/bitbrain/braingdx/graphics/pipeline/LayeredRenderPipeline.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public void moveAfter(String existingSourceId, String existingTargetId) {
105105
}
106106

107107
@Override
108-
public RenderPipe getPipe(String id) {
109-
return pipes.get(id);
108+
public void addEffects(String existingSourceId, PostProcessorEffect... effects) {
109+
throw new UnsupportedOperationException("Not implemented yet!");
110110
}
111111

112112
@Override

core/src/main/java/de/bitbrain/braingdx/graphics/pipeline/RenderPipe.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ public interface RenderPipe {
3131
void addEffects(PostProcessorEffect... effects);
3232

3333
void setEffects(PostProcessorEffect[] effects);
34+
35+
boolean hasEffects();
3436
}

core/src/main/java/de/bitbrain/braingdx/graphics/pipeline/RenderPipeline.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.badlogic.gdx.graphics.g2d.Batch;
1818
import com.badlogic.gdx.utils.Disposable;
1919
import de.bitbrain.braingdx.graphics.postprocessing.PostProcessorEffect;
20+
import de.bitbrain.braingdx.graphics.postprocessing.filters.CrtScreen;
2021
import de.bitbrain.braingdx.util.Resizeable;
2122

2223
import java.util.Collection;
@@ -95,7 +96,7 @@ public interface RenderPipeline extends Disposable, Resizeable {
9596
* @param existingSourceId an existing layer
9697
* @return an existing {@link RenderPipe}
9798
*/
98-
RenderPipe getPipe(String existingSourceId);
99+
void addEffects(String existingSourceId, PostProcessorEffect... effects);
99100

100101
/**
101102
* Returns an ordered collection of all registered layers

core/src/test/java/de/bitbrain/braingdx/graphics/pipeline/RenderPipelineTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void beforeTest() {
5151
public void testAddLayer() {
5252
final String id = "my-id";
5353
pipeline.put(id, mock(RenderLayer.class));
54-
assertThat(pipeline.getPipe(id)).isNotNull();
54+
assertThat(pipeline.getPipeIds()).contains("my-id");
5555
}
5656

5757
@Test

0 commit comments

Comments
 (0)