GLSL simple shader results in strange compilation errors - opengl

I have a very simple shader here:
#version 460
layout (location = 0) in vec3 inPos;
layout (location = 0) out vec3 outPos;
void main() {
gl_Position = vec4(inPos.x, inPos.y, inPos.z, 1.0);
outPos = inPos;
}
and compiled the shader using the command:
glslangValidator -V -G -o triangle.vert.spv triangle.vert
and the compiler turned 3 errors:
error : #version: ES shaders for SPIR-V require version 310 or higher
error : Source\Shaders\triangle.vert:1: '€' : unexpected token
error : Source\Shaders\triangle.vert:1: '' : compilation terminated
It seems that the compiler is having trouble reading the very first line of the code. I am very sure I don't include a euro sign in this, and the syntax should be correct.
How should I fix this?

I am using Jetbrains Rider, for me: File Encoding->Remove BOM solves the issue

Related

OpenGL glAttachShader 1281 Error

I am trying to create a OpenGL framework for a game that includes a shader function that parses a vertex Shader and a vertex fragment.
using the OpenGL Call to isolate the error and report a bug break at the line.
#define ASSERT(x) if (!(x)) __debugbreak();
#define GLCall(x) GLClearError();\
x;\
ASSERT(GLLogCall(#x, __FILE__, __LINE__))
when the debug is reported, "[OPENGL Error]: <1281>", I get an error at this code:
GLCall(glAttachShader(program, vs));
which is used in this function:
static unsigned int CreateShader(const std::string& vertexShader, const
std::string& fragmentShader)
{
GLCall(unsigned int program = glCreateProgram());
GLCall(unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader));
GLCall(unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader));
GLCall(glAttachShader(program, vs));
GLCall(glAttachShader(program, fs));
GLCall(glLinkProgram(program));
GLCall(glValidateProgram(program));
GLCall(glDeleteShader(vs));
GLCall(glDeleteShader(fs));
return program;
}
On the console window, it displays:
"Failed to compile shader! vertex"
ERROR: 0:7 'position' : undeclared indentified
ERROR: 0:7 'assign' : cannot convert from 'float' to 'Position 4-component vector of float'"
However, it still produces a blue quad on my screen.
Make sure that your shader is being properly updated whenever you're building your code. Open the build directory and look for the shader file that you're using, and ensure that changes you're making to the source shader are reflected in the build version. A common error with shaders is that they are copied when the files are first created and then not updated.
I am following the same series by TheCherno on YouTube as you, and I've been using CMake as my build system. It copied over the shader file when I first created it, but hasn't since. By adding the below line to my CMakeLists.txt I ensure that if there are any changes to the shader file that CMake will update the file.
configure_file(${PROJECT_SOURCE_DIR}/res/shaders/shader.shader ${PROJECT_BINARY_DIR}/res/shaders/shader.shader COPYONLY)

GLSL Shader compilation error

I'm trying to compile simple shader on my linux machine with Radeon HD 5470 video card and fglrx AMD driver.
My vertex shader code
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
void main()
{
gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1.0;
}
Read code from file
void Shader::load_from_file(const std::string& file)
{
std::ifstream is(file, std::ios_base::in);
if (is.is_open()) {
std::string line{""};
while(std::getline(is, line)) {
// program_code_ is a std::string member
program_code_ += "\n" + line;
}
is.close();
} else {
throw Exception("Could not open shader source code file");
}
}
Try to compile
void Shader::build_shader()
{
const GLchar* tmp = program_code_.c_str();
const GLint tmplen = program_code_.length();
std::cout << "Shader code: " << tmp << std::endl;
glShaderSource(shader_handler_, 1, &tmp, &tmplen);
CHECK_ERR();
glCompileShader(shader_handler_);
CHECK_ERR();
//...
}
And have error from glGetShaderInfoLog
Exception caught: Vertex shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: "<" parse error
ERROR: error(#273) 1 compilation errors. No code generated
But before I calling glShaderSource, I print to stdout value of tmp pointer and it seems to valid shader code:
Shader code:
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
void main()
{
gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1.0;
}
My code doesn't read garbage from memory, but I can't understand what's wrong.
Also
% glxinfo | grep vertex_program
% GL_ARB_vertex_program
Reading the file line by line, and concatenating these lines, seems to be the problem.
I don't know how this introduces an error which matches the error message you got from the shader compiler, but as suggested in the comments, reading the whole file at once solves the problem.
The following lines reads from a file stream is by utilizing the function rdbuf and a stringstream (you need to #include <sstream>):
std::ostringstream contents;
contents << is.rdbuf();
program_code_ = contents.str();
For more information about this method, and a comparison to other methods, see http://insanecoding.blogspot.de/2011/11/how-to-read-in-file-in-c.html.

‘GL_VERTIEX_SHADER’ was not declared in this scope - when it is <--It was a typo.

This is really weird,
line 1752 of glew.h:
#define GL_VERTEX_SHADER 0x8B31
Under the GL_VERSION_2_0 header guard
I have this code:
GLenum err = glewInit();
if(GLEW_OK != err) {
::std::cout<<"Error: "<<glewGetErrorString(err)<<"\n";
}
//GLuint shader = glCreateShader(GL_VERTIEX_SHADER); <--FAILS
GLuint shader = glCreateShader(0x8b31); <--WORKS
::std::cout<<"Shader: "<<shader<<"\n"<<"Errorstr: "<<
glewGetErrorString(glGetError())<<"\n";
#ifdef GL_VERSION_2_0
::std::cout<<"OKAY I have 2.0\n";
#endif
::std::cout<<glGetString(GL_VERSION)<<"\n";
Output:
Shader: 1
Errorstr: No error
OKAY I have 2.0
4.4.0 NVIDIA 331.38
If I use GL_VERTEX_SHADER however I get a symbol not found, weirdly my IDE can't find it either.
I've just noticed, I actually spelled "VERTEX" wrong. It works now. I feel really silly.
It took a title to make me see that though

OpenGL and SDL '#version required and missing' but working in debug

I'm fairly noobish with C++ so it's probably a bit too early to get into this sorta thing, but anyway. I've been working on setting up a simple triangle in OpenGL and SDL but I have some weird things happening already. The first thing is that when I compile it I get one of three errors:
Line 194: ERROR: Compiled vertex shader was corrupt.
Line 156: ERROR: 0:1: '' : #version required and missing. ERROR: 0:1: '<' : syntax error syntax error
Line 126: ERROR: 0:1: '' : #version required and missing.
ERROR: 0:1: 'lour' : syntax error syntax error
Like literally I just repeatedly hit the build button and it seems completely random whether it displays one or the other of the messages. Ok, so that's weird but what's weirder is that when I run it in debug mode (i.e I put a breakpoint and step through everything) it works perfectly, for no apparent reason.
So, I've come to the conclusion that I've done something stupid with memory, but in any case here is the relevant code (ask if you want to see more):
Read File Function
std::string readFile(std::string name) {
std::ifstream t1(name.c_str());
if (t1.fail()) {
std::cout << "Error loading stream" << "\n";
}
std::stringstream buffer;
buffer << t1.rdbuf();
std::string src = buffer.str();
std::cout << src << std::endl;
return src;
}
Compile Shaders
const char *vSource = readFile("vertexShader.gl").c_str();
// Read fragment shader
const char *fSource = readFile("fragmentShader.gl").c_str();
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vSource, NULL);
glCompileShader(vs);
GLint isCompiled = 0;
glGetShaderiv(vs, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE) { // ... Error stuff here...
N.B there's a fragment shader compiling bit which looks exactly the same. Lines 126 and 156 are in the error stuff part of the vertex and fragment shaders respectively.
Link Shaders
shader_programme = glCreateProgram();
glAttachShader(shader_programme, vs);
glAttachShader(shader_programme, fs);
glLinkProgram(shader_programme);
GLint isLinked = 0;
glGetProgramiv(shader_programme, GL_LINK_STATUS, (int *)&isLinked);
if(isLinked == GL_FALSE) { // ... Error stuff ... }
glDetachShader(shader_programme, vs);
glDetachShader(shader_programme, fs);
You can see the shaders if you want but they work (as in the fragment shader shows the correct colour in debug mode) so I don' think that they are the problem.
I'm on OSX using SDL2, OpenGL v3.2, GLSL v1.50 and Xcode 4 (and yes I did the text file thing if you know what I mean).
Sorry for posting a lot of code - if anyone has any tips on how to debug memory leaks in Xcode that might help, but thanks anyway :)
you throw away the strings as soon as you read them leading to the vSource referencing deleted memory (and undefined behavior), instead keep the sources as std::string while you need the char* to remain valid:
std::string vSource = readFile("vertexShader.gl");
char* vSourcecharp = vSource.c_str();
// Read fragment shader
std::string fSource = readFile("fragmentShader.gl");
char* fSourcecharp = fSource.c_str();
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vSourcecharp, NULL);
glCompileShader(vs);

OpenGL shader loading fails

I'm having a problem with my shader loading code. The bizarre thing that's confusing me is that it works maybe once in 5 times, but then only sort of works. For instance, it'll load the frag shader, but then texturing won't work properly (it'll draw a strange semblance of the texture over the geometry instead). I think the problem is with the loading code, so that's what my question is about. Can anyone spot an error I haven't found in the code below?
char* vs, * fs;
vertexShaderHandle = glCreateShader(GL_VERTEX_SHADER);
fragmentShaderHandle = glCreateShader(GL_FRAGMENT_SHADER);
long sizeOfVShaderFile = getSizeOfFile(VERTEX_SHADER_FILE_NAME);
long sizeOfFShaderFile = getSizeOfFile(FRAGMENT_SHADER_FILE_NAME);
if(sizeOfVShaderFile == -1)
{
cerr << VERTEX_SHADER_FILE_NAME<<" is null! Exiting..." << endl;
return;
}
if(sizeOfFShaderFile == -1)
{
cerr << FRAGMENT_SHADER_FILE_NAME<<" is null! Exiting..." << endl;
return;
}
vs = readFile(VERTEX_SHADER_FILE_NAME);
fs = readFile(FRAGMENT_SHADER_FILE_NAME);
const char* vv = vs, *ff = fs;
glShaderSource(vertexShaderHandle , 1, &vv, NULL);
cout << "DEBUGGING SHADERS" << endl;
cout << "VERTEX SHADER: ";
printShaderInfoLog(vertexShaderHandle);
cout << endl;
glShaderSource(fragmentShaderHandle, 1, &ff, NULL);
cout << "FRAGMENT SHADER: ";
printShaderInfoLog(fragmentShaderHandle);
cout << endl;
glCompileShader(vertexShaderHandle);
cout << "VERTEX SHADER: ";
printShaderInfoLog(vertexShaderHandle);
cout << endl;
glCompileShader(fragmentShaderHandle);
cout << "FRAGMENT SHADER: ";
printShaderInfoLog(fragmentShaderHandle);
cout << endl;
programHandle = glCreateProgram();
cout << "DEBUGGING PROGRAM" << endl;
glAttachShader(programHandle, vertexShaderHandle);
printProgramInfoLog(programHandle);
glAttachShader(programHandle, fragmentShaderHandle);
printProgramInfoLog(programHandle);
glLinkProgram(programHandle);
printProgramInfoLog(programHandle);
glUseProgram(programHandle);
printProgramInfoLog(programHandle);
delete[] vs; delete[] fs;
Here's the readFile function:
char* readFile(const char* path)
{
unsigned int fileSize = getSizeOfFile(path);
char* file_data = new char[fileSize];
ifstream input_stream;
input_stream.open(path, ios::binary);
input_stream.read(file_data, fileSize);
input_stream.close();
//this is deleted at the end of the shader code
return file_data;
}
All of the below messages are from the exact same executable (no rebuild).
Here's the first possible error message:
BallGLWidget::initializeGL called
DEBUGGING SHADERS
VERTEX SHADER:
FRAGMENT SHADER:
VERTEX SHADER: ERROR: 0:17: '<' : syntax error syntax error
FRAGMENT SHADER:
DEBUGGING PROGRAM
ERROR: One or more attached shaders not successfully compiled
ERROR: One or more attached shaders not successfully compiled
glGetError enum value: GL_NO_ERROR
Another possible error message:
BallGLWidget::initializeGL called
DEBUGGING SHADERS
VERTEX SHADER:
FRAGMENT SHADER:
VERTEX SHADER: ERROR: 0:17: 'tt' : syntax error syntax error
FRAGMENT SHADER: ERROR: 0:33: '?' : syntax error syntax error
DEBUGGING PROGRAM
ERROR: One or more attached shaders not successfully compiled
ERROR: One or more attached shaders not successfully compiled
Here's the output when it works (maybe 1 in 5 or 6 times)
BallGLWidget::initializeGL called
DEBUGGING SHADERS
VERTEX SHADER:
FRAGMENT SHADER:
VERTEX SHADER:
FRAGMENT SHADER:
DEBUGGING PROGRAM
Image format is GL_RGB
Checking textures...
glGetError enum value: GL_NO_ERROR
I seriously doubt its the shaders themselves since they do work sometimes... and the reported errors are garbage.
If any more information would be helpful I'll gladly provide it.
EDIT: Here's the shaders
The vertex shader:
attribute vec2 a_v_position;
attribute vec2 a_tex_position;
varying vec2 tex_coord_output;
void main()
{
tex_coord_output = a_tex_position;
gl_Position = vec4(a_v_position, 0.0, 1.0);
}
The fragment shader:
varying vec2 tex_coord_output;
uniform sampler2D ballsampler;
void main()
{
gl_FragColor = texture2D(ballsampler, tex_coord_output);
}
Your question is a duplicate of Getting garbage chars when reading GLSL files and here's my answer to it:
You're using C++, so I suggest you leverage that. Instead of reading into a self allocated char array I suggest you read into a std::string:
#include <string>
#include <fstream>
std::string loadFileToString(char const * const fname)
{
std::ifstream ifile(fname);
std::string filetext;
while( ifile.good() ) {
std::string line;
std::getline(ifile, line);
filetext.append(line + "\n");
}
return filetext;
}
That automatically takes care of all memory allocation and proper delimiting -- the keyword is RAII: Resource Allocation Is Initialization. Later on you can upload the shader source with something like
void glcppShaderSource(GLuint shader, std::string const &shader_string)
{
GLchar const *shader_source = shader_string.c_str();
GLint const shader_length = shader_string.size();
glShaderSource(shader, 1, &shader_source, &shader_length);
}
void load_shader(GLuint shaderobject, char * const shadersourcefilename)
{
glcppShaderSource(shaderobject, loadFileToString(shadersourcefilename));
}
You are reading the files but as far as I can see you are not zero-terminating the text. Try allocating filesize+1 and set the last char to zero.