Skip to content

Commit

Permalink
Widgets|UI|libappfw: Draw GUI widgets with fewer draw calls
Browse files Browse the repository at this point in the history
Most GUI widgets now get batched when drawing.

Added a special-purpose “batch.guiwidget” shader for drawing GUI
widgets.
  • Loading branch information
skyjake committed Feb 19, 2017
1 parent e4d1661 commit 7ef7087
Show file tree
Hide file tree
Showing 46 changed files with 928 additions and 239 deletions.
2 changes: 2 additions & 0 deletions doomsday/apps/client/include/ui/home/columnwidget.h
Expand Up @@ -57,6 +57,8 @@ class ColumnWidget : public de::GuiWidget
void mouseActivity(QObject const *columnWidget);

protected:
//void glInit() override;
//void glDeinit() override;
void updateStyle() override;

private:
Expand Down
2 changes: 1 addition & 1 deletion doomsday/apps/client/include/ui/widgets/taskbarwidget.h
Expand Up @@ -46,7 +46,7 @@ class TaskBarWidget : public de::GuiWidget
de::Rule const &shift();

// Events.
void viewResized();
//void viewResized();
void update();
void drawContent();
bool handleEvent(de::Event const &event);
Expand Down
Expand Up @@ -10,6 +10,7 @@

@include <shaders/generic.dei>
@include <shaders/model.dei>
@include <shaders/batch.dei>
@include <shaders/fx/blur.dei>
@include <shaders/fx/bloom.dei>
@include <shaders/fx/post.dei>
Expand Down
@@ -0,0 +1,57 @@
batch {
# Shader for UI widgets.
shader guiwidget {
vertex = "
uniform highp mat4 uMvpMatrix;
uniform highp vec4 uColor[DENG_MAX_BATCH_UNIFORMS];
uniform highp float uSaturation[DENG_MAX_BATCH_UNIFORMS];
uniform highp vec4 uScissorRect[DENG_MAX_BATCH_UNIFORMS];

attribute highp vec4 aVertex;
attribute highp vec2 aUV;
attribute highp vec4 aColor;
attribute highp float aIndex; // uColor

varying highp vec2 vUV;
varying highp vec4 vColor;
varying highp vec4 vScissor;
varying highp float vSaturation;

void main(void) {
gl_Position = uMvpMatrix * aVertex;
vUV = aUV;

int index = int(aIndex);
vColor = aColor * uColor[index];
vScissor = uScissorRect[index];
vSaturation = uSaturation[index];
}"
include.fragment <include/hsv.glsl>
fragment = "
uniform sampler2D uTex;

varying highp vec2 vUV;
varying highp vec4 vColor;
varying highp vec4 vScissor;
varying highp float vSaturation;

void main(void) {
// Check the scissor first.
if (gl_FragCoord.x < vScissor.x || gl_FragCoord.x > vScissor.z ||
gl_FragCoord.y < vScissor.y || gl_FragCoord.y > vScissor.w) {
discard;
}
gl_FragColor = texture2D(uTex, vUV);

// Optionally adjust color saturation.
if (vSaturation < 1.0) {
highp vec4 hsv = rgbToHsv(gl_FragColor);
hsv.y *= vSaturation;
gl_FragColor = hsvToRgb(hsv);
}

// Final vertex color.
gl_FragColor *= vColor;
}"
}
}
36 changes: 31 additions & 5 deletions doomsday/apps/client/src/ui/home/columnwidget.cpp
Expand Up @@ -64,7 +64,7 @@ DENG_GUI_PIMPL(ColumnWidget)
return update;
}

void glMakeGeometry(DefaultVertexBuf::Builder &verts, Rectanglef const &rect) override
void glMakeGeometry(GuiVertexBuilder &verts, Rectanglef const &rect) override
{
if (!allocId().isNone())
{
Expand Down Expand Up @@ -97,14 +97,15 @@ DENG_GUI_PIMPL(ColumnWidget)
Rule const *maxContentWidth = nullptr;
Vector4f backTintColor;

GLUniform uSaturation { "uSaturation", GLUniform::Float }; // background saturation
//GLProgram bgProgram;
//GLUniform uSaturation { "uSaturation", GLUniform::Float }; // background saturation
//GLUniform uBgColor { "uColor", GLUniform::Vec4 };
Animation backSaturation { 0.f, Animation::Linear };

Impl(Public *i) : Base(i)
{
back = new LabelWidget;
back->setShaderId("generic.textured.hsv.color_ucolor");
back->shaderProgram() << uSaturation;
//back->setCustomShader(&bgProgram);
back->margins().setZero();

scrollArea = new ScrollAreaWidget;
Expand All @@ -124,6 +125,19 @@ DENG_GUI_PIMPL(ColumnWidget)
{
releaseRef(maxContentWidth);
}

/*void glInit()
{
root().shaders().build(bgProgram, "generic.textured.hsv.color_ucolor")
<< uSaturation
<< uBgColor
<< root().uAtlas();
}
void glDeinit()
{
bgProgram.clear();
}*/
};

ColumnWidget::ColumnWidget(String const &name)
Expand Down Expand Up @@ -210,9 +224,21 @@ void ColumnWidget::update()
{
GuiWidget::update();

d->uSaturation = d->backSaturation;
d->back->setSaturation(d->backSaturation);
/*d->uSaturation = d->backSaturation;
d->uBgColor = Vector4f(1, 1, 1, visibleOpacity());*/
}

/*void ColumnWidget::glInit()
{
d->glInit();
}
void ColumnWidget::glDeinit()
{
d->bgProgram.clear();
}*/

void ColumnWidget::updateStyle()
{
GuiWidget::updateStyle();
Expand Down
2 changes: 2 additions & 0 deletions doomsday/apps/client/src/ui/widgets/busywidget.cpp
Expand Up @@ -122,6 +122,7 @@ void BusyWidget::drawContent()

if (Con_TransitionInProgress())
{
root().painter().flush();
GLState::push()
.setViewport(Rectangleui::fromSize(GLState::current().target().size()))
.apply();
Expand All @@ -140,6 +141,7 @@ void BusyWidget::drawContent()

if (d->haveTransitionFrame())
{
root().painter().flush();
GLState::push()
.setAlphaTest(false)
.setBlend(false)
Expand Down
1 change: 1 addition & 0 deletions doomsday/apps/client/src/ui/widgets/gamewidget.cpp
Expand Up @@ -228,6 +228,7 @@ void GameWidget::drawContent()
if (isDisabled() || !GL_IsFullyInited() || !App_GameLoaded())
return;

root().painter().flush();
GLState::push();

Rectanglei pos;
Expand Down
42 changes: 23 additions & 19 deletions doomsday/apps/client/src/ui/widgets/taskbarwidget.cpp
Expand Up @@ -56,6 +56,7 @@
#include <de/Drawable>
#include <de/GLBuffer>
#include <de/KeyEvent>
#include <de/Painter>
#include <de/PopupMenuWidget>
#include <de/SequentialLayout>
#include <de/SignalAction>
Expand Down Expand Up @@ -120,10 +121,11 @@ DENG_GUI_PIMPL(TaskBarWidget)
int maxSpace;

// GL objects:
Drawable drawable;
GLUniform uMvpMatrix;
GLUniform uColor;
Matrix4f projMatrix;
//GuiVertexBuilder verts;
//Drawable drawable;
//GLUniform uMvpMatrix;
//GLUniform uColor;
//Matrix4f projMatrix;

Impl(Public *i)
: Base(i)
Expand All @@ -136,10 +138,10 @@ DENG_GUI_PIMPL(TaskBarWidget)
, configMenu(0)
, multiMenu(0)
, mouseWasTrappedWhenOpening(false)
, uMvpMatrix("uMvpMatrix", GLUniform::Mat4)
, uColor ("uColor", GLUniform::Vec4)
//, uMvpMatrix("uMvpMatrix", GLUniform::Mat4)
//, uColor ("uColor", GLUniform::Vec4)
{
uColor = Vector4f(1, 1, 1, 1);
//uColor = Vector4f(1, 1, 1, 1);
self().set(Background(style().colors().colorf("background")));

vertShift = new AnimationRule(0);
Expand Down Expand Up @@ -206,18 +208,19 @@ DENG_GUI_PIMPL(TaskBarWidget)

void glInit()
{
drawable.addBuffer(new VertexBuf);
//drawable.addBuffer(new VertexBuf);

shaders().build(drawable.program(), "generic.color_ucolor")
/*shaders().build(drawable.program(), "generic.color_ucolor")
<< uMvpMatrix
<< uColor;
updateProjection();
updateProjection();*/
}

void glDeinit()
{
drawable.clear();
//drawable.clear();
//verts.clear();
}

void updateLogoButtonText()
Expand Down Expand Up @@ -245,22 +248,23 @@ DENG_GUI_PIMPL(TaskBarWidget)
logo->setText(text);
}

void updateProjection()
/*void updateProjection()
{
uMvpMatrix = root().projMatrix2D();
}
}*/

void updateGeometry()
{
/*
Rectanglei pos;
if (self().hasChangedPlace(pos) || self().geometryRequested())
{
self().requestGeometry(false);
VertexBuf::Builder verts;
verts.clear();
self().glMakeGeometry(verts);
drawable.buffer<VertexBuf>().setVertices(gl::TriangleStrip, verts, gl::Static);
}
//drawable.buffer<VertexBuf>().setVertices(gl::TriangleStrip, verts, gl::Static);
}*/
}

GuiWidget &itemWidget(PopupMenuWidget *menu, uint pos) const
Expand Down Expand Up @@ -560,11 +564,11 @@ void TaskBarWidget::glDeinit()
d->glDeinit();
}

void TaskBarWidget::viewResized()
/*void TaskBarWidget::viewResized()
{
GuiWidget::viewResized();
d->updateProjection();
}
//d->updateProjection();
}*/

void TaskBarWidget::update()
{
Expand Down
1 change: 1 addition & 0 deletions doomsday/sdk/libappfw/include/de/Painter
@@ -0,0 +1 @@
#include "framework/painter.h"
Expand Up @@ -106,7 +106,7 @@ class LIBAPPFW_PUBLIC AtlasProceduralImage : public ProceduralImage
release();
}

void glMakeGeometry(DefaultVertexBuf::Builder &verts, Rectanglef const &rect)
void glMakeGeometry(GuiVertexBuilder &verts, Rectanglef const &rect)
{
if (_atlas)
{
Expand Down
Expand Up @@ -35,11 +35,6 @@ namespace de {
* the ChildWidgetOrganizer::IWidgetFactory interface. Also, third parties
* may observe widget creation and updates and alter the widget as they choose.
*
* @todo Virtualization: it is not required that all the items of the context
* are represented by widgets on screen at the same time. In contexts with
* large numbers of items, virtualization should be applied to keep only a
* subset/range of items present as widgets.
*
* @ingroup appfw
*/
class LIBAPPFW_PUBLIC ChildWidgetOrganizer
Expand Down Expand Up @@ -125,7 +120,7 @@ class LIBAPPFW_PUBLIC ChildWidgetOrganizer
void setVirtualizationEnabled(bool enabled);

/**
* Enables or disables child recycling. Deleted children will be put up for
* Enables or disables child recycling. Deleted children will be put up for
* recycling instead of being deleted, and new children will first be taken
* from the set of old recycled widgets.
*
Expand Down
9 changes: 5 additions & 4 deletions doomsday/sdk/libappfw/include/de/framework/gltextcomposer.h
Expand Up @@ -23,6 +23,7 @@
#include <de/Font>
#include <de/Atlas>
#include <de/GLBuffer>
#include <de/Painter>

#include "../ui/defs.h"
#include "../FontLineWrapping"
Expand All @@ -40,10 +41,10 @@ namespace de {
*/
class LIBAPPFW_PUBLIC GLTextComposer : public Asset
{
public:
/*public:
typedef Vertex2TexRgba Vertex;
typedef GLBufferT<Vertex> VertexBuf;
typedef VertexBuf::Builder Vertices;
typedef VertexBuf::Builder Vertices;*/

public:
GLTextComposer();
Expand Down Expand Up @@ -89,7 +90,7 @@ class LIBAPPFW_PUBLIC GLTextComposer : public Asset
*/
void releaseLinesOutsideRange();

void makeVertices(Vertices &triStrip,
void makeVertices(GuiVertexBuilder &triStrip,
Vector2i const &topLeft,
ui::Alignment const &lineAlign,
Vector4f const &color = Vector4f(1, 1, 1, 1));
Expand All @@ -104,7 +105,7 @@ class LIBAPPFW_PUBLIC GLTextComposer : public Asset
* @param lineAlign Horizontal alignment for each line within the paragraph.
* @param color Vertex color for the generated vertices.
*/
void makeVertices(Vertices &triStrip,
void makeVertices(GuiVertexBuilder &triStrip,
Rectanglei const &rect,
ui::Alignment const &alignInRect,
ui::Alignment const &lineAlign,
Expand Down
5 changes: 4 additions & 1 deletion doomsday/sdk/libappfw/include/de/framework/guirootwidget.h
Expand Up @@ -30,8 +30,9 @@

namespace de {

class GuiWidget;
class FocusWidget;
class GuiWidget;
class Painter;

/**
* Graphical root widget.
Expand Down Expand Up @@ -75,6 +76,8 @@ class LIBAPPFW_PUBLIC GuiRootWidget : public RootWidget

static GLShaderBank &shaders();

Painter &painter();

/**
* Returns the default projection for 2D graphics.
*/
Expand Down

0 comments on commit 7ef7087

Please sign in to comment.