Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  GLSL and struct arrays  (Read 1778 times)
0 Members and 1 Guest are viewing this topic.
Offline greenOwl

Junior Member


Medals: 1


from Germany


« Posted 2013-04-06 15:11:26 »

Hello,

I want to compute multiple lights in my fragment shader. I pass the light data to the vertex shader via a uniform struct[] and then I want to do something like that:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
struct Global_lights {
  ...
};

uniform ...
uniform Global_lights[] lights;

out ...
out Global_lights[] frag_lights;

void pass_lights() {
   frag_lights = lights;
}

The fragment shader:
1  
2  
3  
4  
5  
6  
struct Global_lights {
  ...
};

in ...
in Global_lights[] frag_lights;


If I try to compile this I get two errors:
Error compiling Shader
Vertex shader failed to compile with the following errors:
ERROR: 0:27: error(#282) Implicitly sized arrays can not be assigned to
ERROR: 0:27: error(#160) Cannot convert from: "uniform array of structure" to: "default out array of structure"
ERROR: error(#273) 2 compilation errors.  No code generated


I somewhat understand the error message but I'd like some kind of source to read what is allowed and what not. Can someone offer a soluting? The exact amount of lights has to variable.
Thanks!

elfeck
Offline Danny02
« Reply #1 - Posted 2013-04-06 16:01:00 »

when u want to use a uniform in the fragment-shader, you don't have to pass it through the vertex-shader.
just declare the uniform array in the fragment shader.

When you want to use a uniform in multiple shader stages(vertex, fragment ...) just declare it with the same name and type in all stages were you want to use it.
Offline greenOwl

Junior Member


Medals: 1


from Germany


« Reply #2 - Posted 2013-04-06 18:32:48 »

Wow thanks, I didn't konw that.
If I declare a uniform in the fragment shader because I only need it in the fragment shader, do I still have to declare it in vertex shader as well? My test suggests that but it doesn't really makes sense.
vertex shader
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
#version 140

in vec4 vertex_position;
in vec4 vertex_color;

uniform vec2 camera_offset;
uniform mat4 mvp_matrix;
// if I remove this the color uniform the uniform in the frag shader is not what it should be
uniform vec4 color;

out vec4 frag_position;
out vec4 frag_colorcode;

void main() {
   frag_position = vec4(vertex_position.xy - camera_offset.xy, vertex_position.zw) * mvp_matrix;
   frag_colorcode = vertex_color;
   gl_Position = frag_position;
}

fragment shader
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
 #version 140
 
//@insert global_lights.struct

in vec4 frag_position;
in vec4 frag_colorcode;

uniform vec4 color;
uniform Global_lights[] frag_lights;
 
 void main() {
    gl_FragColor = (color + frag_colorcode);
}


Thanks!

elfeck
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2013-04-06 20:33:10 »

If you only use a uniform in the frag shader, you don't need to declare it in the vertex shader, and vice versa.

Offline greenOwl

Junior Member


Medals: 1


from Germany


« Reply #4 - Posted 2013-04-06 23:16:37 »

Okay I found the problem! Thank you.
One followup question: Is there a way to determine the length of an implicit declared (uniform) array? .length() doesn't work. Is there some other way to iterate over the array?

elfeck
Offline theagentd
« Reply #5 - Posted 2013-04-07 04:50:41 »

Okay I found the problem! Thank you.
One followup question: Is there a way to determine the length of an implicit declared (uniform) array? .length() doesn't work. Is there some other way to iterate over the array?

There are no implicitly declared arrays in GLSL. You can pass a uniform variable for the length and only compute lighting for the relevant lights, but you need an explicit size on the uniform struct array.

Myomyomyo.
Offline greenOwl

Junior Member


Medals: 1


from Germany


« Reply #6 - Posted 2013-04-07 15:58:21 »

Hm I see ... arg why is everything so uncomfortable ...

Another followup question:
Lets say I have 32 fixed lights. All have a specific position in model space. I have to transform these light coords with the mvpMatrix to get my light calaulations right. It makes absolutly no sense to perform this calculation for every fragment so I wanted to do it once in the vertex shader and then pass it via the positions via an out variable to my fragment shader. Problem is: The amount of "in"-variables in the fragment shader is limited. Much more limited then the amount of uniforms.

Is there a solution to this problem?

elfeck
Offline theagentd
« Reply #7 - Posted 2013-04-07 16:55:07 »

Do it on the CPU once for each light.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (18 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!