Skip to content

Commit

Permalink
dynamic resolution WIP; remove stalling gpu with glFinish
Browse files Browse the repository at this point in the history
  • Loading branch information
malytomas committed Nov 10, 2024
1 parent 7bc7516 commit 298648b
Show file tree
Hide file tree
Showing 22 changed files with 223 additions and 172 deletions.
File renamed without changes.
26 changes: 26 additions & 0 deletions data/cage/shader/engine/blitScaled.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

$include ../shaderConventions.h

$define shader vertex

layout(location = CAGE_SHADER_ATTRIB_IN_POSITION) in vec3 inPosition;
layout(location = CAGE_SHADER_ATTRIB_IN_UV) in vec3 inUv;
out vec2 varUv;

void main()
{
gl_Position = vec4(inPosition.xy * 2 - 1, inPosition.z, 1);
varUv = inUv.xy;
varUv.y = 1 - varUv.y;
}

$define shader fragment

layout(binding = 0) uniform sampler2D texColor;
in vec2 varUv;
out vec4 outColor;

void main()
{
outColor = textureLod(texColor, varUv, 0);
}
5 changes: 3 additions & 2 deletions data/cage/shader/engine/engine.assets
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ engine.pack
[]
scheme = shader
billboard.glsl
blit.glsl
blitPixels.glsl
blitScaled.glsl
decal.glsl
font.glsl
skybox.glsl
standard.glsl
text.glsl
5 changes: 3 additions & 2 deletions data/cage/shader/engine/engine.pack
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[]
blit.glsl
font.glsl
blitPixels.glsl
blitScaled.glsl
standard.glsl
text.glsl
File renamed without changes.
1 change: 0 additions & 1 deletion data/cage/shader/shader.assets
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

[]
scheme = pack
shader.pack
2 changes: 0 additions & 2 deletions data/cage/shader/shader.pack
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

[]
effects/effects.pack
engine/engine.pack
visualize/visualize.pack
gui/gui.pack
14 changes: 0 additions & 14 deletions data/cage/shader/visualize/color.glsl

This file was deleted.

20 changes: 0 additions & 20 deletions data/cage/shader/visualize/depth.glsl

This file was deleted.

14 changes: 0 additions & 14 deletions data/cage/shader/visualize/monochromatic.glsl

This file was deleted.

9 changes: 0 additions & 9 deletions data/cage/shader/visualize/vertex.glsl

This file was deleted.

10 changes: 0 additions & 10 deletions data/cage/shader/visualize/visualize.assets

This file was deleted.

5 changes: 0 additions & 5 deletions data/cage/shader/visualize/visualize.pack

This file was deleted.

4 changes: 2 additions & 2 deletions sources/include/cage-engine/renderQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ namespace cage
{
using Fnc = void(void *);
static Fnc *fnc = +[](void *d) { (*(T *)d)(); };
_customCommand(std::move(data).cast<void>(), fnc);
_customCommand(std::move(data).template cast<void>(), fnc);
}
template<class T>
void customCommand(Holder<T> data, void fnc(T *))
{
using Fnc = void(void *);
_customCommand(std::move(data).cast<void>(), (Fnc *)fnc);
_customCommand(std::move(data).template cast<void>(), (Fnc *)fnc);
}

[[nodiscard]] struct RenderQueueNamedScope namedScope(StringPointer name);
Expand Down
8 changes: 8 additions & 0 deletions sources/include/cage-simple/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ namespace cage
ProvisionalGraphics *engineProvisonalGraphics();
uint64 engineControlTime();

struct EngineDynamicResolution
{
uint32 targetFps = 30;
Real minimumScale = 0.6;
bool enabled = false;
};
EngineDynamicResolution &engineDynamicResolution();

namespace detail
{
void enginePurgeAssetsOnDemandCache();
Expand Down
19 changes: 10 additions & 9 deletions sources/include/cage-simple/statisticsGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ namespace cage
enum class StatisticsGuiFlags : uint32
{
None = 0,
Utilization = 1 << 0, // % main thread cpu use
Control = 1 << 1,
Sound = 1 << 2,
GraphicsPrepare = 1 << 3,
GraphicsDispatch = 1 << 4,
FrameTime = 1 << 5,
DrawCalls = 1 << 6,
DrawPrimitives = 1 << 7,
Entities = 1 << 8,
CpuUtilization = 1 << 0, // % main thread cpu use
DynamicResolution = 1 << 1,
ControlThreadTime = 1 << 2,
SoundThreadTime = 1 << 3,
PrepareThreadTime = 1 << 4,
GpuTime = 1 << 5,
FrameTime = 1 << 6,
DrawCalls = 1 << 7,
DrawPrimitives = 1 << 8,
Entities = 1 << 9,
};
GCHL_ENUM_BITS(StatisticsGuiFlags);

Expand Down
53 changes: 19 additions & 34 deletions sources/libengine/graphics/renderPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ namespace cage
{
const ConfigBool confRenderMissingModels("cage/graphics/renderMissingModels", false);
const ConfigBool confRenderSkeletonBones("cage/graphics/renderSkeletonBones", false);
const ConfigBool confNoAmbientOcclusion("cage/graphics/disableAmbientOcclusion", false);
const ConfigBool confNoBloom("cage/graphics/disableBloom", false);

struct UniViewport
{
Expand Down Expand Up @@ -344,9 +342,9 @@ namespace cage
struct RenderPipelineImpl : RenderPipelineConfig
{
Holder<Model> modelSquare, modelBone;
Holder<ShaderProgram> shaderBlit, shaderDepth, shaderStandard, shaderDepthCutOut, shaderStandardCutOut;
Holder<ShaderProgram> shaderVisualizeColor, shaderVisualizeDepth, shaderVisualizeMonochromatic;
Holder<ShaderProgram> shaderFont;
Holder<ShaderProgram> shaderBlitPixels, shaderBlitScaled;
Holder<ShaderProgram> shaderDepth, shaderStandard, shaderDepthCutOut, shaderStandardCutOut;
Holder<ShaderProgram> shaderText;

Holder<SkeletalAnimationPreparatorCollection> skeletonPreparatorCollection;
EntityComponent *transformComponent = nullptr;
Expand All @@ -362,31 +360,24 @@ namespace cage

modelSquare = assets->get<AssetSchemeIndexModel, Model>(HashString("cage/model/square.obj"));
modelBone = assets->get<AssetSchemeIndexModel, Model>(HashString("cage/model/bone.obj"));
shaderBlit = defaultProgram(assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/engine/blit.glsl")));
CAGE_ASSERT(modelSquare && modelBone);
shaderBlitPixels = assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/engine/blitPixels.glsl"))->get(0);
shaderBlitScaled = assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/engine/blitScaled.glsl"))->get(0);
CAGE_ASSERT(shaderBlitPixels && shaderBlitScaled);
Holder<MultiShaderProgram> standard = assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/engine/standard.glsl"));
shaderStandard = defaultProgram(standard, 0);
shaderStandardCutOut = defaultProgram(standard, HashString("CutOut"));
shaderDepth = defaultProgram(standard, HashString("DepthOnly"));
shaderDepthCutOut = defaultProgram(standard, HashString("DepthOnly") + HashString("CutOut"));
shaderVisualizeColor = defaultProgram(assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/visualize/color.glsl")));
shaderVisualizeDepth = defaultProgram(assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/visualize/depth.glsl")));
shaderVisualizeMonochromatic = defaultProgram(assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/visualize/monochromatic.glsl")));
shaderFont = defaultProgram(assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/engine/font.glsl")));
CAGE_ASSERT(shaderBlit);
CAGE_ASSERT(standard);
shaderDepth = standard->get(HashString("DepthOnly"));
shaderDepthCutOut = standard->get(HashString("DepthOnly") + HashString("CutOut"));
shaderStandard = standard->get(0);
shaderStandardCutOut = standard->get(HashString("CutOut"));
shaderText = assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/engine/text.glsl"))->get(0);

transformComponent = scene->component<TransformComponent>();
prevTransformComponent = scene->componentsByType(detail::typeIndex<TransformComponent>())[1];

skeletonPreparatorCollection = newSkeletalAnimationPreparatorCollection(assets);
}

static Holder<ShaderProgram> defaultProgram(const Holder<MultiShaderProgram> &multi, uint32 variant = 0)
{
if (multi)
return multi->get(variant);
return {};
}

Mat4 modelTransform(Entity *e) const
{
CAGE_ASSERT(e->has(transformComponent));
Expand Down Expand Up @@ -507,8 +498,8 @@ namespace cage
void renderTextImpl(const CameraData &data, const TextPrepare &text) const
{
const Holder<RenderQueue> &renderQueue = data.renderQueue;
renderQueue->uniform(shaderFont, 0, data.viewProj * text.model);
renderQueue->uniform(shaderFont, 4, text.color);
renderQueue->uniform(shaderText, 0, data.viewProj * text.model);
renderQueue->uniform(shaderText, 4, text.color);
text.font->render(+renderQueue, modelSquare, text.glyphs, text.format);
}

Expand Down Expand Up @@ -575,7 +566,7 @@ namespace cage
renderQueue->culling(true);
renderQueue->blending(true);
renderQueue->blendFuncAlphaTransparency();
renderQueue->bind(shaderFont);
renderQueue->bind(shaderText);
for (const TextPrepare &text : layer.texts)
renderTextImpl(data, text);
renderQueue->resetAllState();
Expand Down Expand Up @@ -957,11 +948,6 @@ namespace cage
Holder<RenderQueue> &renderQueue = data.renderQueue;
const auto graphicsDebugScope = renderQueue->namedScope("camera");

if (confNoAmbientOcclusion)
data.effects.effects &= ~ScreenSpaceEffectsFlags::AmbientOcclusion;
if (confNoBloom)
data.effects.effects &= ~ScreenSpaceEffectsFlags::Bloom;

data.model = Mat4(data.transform);
data.view = inverse(data.model);
data.viewProj = data.projection * data.view;
Expand Down Expand Up @@ -1027,8 +1013,7 @@ namespace cage
renderQueue->activeAttachments(renderTarget, 1);
renderQueue->checkFrameBuffer(renderTarget);
renderQueue->viewport(Vec2i(), ssaoResolution);
renderQueue->bind(shaderVisualizeColor);
renderQueue->uniform(shaderVisualizeColor, 0, 1 / Vec2(ssaoResolution));
renderQueue->bind(shaderBlitScaled);
renderQueue->bind(depthTexture, 0);
renderQueue->draw(modelSquare);
renderQueue->resetAllState();
Expand Down Expand Up @@ -1155,7 +1140,7 @@ namespace cage
{
renderQueue->viewport(Vec2i(), data.resolution);
renderQueue->bind(texSource, 0);
renderQueue->bind(shaderBlit);
renderQueue->bind(shaderBlitPixels);
renderQueue->bind(renderTarget);
renderQueue->colorTexture(renderTarget, 0, colorTexture);
renderQueue->activeAttachments(renderTarget, 1);
Expand All @@ -1171,7 +1156,7 @@ namespace cage
renderQueue->resetAllState();
renderQueue->viewport(Vec2i(), data.resolution);
renderQueue->bind(colorTexture, 0);
renderQueue->bind(shaderBlit);
renderQueue->bind(shaderBlitPixels);
renderQueue->bind(renderTarget);
renderQueue->colorTexture(renderTarget, 0, data.target);
renderQueue->depthTexture(renderTarget, {});
Expand Down
4 changes: 2 additions & 2 deletions sources/libsimple/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace cage
void graphicsInitialize(); // opengl thread
void graphicsFinalize(); // opengl thread
void graphicsEmit(uint64 time); // control thread
void graphicsPrepare(uint64 time, uint32 &drawCalls, uint32 &drawPrimitives); // prepare thread
void graphicsPrepare(uint64 time, uint32 &drawCalls, uint32 &drawPrimitives, Real &dynamicResolution); // prepare thread
void graphicsDispatch(); // opengl thread
void graphicsSwap(); // opengl thread
void graphicsSwap(uint64 &gpuTime); // opengl thread

void soundCreate(const EngineCreateConfig &config);
void soundDestroy();
Expand Down
6 changes: 6 additions & 0 deletions sources/libsimple/externs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ namespace cage
static EventDispatcher<bool(const GenericInput &)> instance;
return instance;
}

EngineDynamicResolution &engineDynamicResolution()
{
static EngineDynamicResolution instance;
return instance;
}
}
Loading

0 comments on commit 298648b

Please sign in to comment.