ref: aaf43972b821ab06e955e5dca453412d55e6ef58
parent: bf35146ea4e3278487bb3ed655f182a8a0801c5c
author: Snesrev <snesrev@protonmail.com>
date: Sun Mar 5 21:57:56 EST 2023
Allow whitespec and comments before #version in shaders (Fixes #197)
--- a/glsl_shader.c
+++ b/glsl_shader.c
@@ -212,6 +212,33 @@
free(data_org);
}
+size_t LengthOfInitialComments(const uint8 *data, size_t size) {
+ const uint8 *data_org = data, *data_end = data + size;
+ while (data != data_end) {
+ uint8 c = *data++;
+ if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
+ continue;
+ if (c != '/' || data == data_end) {
+ data--;
+ break;
+ }
+ c = *data++;
+ if (c == '/') {
+ while (data != data_end && *data++ != '\n') {}
+ } else if (c == '*') {
+ do {
+ if (data_end - data < 2)
+ return 0;
+ } while (*data++ != '*' || data[0] != '/');
+ data++;
+ } else {
+ data--;
+ break;
+ }
+ }
+ return data - data_org;
+}
+
static bool GlslPass_Compile(GlslPass *p, uint type, const uint8 *data, size_t size, bool use_opengl_es) {
static const char kVertexPrefix[] = "#define VERTEX\n#define PARAMETER_UNIFORM\n";
static const char kFragmentPrefixCore[] = "#define FRAGMENT\n#define PARAMETER_UNIFORM\n";
@@ -222,6 +249,9 @@
char buffer[256];
GLint compile_status = 0;
size_t skip = 0;
+
+ size_t commsize = LengthOfInitialComments(data, size);
+ data += commsize, size -= commsize;
if (size < 8 || memcmp(data, "#version", 8) != 0) {
if (!use_opengl_es) {