Skip to content

Commit 69781d0

Browse files
committed
finish dynamic resolution
1 parent 298648b commit 69781d0

File tree

8 files changed

+57
-74
lines changed

8 files changed

+57
-74
lines changed

sources/include/cage-engine/provisionalGraphics.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,32 @@ namespace cage
1515
{
1616
public:
1717
Holder<UniformBuffer> resolve(); // requires opengl context
18-
bool ready() const;
1918
};
2019

2120
class CAGE_ENGINE_API ProvisionalFrameBuffer : private Immovable
2221
{
2322
public:
2423
Holder<FrameBuffer> resolve(); // requires opengl context
25-
bool ready() const;
2624
};
2725

2826
class CAGE_ENGINE_API ProvisionalTexture : private Immovable
2927
{
3028
public:
3129
Holder<Texture> resolve(); // requires opengl context
32-
bool ready() const;
3330
};
3431

3532
class CAGE_ENGINE_API ProvisionalGraphics : private Immovable
3633
{
3734
public:
3835
// section: thread-safe, does not require opengl context
3936

40-
Holder<ProvisionalUniformBuffer> uniformBuffer(const String &name);
37+
Holder<ProvisionalUniformBuffer> uniformBuffer(const String &name, std::function<void(UniformBuffer *)> &&init = {});
4138

4239
Holder<ProvisionalFrameBuffer> frameBufferDraw(const String &name);
4340
Holder<ProvisionalFrameBuffer> frameBufferRead(const String &name);
4441

45-
Holder<ProvisionalTexture> texture(const String &name, std::function<void(Texture *)> &&init);
46-
Holder<ProvisionalTexture> texture(const String &name, uint32 target, std::function<void(Texture *)> &&init);
42+
Holder<ProvisionalTexture> texture(const String &name, std::function<void(Texture *)> &&init = {});
43+
Holder<ProvisionalTexture> texture(const String &name, uint32 target, std::function<void(Texture *)> &&init = {});
4744

4845
// section: thread-safe, requires opengl context
4946

sources/include/cage-simple/engine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace cage
8686
VoicesMixer *engineMasterMixer();
8787
VoicesMixer *engineEffectsMixer();
8888
VoicesMixer *engineGuiMixer();
89-
ProvisionalGraphics *engineProvisonalGraphics();
89+
ProvisionalGraphics *engineProvisionalGraphics();
9090
uint64 engineControlTime();
9191

9292
struct EngineDynamicResolution

sources/libengine/graphics/provisionalGraphics.cpp

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ namespace cage
1818
{
1919
public:
2020
const String name;
21+
std::function<void(UniformBuffer *)> init;
2122
Holder<UniformBuffer> result;
2223
ProvisionalGraphicsImpl *impl = nullptr;
24+
uint32 type = m; // 1 = init copied
2325
bool used = true;
2426

2527
ProvisionalUniformBufferImpl(const String &name) : name(name) {}
@@ -64,25 +66,21 @@ namespace cage
6466
{
6567
Holder<T> v = systemMemory().createHolder<T>(name);
6668
data.insert(v.share());
67-
return std::move(v);
69+
return v;
6870
}
6971
return it->share();
7072
}
7173

7274
void reset()
7375
{
7476
ScopeLock lock(mutex);
75-
auto it = data.begin();
76-
while (it != data.end())
77-
{
78-
if ((*it)->used)
77+
std::erase_if(data,
78+
[](auto &it)
7979
{
80-
(*it)->used = false;
81-
it++;
82-
}
83-
else
84-
it = data.erase(it);
85-
}
80+
const bool res = it->used;
81+
it->used = false;
82+
return !res;
83+
});
8684
}
8785

8886
void purge()
@@ -119,9 +117,14 @@ namespace cage
119117
Container<ProvisionalFrameBufferHandleImpl> frameBuffers;
120118
Container<ProvisionalTextureHandleImpl> textures;
121119

122-
Holder<ProvisionalUniformBuffer> uniformBuffer(const String &name)
120+
Holder<ProvisionalUniformBuffer> uniformBuffer(const String &name, std::function<void(UniformBuffer *)> &&init)
123121
{
124122
auto ub = uniformBuffers.acquire(name);
123+
if (ub->type == m)
124+
{
125+
ub->type = 1;
126+
ub->init = std::move(init);
127+
}
125128
return std::move(ub).cast<ProvisionalUniformBuffer>();
126129
}
127130

@@ -169,16 +172,12 @@ namespace cage
169172
{
170173
impl->result = newUniformBuffer();
171174
impl->result->setDebugName(impl->name);
175+
if (impl->init)
176+
impl->init(+impl->result);
172177
}
173178
return impl->result.share();
174179
}
175180

176-
bool ProvisionalUniformBuffer::ready() const
177-
{
178-
const ProvisionalUniformBufferImpl *impl = (const ProvisionalUniformBufferImpl *)this;
179-
return !!impl->result;
180-
}
181-
182181
Holder<FrameBuffer> ProvisionalFrameBuffer::resolve()
183182
{
184183
ProvisionalFrameBufferHandleImpl *impl = (ProvisionalFrameBufferHandleImpl *)this;
@@ -199,12 +198,6 @@ namespace cage
199198
return impl->result.share();
200199
}
201200

202-
bool ProvisionalFrameBuffer::ready() const
203-
{
204-
const ProvisionalFrameBufferHandleImpl *impl = (const ProvisionalFrameBufferHandleImpl *)this;
205-
return !!impl->result;
206-
}
207-
208201
Holder<Texture> ProvisionalTexture::resolve()
209202
{
210203
ProvisionalTextureHandleImpl *impl = (ProvisionalTextureHandleImpl *)this;
@@ -213,21 +206,16 @@ namespace cage
213206
{
214207
impl->result = newTexture(impl->target);
215208
impl->result->setDebugName(impl->name);
216-
impl->init(+impl->result);
209+
if (impl->init)
210+
impl->init(+impl->result);
217211
}
218212
return impl->result.share();
219213
}
220214

221-
bool ProvisionalTexture::ready() const
222-
{
223-
const ProvisionalTextureHandleImpl *impl = (const ProvisionalTextureHandleImpl *)this;
224-
return !!impl->result;
225-
}
226-
227-
Holder<ProvisionalUniformBuffer> ProvisionalGraphics::uniformBuffer(const String &name)
215+
Holder<ProvisionalUniformBuffer> ProvisionalGraphics::uniformBuffer(const String &name, std::function<void(UniformBuffer *)> &&init)
228216
{
229217
ProvisionalGraphicsImpl *impl = (ProvisionalGraphicsImpl *)this;
230-
return impl->uniformBuffer(name);
218+
return impl->uniformBuffer(name, std::move(init));
231219
}
232220

233221
Holder<ProvisionalFrameBuffer> ProvisionalGraphics::frameBufferDraw(const String &name)

sources/libengine/graphics/renderPipeline.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ namespace cage
957957
TextureHandle colorTexture = provisionalGraphics->texture(Stringizer() + "colorTarget_" + data.name + "_" + data.resolution,
958958
[resolution = data.resolution](Texture *t)
959959
{
960-
t->initialize(resolution, 1, GL_RGBA16F);
960+
t->initialize(resolution, 1, GL_RGBA16F); // RGB16F (without alpha) is not required to work in frame buffer
961961
t->filters(GL_LINEAR, GL_LINEAR, 0);
962962
t->wraps(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
963963
});
@@ -1047,17 +1047,13 @@ namespace cage
10471047
const auto graphicsDebugScope = renderQueue->namedScope("effects");
10481048

10491049
TextureHandle texSource = colorTexture;
1050-
TextureHandle texTarget = [&]()
1051-
{
1052-
TextureHandle t = provisionalGraphics->texture(Stringizer() + "intermediateTarget_" + data.resolution,
1053-
[resolution = data.resolution](Texture *t)
1054-
{
1055-
t->initialize(resolution, 1, GL_RGBA16F);
1056-
t->filters(GL_LINEAR, GL_LINEAR, 0);
1057-
t->wraps(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1058-
});
1059-
return t;
1060-
}();
1050+
TextureHandle texTarget = provisionalGraphics->texture(Stringizer() + "intermediateTarget_" + data.resolution,
1051+
[resolution = data.resolution](Texture *t)
1052+
{
1053+
t->initialize(resolution, 1, GL_RGBA16F); // RGB16F (without alpha) is not required to work in frame buffer
1054+
t->filters(GL_LINEAR, GL_LINEAR, 0);
1055+
t->wraps(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1056+
});
10611057

10621058
// depth of field
10631059
if (any(data.effects.effects & ScreenSpaceEffectsFlags::DepthOfField))
@@ -1073,6 +1069,10 @@ namespace cage
10731069
std::swap(texSource, texTarget);
10741070
}
10751071

1072+
renderQueue->resetFrameBuffer();
1073+
renderQueue->resetAllState();
1074+
renderQueue->resetAllTextures();
1075+
10761076
// eye adaptation
10771077
if (any(data.effects.effects & ScreenSpaceEffectsFlags::EyeAdaptation))
10781078
{
@@ -1138,11 +1138,13 @@ namespace cage
11381138
// blit to destination
11391139
if (texSource != colorTexture)
11401140
{
1141+
const auto graphicsDebugScope = renderQueue->namedScope("effects blit");
11411142
renderQueue->viewport(Vec2i(), data.resolution);
11421143
renderQueue->bind(texSource, 0);
11431144
renderQueue->bind(shaderBlitPixels);
11441145
renderQueue->bind(renderTarget);
11451146
renderQueue->colorTexture(renderTarget, 0, colorTexture);
1147+
renderQueue->depthTexture(renderTarget, {});
11461148
renderQueue->activeAttachments(renderTarget, 1);
11471149
renderQueue->checkFrameBuffer(renderTarget);
11481150
renderQueue->draw(modelSquare);
@@ -1155,6 +1157,7 @@ namespace cage
11551157
const auto graphicsDebugScope = renderQueue->namedScope("final blit");
11561158
renderQueue->resetAllState();
11571159
renderQueue->viewport(Vec2i(), data.resolution);
1160+
renderQueue->bind(data.target, 0); // ensure the texture is properly initialized
11581161
renderQueue->bind(colorTexture, 0);
11591162
renderQueue->bind(shaderBlitPixels);
11601163
renderQueue->bind(renderTarget);

sources/libengine/graphics/screenSpaceEffects.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cage-engine/screenSpaceEffects.h>
99
#include <cage-engine/shaderProgram.h>
1010
#include <cage-engine/texture.h>
11+
#include <cage-engine/uniformBuffer.h>
1112

1213
namespace cage
1314
{
@@ -82,13 +83,7 @@ namespace cage
8283
FrameBufferHandle fb = config.provisionals->frameBufferDraw("graphicsEffects");
8384
q->bind(fb);
8485

85-
UniformBufferHandle ssaoPoints = config.provisionals->uniformBuffer("ssaoPoints");
86-
{ // potentially dangerous hack
87-
ProvisionalUniformBuffer *pub = (ProvisionalUniformBuffer *)ssaoPoints.pointer();
88-
CAGE_ASSERT(pub);
89-
if (!pub->ready())
90-
q->writeWhole(ssaoPoints, privat::pointsForSsaoShader(), GL_STATIC_DRAW);
91-
}
86+
UniformBufferHandle ssaoPoints = config.provisionals->uniformBuffer("ssaoPoints", [](UniformBuffer *ub) { ub->writeWhole(privat::pointsForSsaoShader(), GL_STATIC_DRAW); });
9287
q->bind(ssaoPoints, 3);
9388

9489
struct Shader
@@ -229,7 +224,7 @@ namespace cage
229224
q->bindImage(config.inColor, 0, true, false);
230225
TextureHandle texHist = provTex(config.provisionals, Stringizer() + "luminanceHistogram", Vec2i(256, 1), 1, GL_R32UI);
231226
q->bindImage(texHist, 1, true, true);
232-
TextureHandle texAccum = provTex(config.provisionals, Stringizer() + "luminanceAccumulation" + config.cameraId, Vec2i(1), 1, GL_R32F);
227+
TextureHandle texAccum = provTex(config.provisionals, Stringizer() + "luminanceAccumulation_" + config.cameraId, Vec2i(1), 1, GL_R32F);
233228
q->bindImage(texAccum, 2, true, true);
234229

235230
// collection
@@ -312,7 +307,7 @@ namespace cage
312307
q->colorTexture(fb, 0, config.outColor);
313308
q->checkFrameBuffer(fb);
314309
q->bind(config.inColor, 0);
315-
TextureHandle texAccum = provTex(config.provisionals, Stringizer() + "luminanceAccumulation" + config.cameraId, Vec2i(1), 1, GL_R32F);
310+
TextureHandle texAccum = provTex(config.provisionals, Stringizer() + "luminanceAccumulation_" + config.cameraId, Vec2i(1), 1, GL_R32F);
316311
q->bind(texAccum, 1);
317312
Holder<ShaderProgram> shader = config.assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/effects/luminanceApply.glsl"))->get(0);
318313
q->bind(shader);

sources/libsimple/gameloop.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ namespace cage
833833
return +engineData->guiBus;
834834
}
835835

836-
ProvisionalGraphics *engineProvisonalGraphics()
836+
ProvisionalGraphics *engineProvisionalGraphics()
837837
{
838838
return +engineData->provisionalGraphics;
839839
}

sources/libsimple/graphics.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ namespace cage
135135
TextureHandle initializeTarget(const String &prefix, Vec2i resolution)
136136
{
137137
const String name = Stringizer() + prefix + "_" + resolution;
138-
TextureHandle tex = engineProvisonalGraphics()->texture(name,
138+
TextureHandle tex = engineProvisionalGraphics()->texture(name,
139139
[resolution](Texture *tex)
140140
{
141141
tex->initialize(resolution, 1, GL_RGB8);
@@ -174,15 +174,15 @@ namespace cage
174174

175175
void initialize() // opengl thread
176176
{
177-
renderQueue = newRenderQueue("engine", engineProvisonalGraphics());
177+
renderQueue = newRenderQueue("engine", engineProvisionalGraphics());
178178
onDemand = newAssetsOnDemand(engineAssets());
179179
}
180180

181181
void finalize() // opengl thread
182182
{
183183
onDemand->clear(); // make sure to release all assets, but keep the structure to allow remaining calls to enginePurgeAssetsOnDemandCache
184184
renderQueue.clear();
185-
engineProvisonalGraphics()->purge();
185+
engineProvisionalGraphics()->purge();
186186
}
187187

188188
void emit(uint64 emitTime) // control thread
@@ -266,7 +266,7 @@ namespace cage
266266
else
267267
{
268268
CAGE_ASSERT(!windowTarget);
269-
windowTarget = initializeTarget(data.name, data.resolution);
269+
windowTarget = initializeTarget(Stringizer() + "windowTarget_" + data.name, data.resolution);
270270
data.target = windowTarget;
271271
}
272272
data.finalProduct = !cam.target;
@@ -307,13 +307,13 @@ namespace cage
307307
if (dynamicResolution != 1)
308308
data.effects.effects &= ~ScreenSpaceEffectsFlags::AntiAliasing;
309309
data.effects.gamma = Real(confRenderGamma);
310-
data.name = Stringizer() + "vr_camera_" + index;
310+
data.name = Stringizer() + "vrCamera_" + index;
311311
data.resolution = applyDynamicResolution(it.resolution);
312312
data.transform = transformByVrOrigin(cfg.scene, it.transform, cfg.interpolationFactor);
313313
data.projection = it.projection;
314314
data.lodSelection.screenSize = perspectiveScreenSize(it.verticalFov, data.resolution[1]);
315315
data.lodSelection.center = it.primary ? vrFrame->pose().position : it.transform.position;
316-
data.target = initializeTarget(data.name, data.resolution);
316+
data.target = initializeTarget(Stringizer() + "vrTarget_" + data.name, data.resolution);
317317
data.finalProduct = true;
318318
vrTargets.push_back(data.target);
319319
cameras.push_back(std::move(data));
@@ -370,7 +370,7 @@ namespace cage
370370
Holder<Model> modelSquare = engineAssets()->get<AssetSchemeIndexModel, Model>(HashString("cage/model/square.obj"));
371371
CAGE_ASSERT(modelSquare);
372372
modelSquare->bind();
373-
Holder<FrameBuffer> renderTarget = engineProvisonalGraphics()->frameBufferDraw("vr_blit")->resolve();
373+
Holder<FrameBuffer> renderTarget = engineProvisionalGraphics()->frameBufferDraw("vr_blit")->resolve();
374374
renderTarget->bind();
375375
frame->acquireTextures();
376376
CAGE_ASSERT(frame->cameras.size() == targets.size());
@@ -419,7 +419,7 @@ namespace cage
419419
const Vec2i windowResolution = engineWindow()->resolution();
420420
RenderPipelineConfig cfg;
421421
cfg.assets = engineAssets();
422-
cfg.provisionalGraphics = engineProvisonalGraphics();
422+
cfg.provisionalGraphics = engineProvisionalGraphics();
423423
cfg.scene = +eb.scene;
424424
cfg.onDemand = +onDemand;
425425
const uint64 period = controlThread().updatePeriod();
@@ -457,7 +457,7 @@ namespace cage
457457
void dispatch() // opengl thread
458458
{
459459
renderQueue->dispatch();
460-
engineProvisonalGraphics()->reset();
460+
engineProvisionalGraphics()->reset();
461461

462462
{ // check gl errors (even in release, but do not halt the game)
463463
try

sources/libsimple/guiInWorld.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace cage
3232
{
3333
GuiManagerCreateConfig cfg;
3434
cfg.assetManager = engineAssets();
35-
cfg.provisionalGraphics = +engineProvisonalGraphics();
35+
cfg.provisionalGraphics = +engineProvisionalGraphics();
3636
cfg.tooltipsEnabled = config.tooltipsEnabled;
3737
guiMan = newGuiManager(cfg);
3838
guiMan->outputResolution(config.resolution);
@@ -152,7 +152,7 @@ namespace cage
152152
}
153153

154154
// retrieve this every frame so that it is not removed
155-
auto fb = engineProvisonalGraphics()->frameBufferDraw(Stringizer() + "gui-in-world-framebuffer-" + (uintPtr)this);
155+
auto fb = engineProvisionalGraphics()->frameBufferDraw(Stringizer() + "gui-in-world-framebuffer-" + (uintPtr)this);
156156

157157
Holder<RenderQueue> qq;
158158
{
@@ -162,7 +162,7 @@ namespace cage
162162
if (!qq)
163163
return;
164164

165-
Holder<RenderQueue> q = newRenderQueue("gui in world", engineProvisonalGraphics());
165+
Holder<RenderQueue> q = newRenderQueue("gui in world", engineProvisionalGraphics());
166166
{
167167
auto name = q->namedScope("gui in world");
168168
q->bind(fb);

0 commit comments

Comments
 (0)