Kode / Kha

Ultra-portable, high performance, open source multimedia framework.
http://kha.tech
zlib License
1.49k stars 174 forks source link

GLSL runtime error if using `gl_PrimitiveID` #1302

Open Sanva opened 3 years ago

Sanva commented 3 years ago

Describe the bug Runtime error if trying to use gl_PrimitiveID.

To Reproduce Steps to reproduce the behavior:

Take for example https://github.com/Kha-Samples/Shader and use gl_PrimitiveID in the fragment shader — as a silly example I've simply changed the fragment shader by

#version 450

out vec4 FragColor;

void main() {
    FragColor = vec4(1.0, 0.0, 0.0, gl_PrimitiveID);
}

Expected behavior No runtime error messages.

Execution Environment:

Starting Kore
GLSL compiler error: 0(5) : error C7532: global variable gl_PrimitiveID requires "#version 150" or later
0(5) : error C0000: ... or #extension GL_EXT_gpu_shader4 : enable

GLSL linker error: Fragment info
-------------
0(5) : error C7532: global variable gl_PrimitiveID requires "#version 150" or later
0(5) : error C0000: ... or #extension GL_EXT_gpu_shader4 : enable
(0) : error C2003: incompatible options for link

Starting application
GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

[...]
kha/graphics4/PipelineState.hx:89: Error: Could not compile shader:
ERROR: 0:9: 'gl_PrimitiveID' : undeclared identifier

Additional context I'm not sure if this is really a bug or not, I'm very new to graphics programming. But it looks like it to me, since at runtime the application complains about requiring #version 150 at least, but the example uses #version 450, and that global variable looks like it should be available in GLSL since #version 150 ( https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_PrimitiveID.xhtml )... but, clearly this is Kha, not OpenGL/WebGL directly, so if it isn't intended to have this kind of variables available, sorry for the noise.

Maybe related?

Tested also with Kha master — same errors both in Linux and HTML5.

RobDangerous commented 3 years ago

The GLSL code gets compiled to an earlier GLSL version - HTML5 can not support version 450, Linux can support it sometimes. There's an option to change the target GLSL version for Linux somewhere. For HTML5 we would have to add support for compiling to WebGL 1 and 2 shaders and choose the best one at runtime.

RobDangerous commented 3 years ago

--shaderversion is the option to try for Linux, --shaderversion 300 might be an ok-ish value for most systems.

RobDangerous commented 3 years ago

Oh and for html5 we already do that multi-shader-compilation. Does webgl2 work in your browser?

Sanva commented 3 years ago

--shaderversion is the option to try for Linux, --shaderversion 300 might be an ok-ish value for most systems.

Building with that option gives me different errors:

Starting Kinc
GLSL compiler error: 0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type mat4 requires "#version 100" or later
0(5) : error C7532: global type vec3 requires "#version 100" or later
0(6) : error C7532: global type vec2 requires "#version 100" or later
0(6) : error C5060: out can't be used with non-varying texCoord
0(8) : error C7532: global type vec4 requires "#version 100" or later
0(8) : error C5060: out can't be used with non-varying color
0(13) : error C7532: global variable gl_Position requires "#version 100" or later

GLSL compiler error: 0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type sampler2D requires "#version 100" or later
0(5) : error C7532: global type vec2 requires "#version 100" or later
0(6) : error C7532: global type vec4 requires "#version 100" or later
0(7) : error C5060: out can't be used with non-varying FragColor
0(11) : error C7532: global function texture requires "#version 130" or later
0(12) : error C7532: global type vec3 requires "#version 100" or later

GLSL linker error: Vertex info
-----------
0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type mat4 requires "#version 100" or later
0(5) : error C7532: global type vec3 requires "#version 100" or later
0(6) : error C7532: global type vec2 requires "#version 100" or later
0(6) : error C5060: out can't be used with non-varying texCoord
0(8) : error C7532: global type vec4 requires "#version 100" or later
0(8) : error C5060: out can't be used with non-varying color
0(13) : error C7532: global variable gl_Position requires "#version 100" or later
(0) : error C2003: incompatible options for link

Fragment info
-------------
0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type sampler2D requires "#version 100" or later
0(5) : error C7532: global type vec2 requires "#version 100" or later
0(6) : error C7532: global type vec4 requires "#version 100" or later
0(7) : error C5060: out can't be used with non-varying FragColor
0(11) : error C7532: global function texture requires "#version 130" or later
0(12) : error C7532: global type vec3 requires "#version 100" or later
(0) : error C2003: incompatible options for link

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 396

Uniform projectionMatrix not found.
GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 414

GLSL compiler error: 0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type mat4 requires "#version 100" or later
0(5) : error C7532: global type vec3 requires "#version 100" or later
0(6) : error C7532: global type vec4 requires "#version 100" or later
0(6) : error C5060: out can't be used with non-varying fragmentColor
0(11) : error C7532: global variable gl_Position requires "#version 100" or later

GLSL compiler error: 0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type vec4 requires "#version 100" or later
0(3) : error C5060: out can't be used with non-varying FragColor

GLSL linker error: Vertex info
-----------
0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type mat4 requires "#version 100" or later
0(5) : error C7532: global type vec3 requires "#version 100" or later
0(6) : error C7532: global type vec4 requires "#version 100" or later
0(6) : error C5060: out can't be used with non-varying fragmentColor
0(11) : error C7532: global variable gl_Position requires "#version 100" or later
(0) : error C2003: incompatible options for link

Fragment info
-------------
0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type vec4 requires "#version 100" or later
0(3) : error C5060: out can't be used with non-varying FragColor
(0) : error C2003: incompatible options for link

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 396

Uniform projectionMatrix not found.
GLSL compiler error: 0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type mat4 requires "#version 100" or later
0(5) : error C7532: global type vec3 requires "#version 100" or later
0(6) : error C7532: global type vec2 requires "#version 100" or later
0(6) : error C5060: out can't be used with non-varying texCoord
0(8) : error C7532: global type vec4 requires "#version 100" or later
0(8) : error C5060: out can't be used with non-varying fragmentColor
0(13) : error C7532: global variable gl_Position requires "#version 100" or later

GLSL compiler error: 0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type sampler2D requires "#version 100" or later
0(5) : error C7532: global type vec4 requires "#version 100" or later
0(5) : error C5060: out can't be used with non-varying FragColor
0(7) : error C7532: global type vec2 requires "#version 100" or later
0(11) : error C7532: global function texture requires "#version 130" or later

GLSL linker error: Vertex info
-----------
0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type mat4 requires "#version 100" or later
0(5) : error C7532: global type vec3 requires "#version 100" or later
0(6) : error C7532: global type vec2 requires "#version 100" or later
0(6) : error C5060: out can't be used with non-varying texCoord
0(8) : error C7532: global type vec4 requires "#version 100" or later
0(8) : error C5060: out can't be used with non-varying fragmentColor
0(13) : error C7532: global variable gl_Position requires "#version 100" or later
(0) : error C2003: incompatible options for link

Fragment info
-------------
0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type sampler2D requires "#version 100" or later
0(5) : error C7532: global type vec4 requires "#version 100" or later
0(5) : error C5060: out can't be used with non-varying FragColor
0(7) : error C7532: global type vec2 requires "#version 100" or later
0(11) : error C7532: global function texture requires "#version 130" or later
(0) : error C2003: incompatible options for link

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 396

Uniform projectionMatrix not found.
GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 414

GLSL compiler error: 0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type mat4 requires "#version 100" or later
0(5) : error C7532: global type vec3 requires "#version 100" or later
0(6) : error C7532: global type vec2 requires "#version 100" or later
0(6) : error C5060: out can't be used with non-varying texCoord
0(8) : error C7532: global type vec4 requires "#version 100" or later
0(8) : error C5060: out can't be used with non-varying color
0(13) : error C7532: global variable gl_Position requires "#version 100" or later

GLSL compiler error: 0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type sampler2D requires "#version 100" or later
0(5) : error C7532: global type vec2 requires "#version 100" or later
0(6) : error C7532: global type vec4 requires "#version 100" or later
0(7) : error C5060: out can't be used with non-varying FragColor
0(11) : error C7532: global function texture requires "#version 130" or later
0(12) : error C7532: global type vec3 requires "#version 100" or later

GLSL linker error: Vertex info
-----------
0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type mat4 requires "#version 100" or later
0(5) : error C7532: global type vec3 requires "#version 100" or later
0(6) : error C7532: global type vec2 requires "#version 100" or later
0(6) : error C5060: out can't be used with non-varying texCoord
0(8) : error C7532: global type vec4 requires "#version 100" or later
0(8) : error C5060: out can't be used with non-varying color
0(13) : error C7532: global variable gl_Position requires "#version 100" or later
(0) : error C2003: incompatible options for link

Fragment info
-------------
0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type sampler2D requires "#version 100" or later
0(5) : error C7532: global type vec2 requires "#version 100" or later
0(6) : error C7532: global type vec4 requires "#version 100" or later
0(7) : error C5060: out can't be used with non-varying FragColor
0(11) : error C7532: global function texture requires "#version 130" or later
0(12) : error C7532: global type vec3 requires "#version 100" or later
(0) : error C2003: incompatible options for link

GLSL compiler error: 0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type vec3 requires "#version 100" or later
0(7) : error C7532: global variable gl_Position requires "#version 100" or later
0(7) : error C7532: global type vec4 requires "#version 100" or later

GLSL compiler error: 0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type vec4 requires "#version 100" or later
0(3) : error C5060: out can't be used with non-varying FragColor
0(7) : error C7532: global type float requires "#version 100" or later
0(7) : error C7532: global variable gl_PrimitiveID requires "#version 150" or later
0(7) : error C0000: ... or #extension GL_EXT_gpu_shader4 : enable

GLSL linker error: Vertex info
-----------
0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type vec3 requires "#version 100" or later
0(7) : error C7532: global variable gl_Position requires "#version 100" or later
0(7) : error C7532: global type vec4 requires "#version 100" or later
(0) : error C2003: incompatible options for link

Fragment info
-------------
0(2) : error C0121: #version 300 must be followed by es
0(2) : error C0201: unsupported version 300
0(3) : error C7532: global type vec4 requires "#version 100" or later
0(3) : error C5060: out can't be used with non-varying FragColor
0(7) : error C7532: global type float requires "#version 100" or later
0(7) : error C7532: global variable gl_PrimitiveID requires "#version 150" or later
0(7) : error C0000: ... or #extension GL_EXT_gpu_shader4 : enable
(0) : error C2003: incompatible options for link

Starting application
GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

GL Error 1282 ../../../Kha/Kinc/Backends/Graphics4/OpenGL/Sources/kinc/backend/graphics4/pipeline.c 209

[...]

With --shaderversion 150 the basic example works without printing errors — maybe a problem with my system?

RobDangerous commented 3 years ago

No, 300 was just wrong, it's not a thing. 330 would have been correct.

Sanva commented 3 years ago

No, 300 was just wrong, it's not a thing. 330 would have been correct.

Yes, with --shaderversion 330 the shader example with gl_PrimitiveID works without printing errors.

Oh and for html5 we already do that multi-shader-compilation. Does webgl2 work in your browser?

Yes — this is what https://webglreport.com/?v=2 says about it:

Platform:   Linux x86_64
Browser User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36
Context Name:   webgl2
GL Version: WebGL 2.0 (OpenGL ES 3.0 Chromium)
Shading Language Version:   WebGL GLSL ES 3.00 (OpenGL ES GLSL ES 3.0 Chromium)
Vendor: WebKit
Renderer:   WebKit WebGL
Unmasked Vendor:    Google Inc.
Unmasked Renderer:  ANGLE (NVIDIA Corporation, Quadro M2000M/PCIe/SSE2, OpenGL 4.5 core)
Antialiasing:   Available
ANGLE:  No
Major Performance Caveat:   No

Supported Extensions:
EXT_color_buffer_float
EXT_color_buffer_half_float
EXT_disjoint_timer_query_webgl2
EXT_float_blend
EXT_texture_compression_bptc
EXT_texture_compression_rgtc
EXT_texture_filter_anisotropic
EXT_texture_norm16
KHR_parallel_shader_compile
OES_texture_float_linear
WEBGL_compressed_texture_etc1
WEBGL_compressed_texture_s3tc
WEBGL_compressed_texture_s3tc_srgb
WEBGL_debug_renderer_info
WEBGL_debug_shaders
WEBGL_lose_context
WEBGL_multi_draw