Loading a Shader file string into glShaderSource and failing somehow - c++

Hi im getting this very weird error with loading the shader.txt file's string into glShaderSource and the compiling it with glCompileShader.
ERROR: 0:14: error(#89) Syntax error ERROR___EOF_IN_COMMENT and
ERROR: 0:14: error(#131) Syntax error: pre-mature EOF parse error
this is the shader file string im checking with glGetShaderiv:
"#version 330 core\n\nlayout (location = 0) in vec4 position;\n\nuniform mat4 pr_matrix;\nuniform mat4 vw_matrix = mat4(1.0);\nuniform mat4 ml_matrix = mat4(1.0);\n\nvoid main()\n{\n\ngl_Position = /*pr_matrix *
notice that it couldn't copy the whole thing, But i assure you its all correctly writed. What to do?

You have an unterminated comment. Your string should look like this:
"#version 330 core\n\nlayout (location = 0) in vec4 position;\n\nuniform mat4 pr_matrix;\nuniform mat4 vw_matrix = mat4(1.0);\nuniform mat4 ml_matrix = mat4(1.0);\n\nvoid main()\n{\n\ngl_Position = pr_matrix; }"

Related

GLSL vertex shader for texture display not compiling

I've got a vertex shader that doesn't want to compile, but I see absolutely no reason for the error it is giving. It's failing with the following error:
ERROR: D:\local\Temp\f46179fe-8934-4135-820a-bed1e54b98bf.tmp:22: 'texture2D' : no matching overloaded function found
ERROR: D:\local\Temp\f46179fe-8934-4135-820a-bed1e54b98bf.tmp:22: 'assign' : cannot convert from ' const float' to ' smooth out 4-component vector of float'
ERROR: D:\local\Temp\f46179fe-8934-4135-820a-bed1e54b98bf.tmp:22: '' : compilation terminated
ERROR: 3 compilation errors. No code generated.
SPIR-V is not generated for failed compile or link
Here is the code:
#version 460
//Passes variables from uniforms to frag_hud shader
layout(location = 0) in vec3 pos;
layout(location = 1) in vec2 vert_tex_coord;
uniform float vert_col_dist;
uniform sampler2D vert_hud01;
uniform sampler2D vert_hud02;
uniform sampler2D vert_hud03;
out vec2 tex_coord;
out float col_dist;
out vec4 hud01;
out vec4 hud02;
out vec4 hud03;
void main() {
gl_Position = vec4(pos, 1.0);
tex_coord = vec2(vert_tex_coord.x, vert_tex_coord.y);
col_dist = vert_col_dist;
hud01 = texture2D(vert_hud01, vert_tex_coord);
hud02 = texture2D(vert_hud02, vert_tex_coord);
hud03 = texture2D(vert_hud03, vert_tex_coord);
}
Figured it out. Needed to use texture() instead of texture2D().

SPIR-V require locations on user in/out in Vulkan 1.0.51.1 +

i installed the latest Vulkan SDK on my computer how ever whenever i want to generate the SPIR-V files for my shaders through glslValidator.exe it fails and returns the following errors
ERROR: Shader.vert:17: 'location' : SPIR-V requires location for user input/output
ERROR: 1 compilation errors. No code generated.
ERROR: Linking vertex stage: Missing entry point: Each stage requires one entry point
SPIR-V is not generated for failed compile or link
I found out that since update 1.0.51.1 there are some changes that might cause my old shaders to fail
Require locations on user in/out in GL_KHR_vulkan_glsl (internal issue 783).
what is the proper/new way to fix this issue?
vertex shader
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(binding = 0)uniform UniformBufferObject {
mat4 model;
mat4 view;
mat4 proj;
} ubo;
layout(location = 0)in vec3 inPosition;
layout(location = 1)in vec3 inNormals;
layout(location = 2)in vec2 inTexCoord;
layout(location = 0)out vec3 fragColor;
layout(location = 1)out vec2 fragTexCoord;
out vec4 Normal;
out gl_PerVertex{
vec4 gl_Position;
};
void main()
{
gl_Position = ubo.proj * ubo.view * ubo.model * vec4 (inPosition, 1.0);
//fragColor = inColor;
fragTexCoord = inTexCoord;
Normal = ubo.proj * ubo.view * ubo.model * vec4 (inNormals, 1.0);
}
I assume that You need to explicitly set the location through layout qualifier for all Your variables:
layout( location=<number> ) ...
Vulkan requires all input, output and uniform variables to have an explicitly provided location value. Interface matching between shader stages is performed only through a location value (as opposed to OpenGL where it can be performed through both names or locations). I'm not sure as I have always provided the location value, but maybe in earlier versions glslangValidator set them implicitly (if locations were missing).

Cant enable GLSL 1.40 in my Qt application

I want to porting demo project of Box2D from VS2013 in Qt. But i have one unusual problem. When i run demo project in VS i get message with info about my openGL configuration.
I replaced code on Qt and changed initialization of shaders and calls of opengl funcitons with QOpeGL wrappers.
Firstly, i change default surface format like this:
QSurfaceFormat format;
format.setVersion(3, 1);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
But when i debug program is format of my surface stay on 3.0.
And when i try to create shaders i get error message like this:
QOpenGLShader::compile(Vertex): ERROR: 0:1: '140' : version number not supported
ERROR: 0:2: 'layout' : syntax error
*** Problematic Vertex shader source code ***
#version 140
#line 1
uniform mat4 projectionMatrix;
layout(location = 0) in vec2 v_position;
layout(location = 1) in vec4 v_color;
layout(location = 2) in float v_size;
out vec4 f_color;
void main(void)
{
f_color = v_color;
gl_Position = projectionMatrix * vec4(v_position, 0.0f, 1.0f);
gl_PointSize = v_size;
}
Why VS can create shaders and work with OpenGL 3.1/GLSL 1.40, but Qt doesn't do this?
Initialize of GL (m_functions - QOpenGLExtraFunctions)
void MainOpenGLWidget::initializeGL()
{
makeCurrent();
qDebug() << "OPENGL " << QSurfaceFormat::defaultFormat().majorVersion() <<
"." << QSurfaceFormat::defaultFormat().minorVersion();
m_functions.initializeOpenGLFunctions();
m_functions.glClearColor(0, 0, 0, 0);
g_camera.m_width = 1024;
g_camera.m_height = 640;
g_debugDraw.Create(&m_functions);
}
function g_debugDraw.Create(...):
void Create(QOpenGLExtraFunctions *functions)
{
const char *vs = \
"#version 140\n"
"uniform mat4 projectionMatrix;\n"
"layout(location = 0) in vec2 v_position;\n"
"layout(location = 1) in vec4 v_color;\n"
"layout(location = 2) in float v_size;\n"
"out vec4 f_color;\n"
"void main(void)\n"
"{\n"
" f_color = v_color;\n"
" gl_Position = projectionMatrix * vec4(v_position, 0.0f, 1.0f);\n"
" gl_PointSize = v_size;\n"
"}\n";
...
m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vs);
...
m_functions = *functions;
m_functions.glGenVertexArrays(1, &m_vaoId);
m_functions.glGenBuffers(3, m_vboIds);
m_functions.glBindVertexArray(m_vaoId);
m_functions.glEnableVertexAttribArray(m_vertexAttribute);
m_functions.glEnableVertexAttribArray(m_colorAttribute);
m_functions.glEnableVertexAttribArray(m_sizeAttribute);
...
}
I get error when call m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vs);
If you really want to stick to GLSL #version 140, then replace the following lines
layout(location = 0) in vec2 v_position;
layout(location = 1) in vec4 v_color;
layout(location = 2) in float v_size;
by
in vec2 v_position;
in vec4 v_color;
in float v_size;
Explicit attribute location is not part of GLSL until #version 330.
Be careful after that to query your shader's attribute location before enabling it in your C++ code. The shader compiler may or may not order them in the order of declaration.
const int POS_LOCATION = glGetAttribLocation(program_id, "v_position");
if(-1 != POS_LOCATION)
{
glEnableVertexAttribArray(POS_LOCATION);
...
glVertexAttribPointer(POS_LOCATION, ... );
...
}
I've found no mention of layout(location = x) in documentation of GLSL #version 140.
https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.1.40.pdf
Not mentioned until #version 330 at page 35.
https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.3.30.pdf
In your case the shader compiler error message is a little misleading. Maybe if you update you graphic card driver you'll get a better shader compiler.
I didn't took into account that GLSL for desktop is not the same that GLSL from OpenGL ES. This topic little helped me. I change all in to attribute or varying and all out to varying and also set precision on float. After change my shader was compile. Finally my shader code looks like this:
precision lowp float;
uniform mat4 projectionMatrix;
attribute vec2 v_position;
attribute vec4 v_color;
attribute float v_size;
varying vec4 f_color;
void main(void)
{
f_color = v_color;
gl_Position = projectionMatrix * vec4(v_position, 0.0, 1.0);
gl_PointSize = v_size;
}
Thank Octo for support!

GLSL Vertex Shader Compilation Error

This is my following Error which is ERROR #160 in GLSL.
#version 330 core
layout (location = 0) in vec3 position;
out vec4 pos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(position, 1.0f);
pos = position;
}
and this is the error which was created when trying to compile this GLSL program.
ERROR: Vertex Shader Program Compilation Failed! Vertex shader failed to compile with the following errors:
ERROR: 0:13: error(#160) Cannot convert from: "in highp 3-component vector of vec3" to: "default out highp 4-component vector of vec4"
ERROR: error(#273) 1 compilation errors. No code generated
I have no knowledge on debugging GLSL so if there is anyone who can help it would e much appreciated.
Well, your error is pretty clear. Since you assign position to pos, and pos is a vec4 while position is a vec3, and there is no implicit conversion for this, the compiler complains. To fix this, simply create a vec4 from you position, e.g.
pos = vec4(position, 1.0f);
However, you should think about why you would need a vec4 for your position down the pipeline, or whether having pos be a vec3 would be sufficient or not.

GLSL error: illegal non-ASCII character

I am writing a vertex shader for openGl. This is my code:
#version 150
in vec4 vPosition;
in vec3 vNormal;
out vec3 fN;
out vec3 fE;
out vec3 fL;
uniform mat4 model_matrix;
uniform mat4 view_matrix;
uniform mat4 proj_matrix;
uniform vec4 lightPos;
void main()
{
vec3 pos = (view_matrix * model_matrix * vPosition).xyz;
vec3 lightPosInCam = (view_matrix * lightPos).xyz;
fN = normalize(view_matrix * model_matrix * vec4(vNormal,1.0)).xyz;
fE = normalize(vec3(0,0,0) - pos);
fL = normalize(lightPosInCam.xyz - pos);
gl_Position = proj_matrix*view_matrix*model_matrix*vPosition;
}
However, when I run my program, it seems like the error says there is some nonASCII character after my }, because in console prints:
#version 150
in vec4 vPosition;
in vec3 vNormal;
out vec3 fN;
out vec3 fE;
out vec3 fL;
uniform mat4 model_matrix;
uniform mat4 view_matrix;
uniform mat4 proj_matrix;
uniform vec4 lightPos;
void main()
{
vec3 pos = (view_matrix * model_matrix * vPosition).xyz;
vec3 lightPosInCam = (view_matrix * lightPos).xyz;
fN = normalize(view_matrix * model_matrix * vec4(vNormal,1.0)).xyz;
fE = normalize(vec3(0,0,0) - pos);
fL = normalize(lightPosInCam.xyz - pos);
gl_Position = proj_matrix*view_matrix*model_matrix*vPosition;
}═════════════════════════
Printed Shader Read Status
vshader_hw5.glsl failed to compile:
ERROR: 0:26: '' : illegal non-ASCII character (0xcd)
ERROR: 0:26: '' : illegal non-ASCII character (0xcd)
ERROR: 0:26: '' : illegal non-ASCII character (0xcd)
ERROR: 0:26: '' : illegal non-ASCII character (0xcd)
ERROR: 0:26: '' : illegal non-ASCII character (0xcd)
ERROR: 0:26: '' : illegal non-ASCII character (0xcd)
However, when I run my program, it seems like the error says there is some nonASCII character after my }, because in console prints:
This is a typical error encountered when the shader source strings to OpenGL are not properly terminated with a zero byte (NUL) and/or if the the buffer length(s) passed do not match the length of the source string.
Solution: If you're not passing the lengths explicitly (i.e. pass NULL for the lengths parameter) make sure the source strings are properly zero terminated. Otherwise make sure, that the lengths properly match the lengths of the source string buffers.