Shader.cpp
Go to the documentation of this file.
1 #include "quori_face/Shader.hpp"
2 
3 #include <GL/gl3w.h>
4 #include <GLFW/glfw3.h>
5 #include <cstring>
6 #include <unordered_map>
7 #include <iostream>
8 
9 
10 #include "quori_face/gl.hpp"
11 
12 #include "trace.hpp"
13 
14 using namespace quori_face;
15 
16 namespace
17 {
18  const static std::unordered_map<std::uint8_t, GLenum> TYPE_MAP {
19  { static_cast<std::uint8_t>(Shader::Type::Vertex), GL_VERTEX_SHADER },
20  { static_cast<std::uint8_t>(Shader::Type::Fragment), GL_FRAGMENT_SHADER },
21  };
22 }
23 
25 {
27 }
28 
29 Shader::Ptr Shader::compile(const Shader::Type type, const std::uint8_t *const buffer, const std::size_t len)
30 {
31  const auto it = TYPE_MAP.find(static_cast<std::uint8_t>(type));
32  if (it == TYPE_MAP.cend())
33  {
34  throw std::runtime_error("Unknown shader type");
35  }
36 
37  const std::uint32_t handle = QUORI_FACE_TRACE(glCreateShader(it->second));
38  checkGlError();
39 
40  const GLchar *string[1] = { reinterpret_cast<const GLchar *>(buffer) };
41  const GLint length[1] = { (GLint)len };
42 
43  QUORI_FACE_TRACE(glShaderSource(handle, 1, string, length));
44  checkGlError();
45 
47  checkGlError();
48 
49  GLint success = GL_TRUE;
50 
52  checkGlError();
53  if (success == GL_FALSE)
54  {
55  GLint length = 0;
57  char *buffer = new char[length];
60  delete[] buffer;
61  throw std::runtime_error(message);
62  }
63 
64  return Ptr(new Shader(handle));
65 }
66 
68 {
69  return compile(type, reinterpret_cast<const std::uint8_t *>(str.data()), str.size());
70 }
71 
72 std::uint32_t Shader::getHandle() const
73 {
74  return handle_;
75 }
76 
77 
78 Shader::Shader(const std::uint32_t handle)
79  : handle_(handle)
80 {
81 
82 }
buffer
GLuint buffer
Definition: glcorearb.h:636
glShaderSource
#define glShaderSource
Definition: gl3w.h:1181
GL_FALSE
#define GL_FALSE
Definition: glcorearb.h:75
quori_face::Shader::Shader
Shader(const std::uint32_t handle)
Definition: Shader.cpp:78
quori_face::Shader::getHandle
std::uint32_t getHandle() const
Definition: Shader.cpp:72
GL_FRAGMENT_SHADER
#define GL_FRAGMENT_SHADER
Definition: glcorearb.h:709
glDeleteShader
#define glDeleteShader
Definition: gl3w.h:838
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:790
glGetProgramInfoLog
#define glGetProgramInfoLog
Definition: gl3w.h:959
GLchar
char GLchar
Definition: glcorearb.h:672
glGetShaderiv
#define glGetShaderiv
Definition: gl3w.h:988
glGetProgramiv
#define glGetProgramiv
Definition: gl3w.h:969
trace.hpp
quori_face::Shader::~Shader
~Shader()
Definition: Shader.cpp:24
quori_face::Shader::compile
static Ptr compile(const Type type, const std::uint8_t *const buffer, const std::size_t length)
Definition: Shader.cpp:29
quori_face
Definition: Cache.hpp:9
Shader.hpp
gl3w.h
GL_INFO_LOG_LENGTH
#define GL_INFO_LOG_LENGTH
Definition: glcorearb.h:740
quori_face::Shader::Type::Vertex
@ Vertex
quori_face::Shader::handle_
std::uint32_t handle_
Definition: Shader.hpp:57
quori_face::Shader::Type
Type
Definition: Shader.hpp:14
glCreateShader
#define glCreateShader
Definition: gl3w.h:822
QUORI_FACE_TRACE
#define QUORI_FACE_TRACE(stmt)
Definition: quori_face/trace.hpp:16
quori_face::checkGlError
void checkGlError()
Throws a std::runtime_error if the OpenGL error flag is set.
Definition: gl.cpp:47
glCompileShader
#define glCompileShader
Definition: gl3w.h:794
GL_VERTEX_SHADER
#define GL_VERTEX_SHADER
Definition: glcorearb.h:710
gl.hpp
GL_TRUE
#define GL_TRUE
Definition: glcorearb.h:76
message
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2498
quori_face::Shader::Type::Fragment
@ Fragment
len
GLenum GLenum GLsizei len
Definition: glcorearb.h:4219
GL_COMPILE_STATUS
#define GL_COMPILE_STATUS
Definition: glcorearb.h:737
type
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:256
length
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:771
GLint
int GLint
Definition: glcorearb.h:65
quori_face::Shader::Ptr
std::shared_ptr< Shader > Ptr
Definition: Shader.hpp:20


quori_face
Author(s):
autogenerated on Wed Mar 2 2022 00:53:20