Skip to content

Commit

Permalink
Renderer updates
Browse files Browse the repository at this point in the history
* Imagine: Add low-level vertex/index buffer API to allow for future optimizations
* Imagine: Optimize Text::compile() by calculating all geometry in a vertex buffer
* Imagine: Add alignment in TextLayoutConfig
* Imagine: Add begin()/end() to Buffer class
* Imagine: Remove RendererCommands::bindTempVertexBuffer() and replace with new vertex buffer API
* Imagine: Add implicit TextureSpan to TextureBinding conversion
* Imagine: Optimize LGradient to use a single triangle strip
  • Loading branch information
Robert Broglia committed Oct 17, 2023
1 parent 256d5ec commit 5e093a8
Show file tree
Hide file tree
Showing 78 changed files with 1,048 additions and 680 deletions.
1 change: 1 addition & 0 deletions EmuFramework/include/emuframework/ButtonConfigView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private:
IG::WindowRect unbindB, cancelB;
Gfx::Text text;
Gfx::Text unbind, cancel;
Gfx::VertexBuffer<Gfx::IQuad::Vertex> rectVerts;
SetDelegate onSetD;
const Input::Device &dev;
const Input::Device *savedDev{};
Expand Down
1 change: 0 additions & 1 deletion EmuFramework/include/emuframework/EmuApp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include <imagine/base/PerformanceHintManager.hh>
#include <imagine/audio/Manager.hh>
#include <imagine/gfx/Renderer.hh>
#include <imagine/gfx/Vec3.hh>
#include <imagine/data-type/image/PixmapReader.hh>
#include <imagine/data-type/image/PixmapWriter.hh>
#include <imagine/font/Font.hh>
Expand Down
5 changes: 4 additions & 1 deletion EmuFramework/include/emuframework/EmuVideoLayer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <emuframework/VideoImageOverlay.hh>
#include <emuframework/VideoImageEffect.hh>
#include <imagine/gfx/GfxSprite.hh>
#include <imagine/gfx/Buffer.hh>
#include <imagine/gfx/Vec3.hh>
#include <imagine/pixmap/PixelFormat.hh>
#include <imagine/util/container/ArrayList.hh>
Expand All @@ -36,6 +37,7 @@ public:
EmuVideoLayer(EmuVideo &video, float defaultAspectRatio);
void place(IG::WindowRect viewRect, IG::WindowRect displayRect, EmuInputView *inputView, EmuSystem &sys);
void draw(Gfx::RendererCommands &cmds);
void setRendererTask(Gfx::RendererTask &);
void setFormat(EmuSystem &, IG::PixelFormat videoFmt, IG::PixelFormat effectFmt, Gfx::ColorSpace);
void setOverlay(ImageOverlayId id);
void setOverlayIntensity(float intensity);
Expand All @@ -62,7 +64,8 @@ private:
IG::StaticArrayList<VideoImageEffect*, 1> effects;
EmuVideo &video;
VideoImageEffect userEffect;
Gfx::Sprite disp;
Gfx::VertexBuffer<Gfx::Sprite::Vertex> spriteVerts;
Gfx::TextureSpan videoTex;
IG::WindowRect contentRect_;
Gfx::Vec3 brightness{1.f, 1.f, 1.f};
Gfx::Vec3 brightnessSrgb{1.f, 1.f, 1.f};
Expand Down
2 changes: 2 additions & 0 deletions EmuFramework/include/emuframework/EmuView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <imagine/gui/View.hh>
#include <imagine/time/Time.hh>
#include <imagine/gfx/GfxText.hh>
#include <imagine/gfx/GeomQuad.hh>

namespace EmuEx
{
Expand Down Expand Up @@ -54,6 +55,7 @@ private:
struct FrameTimeStatsUI
{
Gfx::Text text;
Gfx::VertexBuffer<Gfx::IQuad::Vertex> bgVerts;
WRect rect{};
};
IG_UseMemberIf(enableFrameTimeStats, FrameTimeStatsUI, frameTimeStats);
Expand Down
2 changes: 1 addition & 1 deletion EmuFramework/include/emuframework/EmuViewController.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class MainMenuView;
class EmuMenuViewStack : public ViewStack
{
public:
EmuMenuViewStack(EmuApp &app): emuAppPtr{&app} {}
EmuMenuViewStack(ViewAttachParams, EmuApp &);
bool inputEvent(const Input::Event &) final;
constexpr EmuApp &app() { return *emuAppPtr; }

Expand Down
1 change: 1 addition & 0 deletions EmuFramework/include/emuframework/InputManagerView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public:

private:
Gfx::Text text;
Gfx::VertexBuffer<Gfx::IQuad::Vertex> rectVerts;
};

class InputManagerView final: public TableView, public EmuAppHelper<InputManagerView>
Expand Down
5 changes: 4 additions & 1 deletion EmuFramework/include/emuframework/LoadProgressView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ private:
MessagePortType msgPort{"LoadProgressView"};
EmuApp::CreateSystemCompleteDelegate onComplete;
Gfx::Text text;
Gfx::VertexBuffer<Gfx::IQuad::Vertex> progessBarVerts;
Input::Event originalEvent;
int pos{}, max{};
int pos{}, max{1};

void updateProgressRect();
};

}
30 changes: 20 additions & 10 deletions EmuFramework/include/emuframework/VController.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <imagine/input/DragTracker.hh>
#include <imagine/gfx/GfxSprite.hh>
#include <imagine/gfx/Texture.hh>
#include <imagine/gfx/Buffer.hh>
#include <imagine/util/variant.hh>
#include <vector>
#include <span>
Expand Down Expand Up @@ -86,7 +87,7 @@ public:
constexpr VControllerDPad(std::span<const KeyInfo, 4> keys):
config{.keys{keys[0], keys[1], keys[2], keys[3]}} {}
constexpr VControllerDPad(const Config &config): config{config} {}
void setImage(Gfx::TextureSpan);
void setImage(Gfx::RendererTask &, Gfx::TextureSpan);
void drawButtons(Gfx::RendererCommands &__restrict__) const;
void drawBounds(Gfx::RendererCommands &__restrict__) const;
void setShowBounds(Gfx::Renderer &r, bool on);
Expand Down Expand Up @@ -114,12 +115,15 @@ public:
}

protected:
Gfx::LitSprite spr;
Gfx::LitSprite mapSpr;
Gfx::VertexBuffer<Gfx::LitSprite::Vertex> spriteVerts;
Gfx::TextureSpan tex;
Gfx::Texture mapImg;
WRect padBaseArea, padArea;
int deadzonePixels{};
int btnSizePixels{};
float alpha{};

void updateSprite();
public:
Config config;
bool isHighlighted[4]{};
Expand All @@ -143,8 +147,8 @@ public:
using KbMap = std::array<KeyInfo, KEY_ROWS * KEY_COLS>;

constexpr VControllerKeyboard() = default;
void updateImg(Gfx::Renderer &r);
void setImg(Gfx::Renderer &r, Gfx::TextureSpan img);
void updateImg();
void setImg(Gfx::RendererTask &, Gfx::TextureSpan);
void place(int btnSize, int yOffset, WRect viewBounds);
void draw(Gfx::RendererCommands &__restrict__) const;
int getInput(WPt c) const;
Expand All @@ -166,7 +170,9 @@ public:
bool shiftIsActive() const;

protected:
Gfx::Sprite spr;
Gfx::VertexBuffer<Gfx::Sprite::Vertex> spriteVerts;
Gfx::VertexBuffer<Gfx::IQuad::Vertex> rectVerts;
Gfx::TextureSpan kbTex;
WRect bound;
int keyXSize{}, keyYSize{};
WRect selected{{-1, -1}, {-1, -1}};
Expand All @@ -182,10 +188,9 @@ public:
constexpr VControllerButton(KeyInfo key): key{key} {}
void setPos(WPt pos, WRect viewBounds, _2DOrigin = C2DO);
void setSize(WSize size, WSize extendedSize = {});
void setImage(Gfx::TextureSpan, int aR = 1);
void setImage(Gfx::RendererTask &, Gfx::TextureSpan, int aR = 1);
WRect bounds() const { return bounds_; }
WRect realBounds() const { return extendedBounds_; }
const auto &sprite() const { return spr; }
void drawBounds(Gfx::RendererCommands &__restrict__) const;
void drawSprite(Gfx::RendererCommands &__restrict__) const;
std::string name(const EmuApp &) const;
Expand All @@ -199,10 +204,15 @@ public:
}

protected:
Gfx::LitSprite spr;
Gfx::VertexBuffer<Gfx::LitSprite::Vertex> spriteVerts;
Gfx::VertexBuffer<Gfx::IQuad::Vertex> rectVerts;
Gfx::TextureSpan tex;
Gfx::Color spriteColor;
WRect bounds_{};
WRect extendedBounds_{};
int aspectRatio{1};

void updateSprite();
public:
Gfx::Color color{};
KeyInfo key{};
Expand Down Expand Up @@ -601,7 +611,7 @@ private:
const Window *win{};
const WindowData *winData{};
const Gfx::GlyphTextureSet *facePtr{};
VControllerKeyboard kb{};
VControllerKeyboard kb;
std::vector<VControllerElement> gpElements{};
std::vector<VControllerElement> uiElements{};
std::span<const KeyCode> disabledKeys{};
Expand Down
5 changes: 4 additions & 1 deletion EmuFramework/include/emuframework/VideoImageEffect.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include <imagine/gfx/Texture.hh>
#include <imagine/gfx/Program.hh>
#include <imagine/gfx/Buffer.hh>
#include <imagine/gfx/GfxSprite.hh>
#include <imagine/util/enum.hh>
#include <optional>

Expand Down Expand Up @@ -52,11 +54,12 @@ public:
void setSampler(Gfx::TextureSamplerConfig);
Gfx::Program &program();
Gfx::Texture &renderTarget();
void drawRenderTarget(Gfx::RendererCommands &, const Gfx::TextureSpan);
void drawRenderTarget(Gfx::RendererCommands &, Gfx::TextureSpan);
constexpr IG::PixelFormat imageFormat() const { return format; }
operator bool() const { return (bool)prog; }

private:
Gfx::VertexBuffer<Gfx::Sprite::Vertex> spriteVerts;
Gfx::Texture renderTarget_;
Gfx::Program prog;
int srcTexelDeltaU{};
Expand Down
5 changes: 3 additions & 2 deletions EmuFramework/include/emuframework/VideoImageOverlay.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <imagine/gfx/GfxSprite.hh>
#include <imagine/gfx/Texture.hh>
#include <imagine/gfx/Buffer.hh>
#include <imagine/util/enum.hh>
#include <array>

Expand All @@ -40,7 +41,7 @@ public:
constexpr VideoImageOverlay() = default;
void setEffect(Gfx::Renderer &, ImageOverlayId, Gfx::ColorSpace);
void setIntensity(float intensity);
void place(const Gfx::Sprite &, WRect contentRect, WSize videoPixels, Rotation);
void place(WRect contentRect, WSize videoPixels, Rotation);
void draw(Gfx::RendererCommands &cmds, Gfx::Vec3 brightness);

private:
Expand All @@ -52,7 +53,7 @@ private:
using Sprite = Gfx::SpriteBase<Vertex>;

Gfx::Texture img;
Sprite spr;
Gfx::VertexBuffer<Sprite::Vertex> spriteVerts;
float intensity = 0.75f;
ImageOverlayId overlayId{};
bool multiplyBlend{};
Expand Down
1 change: 1 addition & 0 deletions EmuFramework/src/EmuApp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ void EmuApp::mainInitCommon(IG::ApplicationInitParams initParams, IG::Applicatio
emuVideo.setRendererTask(renderer.task());
emuVideo.setTextureBufferMode(system(), (Gfx::TextureBufferMode)optionTextureBufferMode.val);
emuVideo.setImageBuffers(optionVideoImageBuffers);
emuVideoLayer.setRendererTask(renderer.task());
emuVideoLayer.setLinearFilter(optionImgFilter); // init the texture sampler before setting format
applyRenderPixelFormat();
emuVideoLayer.setOverlay((ImageOverlayId)optionOverlayEffect.val);
Expand Down
21 changes: 15 additions & 6 deletions EmuFramework/src/EmuVideoLayer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <imagine/base/Window.hh>
#include <imagine/gfx/Renderer.hh>
#include <imagine/gfx/RendererCommands.hh>
#include <imagine/gfx/Vec3.hh>
#include <imagine/glm/common.hpp>
#include <imagine/glm/gtc/color_space.hpp>
#include <imagine/logger/logger.h>
#include <algorithm>
Expand Down Expand Up @@ -136,7 +138,7 @@ void EmuVideoLayer::place(IG::WindowRect viewRect, IG::WindowRect displayRect, E
contentRect_.setPos(displayRect.center() + WPt{landscapeOffset, 0}, C2DO);
}
contentRect_.fitIn(displayRect);
disp.setPos(contentRect_);
Gfx::Sprite::write(spriteVerts, 0, { .bounds = contentRect_.as<int16_t>(), .rotation = rotation }, videoTex);
logMsg("placed game rect, at pixels %d:%d:%d:%d",
contentRect_.x, contentRect_.y, contentRect_.x2, contentRect_.y2);
}
Expand All @@ -145,6 +147,8 @@ void EmuVideoLayer::place(IG::WindowRect viewRect, IG::WindowRect displayRect, E

void EmuVideoLayer::draw(Gfx::RendererCommands &cmds)
{
if(!videoTex)
return;
using namespace IG::Gfx;
bool srgbOutput = srgbColorSpace();
auto c = srgbOutput ? brightnessSrgb : brightness;
Expand All @@ -169,13 +173,18 @@ void EmuVideoLayer::draw(Gfx::RendererCommands &cmds)
}
if(srgbOutput)
cmds.setSrgbFramebufferWrite(true);
disp.draw(cmds, cmds.basicEffect());
cmds.basicEffect().drawSprite(cmds, spriteVerts, 0, videoTex);
video.addFence(cmds);
vidImgOverlay.draw(cmds, c);
if(srgbOutput)
cmds.setSrgbFramebufferWrite(false);
}

void EmuVideoLayer::setRendererTask(Gfx::RendererTask &task)
{
spriteVerts = {task, {.size = 4}};
}

void EmuVideoLayer::setFormat(EmuSystem &sys, IG::PixelFormat videoFmt, IG::PixelFormat effectFmt, Gfx::ColorSpace colorSpace)
{
if(colSpace != colorSpace)
Expand Down Expand Up @@ -210,7 +219,7 @@ void EmuVideoLayer::setOverlayIntensity(float intensity)

void EmuVideoLayer::placeOverlay()
{
vidImgOverlay.place(disp, contentRect(), video.size(), rotation);
vidImgOverlay.place(contentRect(), video.size(), rotation);
}

void EmuVideoLayer::setEffectFormat(IG::PixelFormat fmt)
Expand Down Expand Up @@ -268,7 +277,7 @@ void EmuVideoLayer::onVideoFormatChanged(IG::PixelFormat effectFmt)
void EmuVideoLayer::setRotation(IG::Rotation r)
{
rotation = r;
disp.setUVBounds(disp.unitTexCoordRect(), r);
Gfx::Sprite::write(spriteVerts, 0, {.bounds = contentRect_.as<int16_t>(), .rotation = rotation}, videoTex);
placeOverlay();
}

Expand Down Expand Up @@ -333,12 +342,12 @@ void EmuVideoLayer::updateSprite()
{
if(effects.size())
{
disp.set(effects.back()->renderTarget(), rotation);
videoTex = effects.back()->renderTarget();
video.setSampler(Gfx::SamplerConfigs::noLinearNoMipClamp);
}
else
{
disp.set(video.image(), rotation);
videoTex = video.image();
video.setSampler(samplerConfig());
}
}
Expand Down
8 changes: 5 additions & 3 deletions EmuFramework/src/VideoImageEffect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ static PixelFormat effectFormat(IG::PixelFormat format, Gfx::ColorSpace colSpace

VideoImageEffect::VideoImageEffect(Gfx::Renderer &r, Id effect, IG::PixelFormat fmt, Gfx::ColorSpace colSpace,
Gfx::TextureSamplerConfig samplerConf, WSize size):
spriteVerts{r.mainTask, {.size = 4}},
inputImgSize{size}, format{effectFormat(fmt, colSpace)}, colorSpace{colSpace}
{
Gfx::Sprite::write(spriteVerts, 0, { .bounds = {{-1, -1}, {1, 1}}});
logMsg("compiling effect:%s", effectName(effect));
compile(r, effectDesc(effect), samplerConf);
}
Expand Down Expand Up @@ -232,11 +234,11 @@ Gfx::Texture &VideoImageEffect::renderTarget()
return renderTarget_;
}

void VideoImageEffect::drawRenderTarget(Gfx::RendererCommands &cmds, const Gfx::TextureSpan span)
void VideoImageEffect::drawRenderTarget(Gfx::RendererCommands &cmds, Gfx::TextureSpan texSpan)
{
cmds.setViewport(renderTargetImgSize);
Gfx::Sprite spr{{{-1, -1}, {1, 1}}, {span.texturePtr, {{}, {1.f, 1.f}}}};
spr.draw(cmds);
cmds.set(texSpan);
cmds.drawQuad(spriteVerts, 0);
}

void VideoImageEffect::setSampler(Gfx::TextureSamplerConfig samplerConf)
Expand Down

0 comments on commit 5e093a8

Please sign in to comment.