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

Loading…
Cancel
Save