Skip to content

Commit c6919d6

Browse files
committed
Reduce the maximum number of lights to 2 for vc4 (v2)
This is to work around register allocation failures in the new mesa vc4 driver. Eric wants to implement a pre-register-allocation scheduling pass, which might help, but for the time being reduce the maximum number of lights to 2, which is what lights() needs.
1 parent 28def94 commit c6919d6

6 files changed

Lines changed: 326 additions & 4 deletions

File tree

core/build.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@
6363
<!-- Copy shaders to bin. (Eclipse does this automatically.) -->
6464
<copy todir="bin">
6565
<fileset dir="src">
66-
<include name="processing/opengl/shaders/*.glsl" />
67-
<include name="icon/*.png" />
66+
<include name="processing/opengl/shaders/*.glsl" />
67+
<include name="icon/*.png" />
6868
</fileset>
6969
</copy>
7070
</target>

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7171,6 +7171,16 @@ protected void getGLParameters() {
71717171
maxAnisoAmount = floatBuffer.get(0);
71727172
}
71737173

7174+
// overwrite the default shaders with vendor specific versions
7175+
// if needed
7176+
if (OPENGL_RENDERER.equals("VideoCore IV HW") || // Broadcom's binary driver for Raspberry Pi
7177+
OPENGL_RENDERER.equals("Gallium 0.4 on VC4")) { // Mesa driver for same hardware
7178+
defLightShaderVertURL =
7179+
PGraphicsOpenGL.class.getResource("/processing/opengl/shaders/LightVert-vc4.glsl");
7180+
defTexlightShaderVertURL =
7181+
PGraphicsOpenGL.class.getResource("/processing/opengl/shaders/TexLightVert-vc4.glsl");
7182+
}
7183+
71747184
glParamsRead = true;
71757185
}
71767186

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
Part of the Processing project - http://processing.org
3+
4+
Copyright (c) 2011-13 Ben Fry and Casey Reas
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License version 2.1 as published by the Free Software Foundation.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General
16+
Public License along with this library; if not, write to the
17+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18+
Boston, MA 02111-1307 USA
19+
*/
20+
21+
#define PROCESSING_LIGHT_SHADER
22+
23+
uniform mat4 modelviewMatrix;
24+
uniform mat4 transformMatrix;
25+
uniform mat3 normalMatrix;
26+
27+
uniform int lightCount;
28+
uniform vec4 lightPosition[8];
29+
uniform vec3 lightNormal[8];
30+
uniform vec3 lightAmbient[8];
31+
uniform vec3 lightDiffuse[8];
32+
uniform vec3 lightSpecular[8];
33+
uniform vec3 lightFalloff[8];
34+
uniform vec2 lightSpot[8];
35+
36+
attribute vec4 position;
37+
attribute vec4 color;
38+
attribute vec3 normal;
39+
40+
attribute vec4 ambient;
41+
attribute vec4 specular;
42+
attribute vec4 emissive;
43+
attribute float shininess;
44+
45+
varying vec4 vertColor;
46+
varying vec4 backVertColor;
47+
48+
const float zero_float = 0.0;
49+
const float one_float = 1.0;
50+
const vec3 zero_vec3 = vec3(0);
51+
52+
float falloffFactor(vec3 lightPos, vec3 vertPos, vec3 coeff) {
53+
vec3 lpv = lightPos - vertPos;
54+
vec3 dist = vec3(one_float);
55+
dist.z = dot(lpv, lpv);
56+
dist.y = sqrt(dist.z);
57+
return one_float / dot(dist, coeff);
58+
}
59+
60+
float spotFactor(vec3 lightPos, vec3 vertPos, vec3 lightNorm, float minCos, float spotExp) {
61+
vec3 lpv = normalize(lightPos - vertPos);
62+
vec3 nln = -one_float * lightNorm;
63+
float spotCos = dot(nln, lpv);
64+
return spotCos <= minCos ? zero_float : pow(spotCos, spotExp);
65+
}
66+
67+
float lambertFactor(vec3 lightDir, vec3 vecNormal) {
68+
return max(zero_float, dot(lightDir, vecNormal));
69+
}
70+
71+
float blinnPhongFactor(vec3 lightDir, vec3 vertPos, vec3 vecNormal, float shine) {
72+
vec3 np = normalize(vertPos);
73+
vec3 ldp = normalize(lightDir - np);
74+
return pow(max(zero_float, dot(ldp, vecNormal)), shine);
75+
}
76+
77+
void main() {
78+
// Vertex in clip coordinates
79+
gl_Position = transformMatrix * position;
80+
81+
// Vertex in eye coordinates
82+
vec3 ecVertex = vec3(modelviewMatrix * position);
83+
84+
// Normal vector in eye coordinates
85+
vec3 ecNormal = normalize(normalMatrix * normal);
86+
vec3 ecNormalInv = ecNormal * -one_float;
87+
88+
// Light calculations
89+
vec3 totalAmbient = vec3(0, 0, 0);
90+
91+
vec3 totalFrontDiffuse = vec3(0, 0, 0);
92+
vec3 totalFrontSpecular = vec3(0, 0, 0);
93+
94+
vec3 totalBackDiffuse = vec3(0, 0, 0);
95+
vec3 totalBackSpecular = vec3(0, 0, 0);
96+
97+
// prevent register allocation failure by limiting ourselves to
98+
// two lights for now
99+
for (int i = 0; i < 2; i++) {
100+
if (lightCount == i) break;
101+
102+
vec3 lightPos = lightPosition[i].xyz;
103+
bool isDir = zero_float < lightPosition[i].w;
104+
float spotCos = lightSpot[i].x;
105+
float spotExp = lightSpot[i].y;
106+
107+
vec3 lightDir;
108+
float falloff;
109+
float spotf;
110+
111+
if (isDir) {
112+
falloff = one_float;
113+
lightDir = -one_float * lightNormal[i];
114+
} else {
115+
falloff = falloffFactor(lightPos, ecVertex, lightFalloff[i]);
116+
lightDir = normalize(lightPos - ecVertex);
117+
}
118+
119+
spotf = spotExp > zero_float ? spotFactor(lightPos, ecVertex, lightNormal[i],
120+
spotCos, spotExp)
121+
: one_float;
122+
123+
if (any(greaterThan(lightAmbient[i], zero_vec3))) {
124+
totalAmbient += lightAmbient[i] * falloff;
125+
}
126+
127+
if (any(greaterThan(lightDiffuse[i], zero_vec3))) {
128+
totalFrontDiffuse += lightDiffuse[i] * falloff * spotf *
129+
lambertFactor(lightDir, ecNormal);
130+
totalBackDiffuse += lightDiffuse[i] * falloff * spotf *
131+
lambertFactor(lightDir, ecNormalInv);
132+
}
133+
134+
if (any(greaterThan(lightSpecular[i], zero_vec3))) {
135+
totalFrontSpecular += lightSpecular[i] * falloff * spotf *
136+
blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess);
137+
totalBackSpecular += lightSpecular[i] * falloff * spotf *
138+
blinnPhongFactor(lightDir, ecVertex, ecNormalInv, shininess);
139+
}
140+
}
141+
142+
// Calculating final color as result of all lights (plus emissive term).
143+
// Transparency is determined exclusively by the diffuse component.
144+
vertColor = vec4(totalAmbient, 0) * ambient +
145+
vec4(totalFrontDiffuse, 1) * color +
146+
vec4(totalFrontSpecular, 0) * specular +
147+
vec4(emissive.rgb, 0);
148+
149+
backVertColor = vec4(totalAmbient, 0) * ambient +
150+
vec4(totalBackDiffuse, 1) * color +
151+
vec4(totalBackSpecular, 0) * specular +
152+
vec4(emissive.rgb, 0);
153+
}

core/src/processing/opengl/shaders/LightVert.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ uniform vec4 lightPosition[8];
2929
uniform vec3 lightNormal[8];
3030
uniform vec3 lightAmbient[8];
3131
uniform vec3 lightDiffuse[8];
32-
uniform vec3 lightSpecular[8];
32+
uniform vec3 lightSpecular[8];
3333
uniform vec3 lightFalloff[8];
3434
uniform vec2 lightSpot[8];
3535

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
Part of the Processing project - http://processing.org
3+
4+
Copyright (c) 2011-13 Ben Fry and Casey Reas
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License version 2.1 as published by the Free Software Foundation.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General
16+
Public License along with this library; if not, write to the
17+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18+
Boston, MA 02111-1307 USA
19+
*/
20+
21+
#define PROCESSING_TEXLIGHT_SHADER
22+
23+
uniform mat4 modelviewMatrix;
24+
uniform mat4 transformMatrix;
25+
uniform mat3 normalMatrix;
26+
uniform mat4 texMatrix;
27+
28+
uniform int lightCount;
29+
uniform vec4 lightPosition[8];
30+
uniform vec3 lightNormal[8];
31+
uniform vec3 lightAmbient[8];
32+
uniform vec3 lightDiffuse[8];
33+
uniform vec3 lightSpecular[8];
34+
uniform vec3 lightFalloff[8];
35+
uniform vec2 lightSpot[8];
36+
37+
attribute vec4 position;
38+
attribute vec4 color;
39+
attribute vec3 normal;
40+
attribute vec2 texCoord;
41+
42+
attribute vec4 ambient;
43+
attribute vec4 specular;
44+
attribute vec4 emissive;
45+
attribute float shininess;
46+
47+
varying vec4 vertColor;
48+
varying vec4 backVertColor;
49+
varying vec4 vertTexCoord;
50+
51+
const float zero_float = 0.0;
52+
const float one_float = 1.0;
53+
const vec3 zero_vec3 = vec3(0);
54+
55+
float falloffFactor(vec3 lightPos, vec3 vertPos, vec3 coeff) {
56+
vec3 lpv = lightPos - vertPos;
57+
vec3 dist = vec3(one_float);
58+
dist.z = dot(lpv, lpv);
59+
dist.y = sqrt(dist.z);
60+
return one_float / dot(dist, coeff);
61+
}
62+
63+
float spotFactor(vec3 lightPos, vec3 vertPos, vec3 lightNorm, float minCos, float spotExp) {
64+
vec3 lpv = normalize(lightPos - vertPos);
65+
vec3 nln = -one_float * lightNorm;
66+
float spotCos = dot(nln, lpv);
67+
return spotCos <= minCos ? zero_float : pow(spotCos, spotExp);
68+
}
69+
70+
float lambertFactor(vec3 lightDir, vec3 vecNormal) {
71+
return max(zero_float, dot(lightDir, vecNormal));
72+
}
73+
74+
float blinnPhongFactor(vec3 lightDir, vec3 vertPos, vec3 vecNormal, float shine) {
75+
vec3 np = normalize(vertPos);
76+
vec3 ldp = normalize(lightDir - np);
77+
return pow(max(zero_float, dot(ldp, vecNormal)), shine);
78+
}
79+
80+
void main() {
81+
// Vertex in clip coordinates
82+
gl_Position = transformMatrix * position;
83+
84+
// Vertex in eye coordinates
85+
vec3 ecVertex = vec3(modelviewMatrix * position);
86+
87+
// Normal vector in eye coordinates
88+
vec3 ecNormal = normalize(normalMatrix * normal);
89+
vec3 ecNormalInv = ecNormal * -one_float;
90+
91+
// Light calculations
92+
vec3 totalAmbient = vec3(0, 0, 0);
93+
94+
vec3 totalFrontDiffuse = vec3(0, 0, 0);
95+
vec3 totalFrontSpecular = vec3(0, 0, 0);
96+
97+
vec3 totalBackDiffuse = vec3(0, 0, 0);
98+
vec3 totalBackSpecular = vec3(0, 0, 0);
99+
100+
// prevent register allocation failure by limiting ourselves to
101+
// two lights for now
102+
for (int i = 0; i < 2; i++) {
103+
if (lightCount == i) break;
104+
105+
vec3 lightPos = lightPosition[i].xyz;
106+
bool isDir = zero_float < lightPosition[i].w;
107+
float spotCos = lightSpot[i].x;
108+
float spotExp = lightSpot[i].y;
109+
110+
vec3 lightDir;
111+
float falloff;
112+
float spotf;
113+
114+
if (isDir) {
115+
falloff = one_float;
116+
lightDir = -one_float * lightNormal[i];
117+
} else {
118+
falloff = falloffFactor(lightPos, ecVertex, lightFalloff[i]);
119+
lightDir = normalize(lightPos - ecVertex);
120+
}
121+
122+
spotf = spotExp > zero_float ? spotFactor(lightPos, ecVertex, lightNormal[i],
123+
spotCos, spotExp)
124+
: one_float;
125+
126+
if (any(greaterThan(lightAmbient[i], zero_vec3))) {
127+
totalAmbient += lightAmbient[i] * falloff;
128+
}
129+
130+
if (any(greaterThan(lightDiffuse[i], zero_vec3))) {
131+
totalFrontDiffuse += lightDiffuse[i] * falloff * spotf *
132+
lambertFactor(lightDir, ecNormal);
133+
totalBackDiffuse += lightDiffuse[i] * falloff * spotf *
134+
lambertFactor(lightDir, ecNormalInv);
135+
}
136+
137+
if (any(greaterThan(lightSpecular[i], zero_vec3))) {
138+
totalFrontSpecular += lightSpecular[i] * falloff * spotf *
139+
blinnPhongFactor(lightDir, ecVertex, ecNormal, shininess);
140+
totalBackSpecular += lightSpecular[i] * falloff * spotf *
141+
blinnPhongFactor(lightDir, ecVertex, ecNormalInv, shininess);
142+
}
143+
}
144+
145+
// Calculating final color as result of all lights (plus emissive term).
146+
// Transparency is determined exclusively by the diffuse component.
147+
vertColor = vec4(totalAmbient, 0) * ambient +
148+
vec4(totalFrontDiffuse, 1) * color +
149+
vec4(totalFrontSpecular, 0) * specular +
150+
vec4(emissive.rgb, 0);
151+
152+
backVertColor = vec4(totalAmbient, 0) * ambient +
153+
vec4(totalBackDiffuse, 1) * color +
154+
vec4(totalBackSpecular, 0) * specular +
155+
vec4(emissive.rgb, 0);
156+
157+
// Calculating texture coordinates, with r and q set both to one
158+
vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0);
159+
}

core/src/processing/opengl/shaders/TexLightVert.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ uniform vec4 lightPosition[8];
3030
uniform vec3 lightNormal[8];
3131
uniform vec3 lightAmbient[8];
3232
uniform vec3 lightDiffuse[8];
33-
uniform vec3 lightSpecular[8];
33+
uniform vec3 lightSpecular[8];
3434
uniform vec3 lightFalloff[8];
3535
uniform vec2 lightSpot[8];
3636

0 commit comments

Comments
 (0)