-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_shader.cpp
137 lines (118 loc) · 4.03 KB
/
load_shader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "load_shader.h"
#include "stack_alloc.h"
#include "file_io.h"
#include "profile.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
namespace Shader
{
static GLchar*
ReadASCIIFileIntoMemory(Memory::stack_allocator* const Allocator, const char* FileName,
GLint* Success)
{
debug_read_file_result ReadFile = Platform::ReadEntireFile(Allocator, FileName);
assert(ReadFile.ContentsSize && ReadFile.Contents);
char* LastChar = PushStruct(Allocator, char);
*LastChar = '\0';
return (GLchar*)ReadFile.Contents;
}
static GLuint
ImportShader(Memory::stack_allocator* const Allocator, const char* ShaderPath)
{
char* VertexShaderPath =
(char*)Allocator->Alloc(Memory::SafeTruncate_size_t_To_uint32_t((strlen(ShaderPath) + 6)));
strcpy(VertexShaderPath, ShaderPath);
strcat(VertexShaderPath, ".vert\0");
char* FragmentShaderPath =
(char*)Allocator->Alloc(Memory::SafeTruncate_size_t_To_uint32_t(strlen(ShaderPath) + 6));
strcpy(FragmentShaderPath, ShaderPath);
strcat(FragmentShaderPath, ".frag\0");
int Success = 1;
GLchar InfoLog[512];
GLuint VertexShader;
GLuint FragmentShader;
GLchar* VertexShaderSource = ReadASCIIFileIntoMemory(Allocator, VertexShaderPath, &Success);
if(!Success)
{
printf("Shader at %s could not be read!\n", VertexShaderPath);
return 0;
}
// Setting up shader
VertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(VertexShader, 1, &VertexShaderSource, NULL);
glCompileShader(VertexShader);
static const char* HorizontalSeparator =
"------------------------------------------------------------";
// Checking if shader compiled successfully
glGetShaderiv(VertexShader, GL_COMPILE_STATUS, &Success);
if(!Success)
{
printf("%s\n", HorizontalSeparator);
glGetShaderInfoLog(VertexShader, 512, NULL, InfoLog);
printf("Shader at %s compilation failed!%s", VertexShaderPath, InfoLog);
printf("%s\n", HorizontalSeparator);
return 0;
}
GLchar* FragmentShaderSource = ReadASCIIFileIntoMemory(Allocator, FragmentShaderPath, &Success);
if(!Success)
{
printf("Shader at %s could not be read!\n", FragmentShaderPath);
return 0;
}
// Setting up fragment shader
FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(FragmentShader, 1, &FragmentShaderSource, NULL);
glCompileShader(FragmentShader);
// Checking if fragment shader compiled successfully
glGetShaderiv(FragmentShader, GL_COMPILE_STATUS, &Success);
if(!Success)
{
printf("%s\n", HorizontalSeparator);
glGetShaderInfoLog(FragmentShader, 512, NULL, InfoLog);
printf("Shader file %s compilation error: %s", FragmentShaderPath, InfoLog);
printf("%s\n", HorizontalSeparator);
return 0;
}
// Setting up shader program
GLuint ShaderProgram = glCreateProgram();
glAttachShader(ShaderProgram, VertexShader);
glAttachShader(ShaderProgram, FragmentShader);
// Linking shader program
glLinkProgram(ShaderProgram);
glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &Success);
if(!Success)
{
printf("%s\n", HorizontalSeparator);
glGetProgramInfoLog(ShaderProgram, 512, NULL, InfoLog);
printf("Shader linker error: %s", InfoLog);
printf("%s\n", HorizontalSeparator);
return 0;
}
// Deleting linked shaders
glDeleteShader(VertexShader);
glDeleteShader(FragmentShader);
if(Success)
{
return ShaderProgram;
}
else
{
return 0;
}
}
GLuint
CheckedLoadCompileFreeShader(Memory::stack_allocator* Alloc, const char* RelativePath)
{
BEGIN_TIMED_BLOCK(LoadShader);
Memory::marker LoadStart = Alloc->GetMarker();
GLuint Result = Shader::ImportShader(Alloc, RelativePath);
Alloc->FreeToMarker(LoadStart);
if(Result == 0)
{
printf("Shader %s failed to load correctly!\n", RelativePath);
}
END_TIMED_BLOCK(LoadShader);
return Result;
}
}