Skip to content

Commit

Permalink
[glsl-out] Write vector access as a swizzle
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon-F authored and kvark committed May 19, 2021
1 parent 5753f6e commit 6b3db1f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
7 changes: 5 additions & 2 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,8 +1527,11 @@ impl<'a, W: Write> Writer<'a, W> {
};

match *resolved {
TypeInner::Vector { .. }
| TypeInner::Matrix { .. }
TypeInner::Vector { .. } => {
// Write vector access as a swizzle
write!(self.out, ".{}", COMPONENTS[index as usize])?
}
TypeInner::Matrix { .. }
| TypeInner::Array { .. }
| TypeInner::ValuePointer { .. } => write!(self.out, "[{}]", index)?,
TypeInner::Struct { .. } => {
Expand Down
28 changes: 14 additions & 14 deletions tests/out/boids.Compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ void main() {
vec2 pos1;
vec2 vel1;
uint i = 0u;
if((global_invocation_id[0] >= 1500u)) {
if((global_invocation_id.x >= 1500u)) {
return;
}
vPos = _group_0_binding_1.particles[global_invocation_id[0]].pos;
vVel = _group_0_binding_1.particles[global_invocation_id[0]].vel;
vPos = _group_0_binding_1.particles[global_invocation_id.x].pos;
vVel = _group_0_binding_1.particles[global_invocation_id.x].vel;
cMass = vec2(0.0, 0.0);
cVel = vec2(0.0, 0.0);
colVel = vec2(0.0, 0.0);
while(true) {
if((i >= 1500u)) {
break;
}
if((i == global_invocation_id[0])) {
if((i == global_invocation_id.x)) {
continue;
}
pos1 = _group_0_binding_1.particles[i].pos;
Expand All @@ -79,20 +79,20 @@ void main() {
vVel = (((vVel + (cMass * _group_0_binding_0.rule1Scale)) + (colVel * _group_0_binding_0.rule2Scale)) + (cVel * _group_0_binding_0.rule3Scale));
vVel = (normalize(vVel) * clamp(length(vVel), 0.0, 0.1));
vPos = (vPos + (vVel * _group_0_binding_0.deltaT));
if((vPos[0] < -1.0)) {
vPos[0] = 1.0;
if((vPos.x < -1.0)) {
vPos.x = 1.0;
}
if((vPos[0] > 1.0)) {
vPos[0] = -1.0;
if((vPos.x > 1.0)) {
vPos.x = -1.0;
}
if((vPos[1] < -1.0)) {
vPos[1] = 1.0;
if((vPos.y < -1.0)) {
vPos.y = 1.0;
}
if((vPos[1] > 1.0)) {
vPos[1] = -1.0;
if((vPos.y > 1.0)) {
vPos.y = -1.0;
}
_group_0_binding_2.particles[global_invocation_id[0]].pos = vPos;
_group_0_binding_2.particles[global_invocation_id[0]].vel = vVel;
_group_0_binding_2.particles[global_invocation_id.x].pos = vPos;
_group_0_binding_2.particles[global_invocation_id.x].vel = vVel;
return;
}

2 changes: 1 addition & 1 deletion tests/out/quad-vert.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ smooth layout(location = 0) out vec2 _vs2fs_location0;
void main1() {
v_uv = a_uv;
vec2 _expr13 = a_pos;
perVertexStruct.gen_gl_Position = vec4(_expr13[0], _expr13[1], 0.0, 1.0);
perVertexStruct.gen_gl_Position = vec4(_expr13.x, _expr13.y, 0.0, 1.0);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/out/quad.Fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ layout(location = 0) out vec4 _fs2p_location0;
void main() {
vec2 uv2 = _vs2fs_location0;
vec4 _expr4 = texture(_group_0_binding_0, vec2(uv2));
if((_expr4[3] == 0.0)) {
if((_expr4.w == 0.0)) {
discard;
}
_fs2p_location0 = (_expr4[3] * _expr4);
_fs2p_location0 = (_expr4.w * _expr4);
return;
}

6 changes: 3 additions & 3 deletions tests/out/shadow.Fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ smooth layout(location = 1) in vec4 _vs2fs_location1;
layout(location = 0) out vec4 _fs2p_location0;

float fetch_shadow(uint light_id, vec4 homogeneous_coords) {
if((homogeneous_coords[3] <= 0.0)) {
if((homogeneous_coords.w <= 0.0)) {
return 1.0;
}
float _expr26 = textureGrad(_group_0_binding_2, vec4((((homogeneous_coords.xy * vec2(0.5, -0.5)) / vec2(homogeneous_coords[3])) + vec2(0.5, 0.5)), int(light_id), (homogeneous_coords[2] / homogeneous_coords[3])), vec2(0, 0), vec2(0,0));
float _expr26 = textureGrad(_group_0_binding_2, vec4((((homogeneous_coords.xy * vec2(0.5, -0.5)) / vec2(homogeneous_coords.w)) + vec2(0.5, 0.5)), int(light_id), (homogeneous_coords.z / homogeneous_coords.w)), vec2(0, 0), vec2(0,0));
return _expr26;
}

Expand All @@ -36,7 +36,7 @@ void main() {
vec3 color1 = vec3(0.05, 0.05, 0.05);
uint i = 0u;
while(true) {
if((i >= min(_group_0_binding_0.num_lights[0], 10u))) {
if((i >= min(_group_0_binding_0.num_lights.x, 10u))) {
break;
}
Light _expr21 = _group_0_binding_1.data[i];
Expand Down

0 comments on commit 6b3db1f

Please sign in to comment.