Browse Source

switch to SDL_LoadFile for reading shader sources

main
cinnaboot 4 years ago
parent
commit
613ded9b4e
  1. 34
      src/shader.cpp

34
src/shader.cpp

@ -5,8 +5,6 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iostream>
#include <string>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
@ -21,18 +19,24 @@
// NOTE: forward declarations // NOTE: forward declarations
const std::string dumpTextFile(const char* filepath);
bool parseShader(MemoryArena* arena, GLContext* gl_ctx, ShaderProgram* s); bool parseShader(MemoryArena* arena, GLContext* gl_ctx, ShaderProgram* s);
void loadDummyShader(); void loadDummyShader();
void initCTXSizes(GLContext* gl_ctx); void initCTXSizes(GLContext* gl_ctx);
bool compileAndLinkShader(ShaderProgram* shader, bool compileAndLinkShader(ShaderProgram* shader,
const char* vert_src, const char* vert_src,
const char* frag_src, const char* frag_src,
GLuint& vs_id, GLuint& vs_id,
GLuint& fs_id); GLuint& fs_id);
u32 getGLTypeSize(GLenum e); u32 getGLTypeSize(GLenum e);
GLTexture* getFreeGLTexture(GLContext* gl_ctx); GLTexture* getFreeGLTexture(GLContext* gl_ctx);
bool loadGLTexture(Texture* image, GLuint& tex_id); bool loadGLTexture(Texture* image, GLuint& tex_id);
void* getMeshData(const Mesh& m, const GLBufferToAttribMapping& mapping); void* getMeshData(const Mesh& m, const GLBufferToAttribMapping& mapping);
@ -92,12 +96,12 @@ addShaderProgram(MemoryArena* arena,
s->name = arenaCopyCStr(arena, name); s->name = arenaCopyCStr(arena, name);
s->hash = hash; s->hash = hash;
// TODO: replace std::string w/ utilAllocateCStr() ? size_t vs_size, fs_size;
std::string vert = dumpTextFile(vs); const char* v_str = (const char*) SDL_LoadFile(vs, &vs_size);
std::string frag = dumpTextFile(fs); const char* f_str = (const char*) SDL_LoadFile(fs, &fs_size);
GLuint vs_id, fs_id; GLuint vs_id, fs_id;
if (compileAndLinkShader(s, vert.c_str(), frag.c_str(), vs_id, fs_id)) { if (compileAndLinkShader(s, v_str, f_str, vs_id, fs_id)) {
s->node_xform_id = glGetUniformLocation(s->prog_id, "node_xform"); s->node_xform_id = glGetUniformLocation(s->prog_id, "node_xform");
// TODO: add sampler uniform detection in dynamic shader parsing // TODO: add sampler uniform detection in dynamic shader parsing
s->sampler_id = glGetUniformLocation(s->prog_id, "sampler"); s->sampler_id = glGetUniformLocation(s->prog_id, "sampler");
@ -430,7 +434,7 @@ compileAndLinkShader(ShaderProgram* shader,
GLuint& vs_id, GLuint& vs_id,
GLuint& fs_id) GLuint& fs_id)
{ {
if (strlen(vert_src) > 0 && strlen(frag_src) > 0) { if (vert_src && frag_src && strlen(vert_src) > 0 && strlen(frag_src) > 0) {
vs_id = glCreateShader(GL_VERTEX_SHADER); vs_id = glCreateShader(GL_VERTEX_SHADER);
fs_id = glCreateShader(GL_FRAGMENT_SHADER); fs_id = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vs_id, 1, &vert_src, NULL); glShaderSource(vs_id, 1, &vert_src, NULL);
@ -474,20 +478,6 @@ compileAndLinkShader(ShaderProgram* shader,
return false; return false;
} }
const std::string
dumpTextFile(const char* filepath)
{
std::ifstream fs { filepath };
std::string s {
std::istreambuf_iterator<char>(fs),
std::istreambuf_iterator<char>()};
if (!fs || !fs.good())
std::cout << "error reading file, " << filepath << "\n";
return s;
}
void void
loadDummyShader() loadDummyShader()
{ {

Loading…
Cancel
Save