Browse Source

Shaders: use StringViews to make code generation a bit less shitty.

Also it seems the reserve() call was wrong.
pull/470/head
Vladimír Vondruš 6 years ago
parent
commit
35cb06fe1e
  1. 17
      src/Magnum/Shaders/Phong.cpp

17
src/Magnum/Shaders/Phong.cpp

@ -30,6 +30,7 @@
#endif #endif
#include <Corrade/Containers/EnumSet.hpp> #include <Corrade/Containers/EnumSet.hpp>
#include <Corrade/Containers/Reference.h> #include <Corrade/Containers/Reference.h>
#include <Corrade/Containers/StringView.h>
#include <Corrade/Utility/FormatStl.h> #include <Corrade/Utility/FormatStl.h>
#include <Corrade/Utility/Resource.h> #include <Corrade/Utility/Resource.h>
@ -77,18 +78,16 @@ Phong::Phong(const Flags flags, const UnsignedInt lightCount): _flags{flags}, _l
#ifndef MAGNUM_TARGET_GLES #ifndef MAGNUM_TARGET_GLES
std::string lightInitializer; std::string lightInitializer;
if(lightCount) { if(lightCount) {
using namespace Containers::Literals;
/* Initializer for the light color array -- we need a list of vec4(1.0) /* Initializer for the light color array -- we need a list of vec4(1.0)
joined by commas. For GLES we'll simply upload the values directly. */ joined by commas. For GLES we'll simply upload the values directly. */
constexpr const char lightInitializerPreamble[] = "#define LIGHT_COLOR_INITIALIZER "; constexpr Containers::StringView lightInitializerPreamble = "#define LIGHT_COLOR_INITIALIZER "_s;
constexpr std::size_t lightInitializerPreambleSize = constexpr Containers::StringView lightInitializerItem = "vec4(1.0), "_s;
Containers::arraySize(lightInitializerPreamble) - 1; lightInitializer.reserve(lightInitializerPreamble.size() + lightCount*lightInitializerItem.size());
constexpr const char lightInitializerItem[] = "vec4(1.0), "; lightInitializer.append(lightInitializerPreamble.data(), lightInitializerPreamble.size());
constexpr std::size_t lightInitializerItemSize =
Containers::arraySize(lightInitializerItem) - 1;
lightInitializer.reserve(Containers::arraySize(lightInitializerPreamble) - 1 + lightCount*lightInitializerItemSize);
lightInitializer.append(lightInitializerPreamble, lightInitializerPreambleSize);
for(std::size_t i = 0; i != lightCount; ++i) for(std::size_t i = 0; i != lightCount; ++i)
lightInitializer.append(lightInitializerItem, lightInitializerItemSize); lightInitializer.append(lightInitializerItem.data(), lightInitializerItem.size());
/* Drop the last comma and add a newline at the end */ /* Drop the last comma and add a newline at the end */
lightInitializer[lightInitializer.size() - 2] = '\n'; lightInitializer[lightInitializer.size() - 2] = '\n';

Loading…
Cancel
Save