Browse Source

Shaders: assume drawOffset is 0 if DRAW_COUNT is 1.

This is always true in the single-draw case, since setDrawOffset()
asserts on this. In the multi-draw case this optimization doesn't make
sense, because it doesn't make sense to create a multidraw shader with
just one draw.

On an Intel 630 GPU this resulted in single-draw single-material Phong
to go from 550 ms to 440, which is roughly a 20% improvement. For the
simpler shaders the difference is even higher. The multidraw numbers
stayed the same as before, obviously.
pull/518/head
Vladimír Vondruš 5 years ago
parent
commit
c92d608aa7
  1. 4
      src/Magnum/Shaders/DistanceFieldVector.frag
  2. 4
      src/Magnum/Shaders/DistanceFieldVectorGL.cpp
  3. 4
      src/Magnum/Shaders/DistanceFieldVectorGL.h
  4. 4
      src/Magnum/Shaders/Flat.frag
  5. 4
      src/Magnum/Shaders/Flat.vert
  6. 4
      src/Magnum/Shaders/FlatGL.cpp
  7. 4
      src/Magnum/Shaders/FlatGL.h
  8. 4
      src/Magnum/Shaders/MeshVisualizer.frag
  9. 4
      src/Magnum/Shaders/MeshVisualizer.vert
  10. 6
      src/Magnum/Shaders/MeshVisualizerGL.cpp
  11. 7
      src/Magnum/Shaders/MeshVisualizerGL.h
  12. 4
      src/Magnum/Shaders/Phong.frag
  13. 4
      src/Magnum/Shaders/Phong.vert
  14. 4
      src/Magnum/Shaders/PhongGL.cpp
  15. 4
      src/Magnum/Shaders/PhongGL.h
  16. 4
      src/Magnum/Shaders/Vector.frag
  17. 4
      src/Magnum/Shaders/Vector.vert
  18. 4
      src/Magnum/Shaders/VectorGL.cpp
  19. 4
      src/Magnum/Shaders/VectorGL.h
  20. 4
      src/Magnum/Shaders/VertexColor.vert
  21. 4
      src/Magnum/Shaders/VertexColorGL.cpp
  22. 4
      src/Magnum/Shaders/VertexColorGL.h

4
src/Magnum/Shaders/DistanceFieldVector.frag

@ -72,6 +72,7 @@ uniform lowp float smoothness
#else
#ifndef MULTI_DRAW
#if DRAW_COUNT > 1
#ifdef EXPLICIT_UNIFORM_LOCATION
layout(location = 0)
#endif
@ -80,6 +81,9 @@ uniform highp uint drawOffset
= 0u
#endif
;
#else
#define drawOffset 0u
#endif
#define drawId drawOffset
#endif

4
src/Magnum/Shaders/DistanceFieldVectorGL.cpp

@ -165,7 +165,7 @@ template<UnsignedInt dimensions> DistanceFieldVectorGL<dimensions>::DistanceFiel
{
#ifndef MAGNUM_TARGET_GLES2
if(flags >= Flag::UniformBuffers) {
_drawOffsetUniform = uniformLocation("drawOffset");
if(_drawCount > 1) _drawOffsetUniform = uniformLocation("drawOffset");
} else
#endif
{
@ -280,7 +280,7 @@ template<UnsignedInt dimensions> DistanceFieldVectorGL<dimensions>& DistanceFiel
"Shaders::DistanceFieldVectorGL::setDrawOffset(): the shader was not created with uniform buffers enabled", *this);
CORRADE_ASSERT(offset < _drawCount,
"Shaders::DistanceFieldVectorGL::setDrawOffset(): draw offset" << offset << "is out of bounds for" << _drawCount << "draws", *this);
setUniform(_drawOffsetUniform, offset);
if(_drawCount > 1) setUniform(_drawOffsetUniform, offset);
return *this;
}

4
src/Magnum/Shaders/DistanceFieldVectorGL.h

@ -445,7 +445,9 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT DistanceFieldVector
* @ref bindTransformationProjectionBuffer(), @ref bindDrawBuffer() and
* @ref bindTextureTransformationBuffer() should be used for current
* draw. Expects that @ref Flag::UniformBuffers is set and @p offset is
* less than @ref drawCount(). Initial value is @cpp 0 @ce.
* less than @ref drawCount(). Initial value is @cpp 0 @ce, if
* @ref drawCount() is @cpp 1 @ce, the function is a no-op as the
* shader assumes draw offset to be always zero.
*
* If @ref Flag::MultiDraw is set, @glsl gl_DrawID @ce is added to this
* value, which makes each draw submitted via

4
src/Magnum/Shaders/Flat.frag

@ -72,6 +72,7 @@ uniform highp uint objectId; /* defaults to zero */
#else
#ifndef MULTI_DRAW
#if DRAW_COUNT > 1
#ifdef EXPLICIT_UNIFORM_LOCATION
layout(location = 0)
#endif
@ -80,6 +81,9 @@ uniform highp uint drawOffset
= 0u
#endif
;
#else
#define drawOffset 0u
#endif
#define drawId drawOffset
#endif

4
src/Magnum/Shaders/Flat.vert

@ -92,6 +92,7 @@ uniform highp uint textureLayer; /* defaults to zero */
/* Uniform buffers */
#else
#if DRAW_COUNT > 1
#ifdef EXPLICIT_UNIFORM_LOCATION
layout(location = 0)
#endif
@ -100,6 +101,9 @@ uniform highp uint drawOffset
= 0u
#endif
;
#else
#define drawOffset 0u
#endif
layout(std140
#ifdef EXPLICIT_BINDING

4
src/Magnum/Shaders/FlatGL.cpp

@ -216,7 +216,7 @@ template<UnsignedInt dimensions> FlatGL<dimensions>::FlatGL(const Flags flags
{
#ifndef MAGNUM_TARGET_GLES2
if(flags >= Flag::UniformBuffers) {
_drawOffsetUniform = uniformLocation("drawOffset");
if(_drawCount > 1) _drawOffsetUniform = uniformLocation("drawOffset");
} else
#endif
{
@ -342,7 +342,7 @@ template<UnsignedInt dimensions> FlatGL<dimensions>& FlatGL<dimensions>::setDraw
"Shaders::FlatGL::setDrawOffset(): the shader was not created with uniform buffers enabled", *this);
CORRADE_ASSERT(offset < _drawCount,
"Shaders::FlatGL::setDrawOffset(): draw offset" << offset << "is out of bounds for" << _drawCount << "draws", *this);
setUniform(_drawOffsetUniform, offset);
if(_drawCount > 1) setUniform(_drawOffsetUniform, offset);
return *this;
}

4
src/Magnum/Shaders/FlatGL.h

@ -755,7 +755,9 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT FlatGL: public GL::
* @ref bindTransformationProjectionBuffer(), @ref bindDrawBuffer() and
* @ref bindTextureTransformationBuffer() should be used for current
* draw. Expects that @ref Flag::UniformBuffers is set and @p offset is
* less than @ref drawCount(). Initial value is @cpp 0 @ce.
* less than @ref drawCount(). Initial value is @cpp 0 @ce, if
* @ref drawCount() is @cpp 1 @ce, the function is a no-op as the
* shader assumes draw offset to be always zero.
*
* If @ref Flag::MultiDraw is set, @glsl gl_DrawID @ce is added to this
* value, which makes each draw submitted via

4
src/Magnum/Shaders/MeshVisualizer.frag

@ -111,6 +111,7 @@ uniform lowp vec2 colorMapOffsetScale
#else
#ifndef MULTI_DRAW
#if DRAW_COUNT > 1
#ifdef EXPLICIT_UNIFORM_LOCATION
layout(location = 1)
#endif
@ -119,6 +120,9 @@ uniform highp uint drawOffset
= 0u
#endif
;
#else
#define drawOffset 0u
#endif
#define drawId drawOffset
#endif

4
src/Magnum/Shaders/MeshVisualizer.vert

@ -113,6 +113,7 @@ uniform highp float lineLength
/* Uniform buffers */
#else
#if DRAW_COUNT > 1
#ifdef EXPLICIT_UNIFORM_LOCATION
layout(location = 1)
#endif
@ -121,6 +122,9 @@ uniform highp uint drawOffset
= 0u
#endif
;
#else
#define drawOffset 0u
#endif
#ifdef TWO_DIMENSIONS
layout(std140

6
src/Magnum/Shaders/MeshVisualizerGL.cpp

@ -263,7 +263,7 @@ MeshVisualizerGLBase& MeshVisualizerGLBase::setDrawOffset(const UnsignedInt offs
"Shaders::MeshVisualizerGL::setDrawOffset(): the shader was not created with uniform buffers enabled", *this);
CORRADE_ASSERT(offset < _drawCount,
"Shaders::MeshVisualizerGL::setDrawOffset(): draw offset" << offset << "is out of bounds for" << _drawCount << "draws", *this);
setUniform(_drawOffsetUniform, offset);
if(_drawCount > 1) setUniform(_drawOffsetUniform, offset);
return *this;
}
@ -426,7 +426,7 @@ MeshVisualizerGL2D::MeshVisualizerGL2D(const Flags flags
#ifndef MAGNUM_TARGET_GLES2
if(flags >= Flag::UniformBuffers) {
_drawOffsetUniform = uniformLocation("drawOffset");
if(_drawCount > 1) _drawOffsetUniform = uniformLocation("drawOffset");
} else
#endif
{
@ -757,7 +757,7 @@ MeshVisualizerGL3D::MeshVisualizerGL3D(const Flags flags
#ifndef MAGNUM_TARGET_GLES2
if(flags >= Flag::UniformBuffers) {
_drawOffsetUniform = uniformLocation("drawOffset");
if(_drawCount > 1) _drawOffsetUniform = uniformLocation("drawOffset");
} else
#endif
{

7
src/Magnum/Shaders/MeshVisualizerGL.h

@ -495,7 +495,9 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizerGL2D: public Implementation::MeshVisua
* @ref bindTransformationProjectionBuffer() and @ref bindDrawBuffer()
* should be used for current draw. Expects that
* @ref Flag::UniformBuffers is set and @p offset is less than
* @ref drawCount(). Initial value is @cpp 0 @ce.
* @ref drawCount(). Initial value is @cpp 0 @ce, if @ref drawCount()
* is @cpp 1 @ce, the function is a no-op as the shader assumes draw
* offset to be always zero.
*
* If @ref Flag::MultiDraw is set, @glsl gl_DrawID @ce is added to this
* value, which makes each draw submitted via
@ -1490,7 +1492,8 @@ class MAGNUM_SHADERS_EXPORT MeshVisualizerGL3D: public Implementation::MeshVisua
* @ref bindTransformationBuffer() and @ref bindDrawBuffer() should be
* used for current draw. Expects that @ref Flag::UniformBuffers is set
* and @p offset is less than @ref drawCount(). Initial value is
* @cpp 0 @ce.
* @cpp 0 @ce, if @ref drawCount() is @cpp 1 @ce, the function is a
* no-op as the shader assumes draw offset to be always zero.
*
* If @ref Flag::MultiDraw is set, @glsl gl_DrawID @ce is added to this
* value, which makes each draw submitted via

4
src/Magnum/Shaders/Phong.frag

@ -149,6 +149,7 @@ uniform lowp float lightRanges[LIGHT_COUNT]
#else
#ifndef MULTI_DRAW
#if DRAW_COUNT > 1
#ifdef EXPLICIT_UNIFORM_LOCATION
layout(location = 0)
#endif
@ -157,6 +158,9 @@ uniform highp uint drawOffset
= 0u
#endif
;
#else
#define drawOffset 0u
#endif
#define drawId drawOffset
#endif

4
src/Magnum/Shaders/Phong.vert

@ -114,6 +114,7 @@ uniform highp vec4 lightPositions[LIGHT_COUNT]
/* Uniform buffers */
#else
#if DRAW_COUNT > 1
#ifdef EXPLICIT_UNIFORM_LOCATION
layout(location = 0)
#endif
@ -122,6 +123,9 @@ uniform highp uint drawOffset
= 0u
#endif
;
#else
#define drawOffset 0u
#endif
/* Keep in sync with Phong.frag. Can't "outsource" to a common file because
the #extension directive needs to be always before any code. */

4
src/Magnum/Shaders/PhongGL.cpp

@ -326,7 +326,7 @@ PhongGL::PhongGL(const Flags flags, const UnsignedInt lightCount
{
#ifndef MAGNUM_TARGET_GLES2
if(flags >= Flag::UniformBuffers) {
_drawOffsetUniform = uniformLocation("drawOffset");
if(_drawCount > 1) _drawOffsetUniform = uniformLocation("drawOffset");
} else
#endif
{
@ -706,7 +706,7 @@ PhongGL& PhongGL::setDrawOffset(const UnsignedInt offset) {
"Shaders::PhongGL::setDrawOffset(): the shader was not created with uniform buffers enabled", *this);
CORRADE_ASSERT(offset < _drawCount,
"Shaders::PhongGL::setDrawOffset(): draw offset" << offset << "is out of bounds for" << _drawCount << "draws", *this);
setUniform(_drawOffsetUniform, offset);
if(_drawCount > 1) setUniform(_drawOffsetUniform, offset);
return *this;
}

4
src/Magnum/Shaders/PhongGL.h

@ -1296,7 +1296,9 @@ class MAGNUM_SHADERS_EXPORT PhongGL: public GL::AbstractShaderProgram {
* bound with @ref bindTransformationBuffer(), @ref bindDrawBuffer()
* and @ref bindTextureTransformationBuffer() should be used for
* current draw. Expects that @ref Flag::UniformBuffers is set and
* @p offset is less than @ref drawCount(). Initial value is @cpp 0 @ce.
* @p offset is less than @ref drawCount(). Initial value is
* @cpp 0 @ce, if @ref drawCount() is @cpp 1 @ce, the function is a
* no-op as the shader assumes draw offset to be always zero.
*
* If @ref Flag::MultiDraw is set, @glsl gl_DrawID @ce is added to this
* value, which makes each draw submitted via

4
src/Magnum/Shaders/Vector.frag

@ -54,6 +54,7 @@ uniform lowp vec4 color
#else
#ifndef MULTI_DRAW
#if DRAW_COUNT > 1
#ifdef EXPLICIT_UNIFORM_LOCATION
layout(location = 0)
#endif
@ -62,6 +63,9 @@ uniform highp uint drawOffset
= 0u
#endif
;
#else
#define drawOffset 0u
#endif
#define drawId drawOffset
#endif

4
src/Magnum/Shaders/Vector.vert

@ -76,6 +76,7 @@ uniform mediump mat3 textureMatrix
/* Uniform buffers */
#else
#if DRAW_COUNT > 1
#ifdef EXPLICIT_UNIFORM_LOCATION
layout(location = 0)
#endif
@ -84,6 +85,9 @@ uniform highp uint drawOffset
= 0u
#endif
;
#else
#define drawOffset 0u
#endif
layout(std140
#ifdef EXPLICIT_BINDING

4
src/Magnum/Shaders/VectorGL.cpp

@ -164,7 +164,7 @@ template<UnsignedInt dimensions> VectorGL<dimensions>::VectorGL(const Flags flag
{
#ifndef MAGNUM_TARGET_GLES2
if(flags >= Flag::UniformBuffers) {
_drawOffsetUniform = uniformLocation("drawOffset");
if(_drawCount > 1) _drawOffsetUniform = uniformLocation("drawOffset");
} else
#endif
{
@ -257,7 +257,7 @@ template<UnsignedInt dimensions> VectorGL<dimensions>& VectorGL<dimensions>::set
"Shaders::VectorGL::setDrawOffset(): the shader was not created with uniform buffers enabled", *this);
CORRADE_ASSERT(offset < _drawCount,
"Shaders::VectorGL::setDrawOffset(): draw offset" << offset << "is out of bounds for" << _drawCount << "draws", *this);
setUniform(_drawOffsetUniform, offset);
if(_drawCount > 1) setUniform(_drawOffsetUniform, offset);
return *this;
}

4
src/Magnum/Shaders/VectorGL.h

@ -401,7 +401,9 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT VectorGL: public GL
* @ref bindDrawBuffer() and @ref bindTextureTransformationBuffer()
* should be used for current draw. Expects that
* @ref Flag::UniformBuffers is set and @p offset is less than
* @ref drawCount(). Initial value is @cpp 0 @ce.
* @ref drawCount(). Initial value is @cpp 0 @ce, if @ref drawCount()
* is @cpp 1 @ce, the function is a no-op as the shader assumes draw
* offset to be always zero.
*
* If @ref Flag::MultiDraw is set, @glsl gl_DrawID @ce is added to this
* value, which makes each draw submitted via

4
src/Magnum/Shaders/VertexColor.vert

@ -65,6 +65,7 @@ uniform highp mat4 transformationProjectionMatrix
/* Uniform buffers */
#else
#if DRAW_COUNT > 1
#ifdef EXPLICIT_UNIFORM_LOCATION
layout(location = 0)
#endif
@ -73,6 +74,9 @@ uniform highp uint drawOffset
= 0u
#endif
;
#else
#define drawOffset 0u
#endif
layout(std140
#ifdef EXPLICIT_BINDING

4
src/Magnum/Shaders/VertexColorGL.cpp

@ -144,7 +144,7 @@ template<UnsignedInt dimensions> VertexColorGL<dimensions>::VertexColorGL(const
{
#ifndef MAGNUM_TARGET_GLES2
if(flags >= Flag::UniformBuffers) {
_drawOffsetUniform = uniformLocation("drawOffset");
if(_drawCount > 1) _drawOffsetUniform = uniformLocation("drawOffset");
} else
#endif
{
@ -194,7 +194,7 @@ template<UnsignedInt dimensions> VertexColorGL<dimensions>& VertexColorGL<dimens
"Shaders::VertexColorGL::setDrawOffset(): the shader was not created with uniform buffers enabled", *this);
CORRADE_ASSERT(offset < _drawCount,
"Shaders::VertexColorGL::setDrawOffset(): draw offset" << offset << "is out of bounds for" << _drawCount << "draws", *this);
setUniform(_drawOffsetUniform, offset);
if(_drawCount > 1) setUniform(_drawOffsetUniform, offset);
return *this;
}

4
src/Magnum/Shaders/VertexColorGL.h

@ -331,7 +331,9 @@ template<UnsignedInt dimensions> class MAGNUM_SHADERS_EXPORT VertexColorGL: publ
* @ref TransformationProjectionUniform3D buffers bound with
* @ref bindTransformationProjectionBuffer() should be used for current
* draw. Expects that @ref Flag::UniformBuffers is set and @p offset is
* less than @ref drawCount(). Initial value is @cpp 0 @ce.
* less than @ref drawCount(). Initial value is @cpp 0 @ce, if
* @ref drawCount() is @cpp 1 @ce, the function is a no-op as the
* shader assumes draw offset to be always zero.
*
* If @ref Flag::MultiDraw is set, @glsl gl_DrawID @ce is added to this
* value, which makes each draw submitted via

Loading…
Cancel
Save