Skip to content

Commit

Permalink
Make osc::Shader take std::string_view rather than CStringView
Browse files Browse the repository at this point in the history
adamkewley committed Jan 8, 2025
1 parent 9ea996d commit 22f9a27
Showing 10 changed files with 62 additions and 52 deletions.
40 changes: 20 additions & 20 deletions src/oscar/Graphics/GraphicsImplementation.cpp
Original file line number Diff line number Diff line change
@@ -178,7 +178,7 @@ namespace
// it's here, rather than in an external resource file, because it is eagerly
// loaded while the graphics backend is initialized (i.e. potentially before
// the application is fully loaded)
constexpr CStringView c_quad_vertex_shader_src = R"(
constexpr std::string_view c_quad_vertex_shader_src = R"(
#version 330 core
layout (location = 0) in vec3 aPos;
@@ -198,7 +198,7 @@ namespace
// it's here, rather than in an external resource file, because it is eagerly
// loaded while the graphics backend is initialized (i.e. potentially before
// the application is fully loaded)
constexpr CStringView c_quad_fragment_shader_src = R"(
constexpr std::string_view c_quad_fragment_shader_src = R"(
#version 330 core
uniform sampler2D uTexture;
@@ -2783,22 +2783,22 @@ std::ostream& osc::operator<<(std::ostream& o, const RenderTexture&)
namespace
{
gl::Program compile_program_with_shimming(
CStringView vertex_shader_src,
CStringView fragment_shader_src,
std::optional<CStringView> geometry_shader_src = std::nullopt)
std::string_view vertex_shader_src,
std::string_view fragment_shader_src,
std::optional<std::string_view> geometry_shader_src = std::nullopt)
{
#ifndef EMSCRIPTEN
if (geometry_shader_src) {
return gl::create_program_from(
gl::compile_from_source<gl::VertexShader>(vertex_shader_src.c_str()),
gl::compile_from_source<gl::FragmentShader>(fragment_shader_src.c_str()),
gl::compile_from_source<gl::GeometryShader>(geometry_shader_src->c_str())
gl::compile_from_source<gl::VertexShader>(vertex_shader_src),
gl::compile_from_source<gl::FragmentShader>(fragment_shader_src),
gl::compile_from_source<gl::GeometryShader>(*geometry_shader_src)
);
}
else {
return gl::create_program_from(
gl::compile_from_source<gl::VertexShader>(vertex_shader_src.c_str()),
gl::compile_from_source<gl::FragmentShader>(fragment_shader_src.c_str())
gl::compile_from_source<gl::VertexShader>(vertex_shader_src),
gl::compile_from_source<gl::FragmentShader>(fragment_shader_src)
);
}
#else
@@ -2860,18 +2860,18 @@ namespace
class osc::Shader::Impl final {
public:
Impl(
CStringView vertex_shader_src,
CStringView fragment_shader_src) :
std::string_view vertex_shader_src,
std::string_view fragment_shader_src) :

program_{compile_program_with_shimming(vertex_shader_src, fragment_shader_src)}
{
parse_uniforms_and_attributes_from_program();
}

Impl(
CStringView vertex_shader_src,
CStringView geometry_shader_src,
CStringView fragment_shader_src) :
std::string_view vertex_shader_src,
std::string_view geometry_shader_src,
std::string_view fragment_shader_src) :

program_{compile_program_with_shimming(vertex_shader_src, fragment_shader_src, geometry_shader_src)}
{
@@ -3025,16 +3025,16 @@ std::ostream& osc::operator<<(std::ostream& o, ShaderPropertyType shader_type)
}

osc::Shader::Shader(
CStringView vertex_shader_src,
CStringView fragment_shader_src) :
std::string_view vertex_shader_src,
std::string_view fragment_shader_src) :

impl_{make_cow<Impl>(vertex_shader_src, fragment_shader_src)}
{}

osc::Shader::Shader(
CStringView vertex_shader_src,
CStringView geometry_shader_src,
CStringView fragment_shader_src) :
std::string_view vertex_shader_src,
std::string_view geometry_shader_src,
std::string_view fragment_shader_src) :

impl_{make_cow<Impl>(vertex_shader_src, geometry_shader_src, fragment_shader_src)}
{}
7 changes: 4 additions & 3 deletions src/oscar/Graphics/Materials/MeshBasicMaterial.cpp
Original file line number Diff line number Diff line change
@@ -2,14 +2,15 @@

#include <oscar/Graphics/Material.h>
#include <oscar/Graphics/Shader.h>
#include <oscar/Utils/CStringView.h>
#include <oscar/Utils/StringName.h>

#include <string_view>

using namespace osc;

namespace
{
constexpr CStringView c_vertex_shader_src = R"(
constexpr std::string_view c_vertex_shader_src = R"(
#version 330 core
uniform mat4 uViewProjMat;
@@ -22,7 +23,7 @@ void main()
gl_Position = uViewProjMat * aModelMat * vec4(aPos, 1.0);
}
)";
constexpr CStringView c_fragment_shader_src = R"(
constexpr std::string_view c_fragment_shader_src = R"(
#version 330 core
uniform vec4 uDiffuseColor;
7 changes: 4 additions & 3 deletions src/oscar/Graphics/Materials/MeshBasicTexturedMaterial.cpp
Original file line number Diff line number Diff line change
@@ -3,13 +3,14 @@
#include <oscar/Graphics/Material.h>
#include <oscar/Graphics/Shader.h>
#include <oscar/Graphics/Texture2D.h>
#include <oscar/Utils/CStringView.h>

#include <string_view>

using namespace osc;

namespace
{
constexpr CStringView c_vertex_shader_src = R"(
constexpr std::string_view c_vertex_shader_src = R"(
#version 330 core
uniform mat4 uModelMat;
@@ -27,7 +28,7 @@ namespace
}
)";

constexpr CStringView c_fragment_shader_src = R"(
constexpr std::string_view c_fragment_shader_src = R"(
#version 330 core
uniform sampler2D uTextureSampler;
6 changes: 3 additions & 3 deletions src/oscar/Graphics/Materials/MeshDepthWritingMaterial.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "MeshDepthWritingMaterial.h"

#include <oscar/Utils/CStringView.h>
#include <string_view>

using namespace osc;

namespace
{
constexpr CStringView c_vertex_shader_src = R"(
constexpr std::string_view c_vertex_shader_src = R"(
#version 330 core
uniform mat4 uViewProjMat;
@@ -19,7 +19,7 @@ void main()
gl_Position = uViewProjMat * aModelMat * vec4(aPos, 1.0);
}
)";
constexpr CStringView c_fragment_shader_src = R"(
constexpr std::string_view c_fragment_shader_src = R"(
#version 330 core
void main() {} // implicitly writes the depth
8 changes: 4 additions & 4 deletions src/oscar/Graphics/Materials/MeshNormalVectorsMaterial.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "MeshNormalVectorsMaterial.h"

#include <oscar/Utils/CStringView.h>
#include <string_view>

using namespace osc;

namespace
{
constexpr CStringView c_vertex_shader_src = R"(
constexpr std::string_view c_vertex_shader_src = R"(
#version 330 core
// draw_normals: program that draws mesh normals
@@ -27,7 +27,7 @@ void main()
vs_out.normal = aNormal;
}
)";
constexpr CStringView c_geometry_shader_src = R"(
constexpr std::string_view c_geometry_shader_src = R"(
#version 330 core
// draw_normals: program that draws mesh normals
@@ -79,7 +79,7 @@ void main()
}
)";
constexpr CStringView c_fragment_shader_src = R"(
constexpr std::string_view c_fragment_shader_src = R"(
#version 330 core
// draw_normals: program that draws mesh normals
7 changes: 4 additions & 3 deletions src/oscar/Graphics/Materials/MeshPhongMaterial.cpp
Original file line number Diff line number Diff line change
@@ -2,14 +2,15 @@

#include <oscar/Graphics/Shader.h>
#include <oscar/Graphics/Material.h>
#include <oscar/Utils/CStringView.h>
#include <oscar/Utils/StringName.h>

#include <string_view>

using namespace osc;

namespace
{
constexpr CStringView c_vertex_shader_src = R"(
constexpr std::string_view c_vertex_shader_src = R"(
#version 330 core
uniform mat4 uModelMat;
@@ -30,7 +31,7 @@ void main()
gl_Position = uViewProjMat * vec4(FragPos, 1.0);
}
)";
constexpr CStringView c_fragment_shader_src = R"(
constexpr std::string_view c_fragment_shader_src = R"(
#version 330 core
uniform vec3 uLightPos;
10 changes: 8 additions & 2 deletions src/oscar/Graphics/OpenGL/Gl.cpp
Original file line number Diff line number Diff line change
@@ -2,13 +2,19 @@

#include <glad/glad.h>

#include <oscar/Utils/Assertions.h>

#include <sstream>
#include <string_view>
#include <stdexcept>
#include <vector>

void osc::gl::compile_from_source(const ShaderHandle& shader_handle, const GLchar* shader_src)
void osc::gl::compile_from_source(const ShaderHandle& shader_handle, std::string_view shader_src)
{
glShaderSource(shader_handle.get(), 1, &shader_src, nullptr);
OSC_ASSERT_ALWAYS(not shader_src.empty() && "empty source code passed to the shader compiler");
const GLchar* shader_src_ptr = shader_src.data();
const GLint shader_src_length = static_cast<GLint>(shader_src.size());
glShaderSource(shader_handle.get(), 1, &shader_src_ptr, &shader_src_length);
glCompileShader(shader_handle.get());

// check for compile errors
5 changes: 3 additions & 2 deletions src/oscar/Graphics/OpenGL/Gl.h
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
#include <ranges>
#include <span>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>

@@ -78,7 +79,7 @@ namespace osc::gl
};

// compile a shader from source
void compile_from_source(const ShaderHandle&, const GLchar* src);
void compile_from_source(const ShaderHandle&, std::string_view src);

// a shader of a particular type (e.g. GL_FRAGMENT_SHADER) that owns a
// shader handle
@@ -103,7 +104,7 @@ namespace osc::gl
class GeometryShader : public Shader<GL_GEOMETRY_SHADER> {};

template<typename TShader>
inline TShader compile_from_source(const GLchar* shader_src)
inline TShader compile_from_source(std::string_view shader_src)
{
TShader rv;
compile_from_source(rv.handle(), shader_src);
13 changes: 7 additions & 6 deletions src/oscar/Graphics/Scene/SceneRenderer.cpp
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@
#include <algorithm>
#include <memory>
#include <span>
#include <string_view>
#include <utility>

using namespace osc::literals;
@@ -145,7 +146,7 @@ namespace
{}

private:
static constexpr CStringView c_vertex_shader_src = R"(
static constexpr std::string_view c_vertex_shader_src = R"(
#version 330 core
uniform mat4 uViewProjMat;
@@ -191,7 +192,7 @@ namespace
}
)";

static constexpr CStringView c_fragment_shader_src = R"(
static constexpr std::string_view c_fragment_shader_src = R"(
#version 330 core
uniform bool uHasShadowMap = false;
@@ -282,7 +283,7 @@ namespace
}

private:
static constexpr CStringView c_vertex_shader_src = R"(
static constexpr std::string_view c_vertex_shader_src = R"(
#version 330 core
uniform mat4 uViewProjMat;
@@ -332,7 +333,7 @@ namespace
}
)";

static constexpr CStringView c_fragment_shader_src = R"(
static constexpr std::string_view c_fragment_shader_src = R"(
#version 330 core
uniform bool uHasShadowMap = false;
@@ -423,7 +424,7 @@ namespace
}

private:
static constexpr CStringView c_vertex_shader_src = R"(
static constexpr std::string_view c_vertex_shader_src = R"(
#version 330 core
uniform mat4 uModelMat;
@@ -443,7 +444,7 @@ namespace
}
)";

static constexpr CStringView c_fragment_shader_src = R"(
static constexpr std::string_view c_fragment_shader_src = R"(
#version 330 core
uniform sampler2D uScreenTexture;
11 changes: 5 additions & 6 deletions src/oscar/Graphics/Shader.h
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@

#include <oscar/Graphics/ShaderPropertyType.h>
#include <oscar/Utils/CopyOnUpdPtr.h>
#include <oscar/Utils/CStringView.h>

#include <cstddef>
#include <iosfwd>
@@ -16,15 +15,15 @@ namespace osc
public:
// throws on compile error
Shader(
CStringView vertex_shader_src,
CStringView fragment_shader_src
std::string_view vertex_shader_src,
std::string_view fragment_shader_src
);

// throws on compile error
Shader(
CStringView vertex_shader_src,
CStringView geometry_shader_src,
CStringView fragment_shader_src
std::string_view vertex_shader_src,
std::string_view geometry_shader_src,
std::string_view fragment_shader_src
);

size_t num_properties() const;

0 comments on commit 22f9a27

Please sign in to comment.