Skip to content

Commit a95adca

Browse files
committed
Add r,g,b and x,y,z accessors to triple types.
1 parent 28f0f18 commit a95adca

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ TESTSUITE ( and-or-not-synonyms aastep arithmetic array array-derivs array-range
291291
struct-isomorphic-overload struct-layers
292292
struct-operator-overload struct-return struct-with-array
293293
struct-nested struct-nested-assign struct-nested-deep
294+
swizzle
294295
ternary
295296
testshade-expr
296297
texture-alpha texture-blur texture-connected-options

src/liboslcomp/ast.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,11 +719,37 @@ ASTstructselect::print (std::ostream &out, int indentlevel) const
719719

720720

721721

722-
ASTfieldselect* ASTfieldselect::create (OSLCompilerImpl *comp, ASTNode *expr,
723-
ustring field) {
722+
ASTNode* ASTfieldselect::create (OSLCompilerImpl *comp, ASTNode *expr,
723+
ustring field)
724+
{
724725
if (expr->typespec().is_structure_based())
725726
return new ASTstructselect (comp, expr, field);
726727

728+
const TypeSpec &type = expr->nodetype() != structselect_node ? expr->typespec() :
729+
static_cast<ASTstructselect*>(expr)->fieldsym()->typespec();
730+
731+
if (type.aggregate() == TypeDesc::VEC3) {
732+
int component = -1;
733+
switch (field[0]) {
734+
case 'r': component = 0; break;
735+
case 'g': component = 1; break;
736+
case 'b': component = 2; break;
737+
738+
case 'x': component = 0; break;
739+
case 'y': component = 1; break;
740+
case 'z': component = 2; break;
741+
742+
default: break;
743+
}
744+
if (component != -1) {
745+
if (expr->nodetype() == index_node) {
746+
static_cast<ASTindex*>(expr)->extend(new ASTliteral (comp, component));
747+
return expr;
748+
}
749+
return new ASTindex (comp, expr, new ASTliteral (comp, component));
750+
}
751+
}
752+
727753
comp->error (comp->filename(), comp->lineno(),
728754
"type '%s' does not have a member '%s'",
729755
comp->type_c_str(expr->typespec()), field);

src/liboslcomp/ast.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,13 @@ class ASTindex : public ASTNode
579579
ustring destname, ustring srcname,
580580
Symbol *index);
581581

582+
/// Add another index: obj[current][extended]
583+
///
584+
void extend (ASTNode *ext) {
585+
ASSERT (nchildren() < 4);
586+
m_children.emplace_back(ext);
587+
}
588+
582589
ref lvalue () const { return child (0); }
583590
ref index () const { return child (1); }
584591
ref index2 () const { return child (2); }
@@ -598,8 +605,7 @@ class ASTfieldselect : public ASTNode
598605
ustring m_fullname; ///< Full name of variable and field
599606

600607
public:
601-
static ASTfieldselect* create (OSLCompilerImpl *comp, ASTNode *expr,
602-
ustring field);
608+
static ASTNode* create (OSLCompilerImpl *comp, ASTNode *expr, ustring field);
603609

604610
ustring field () const { return m_field; }
605611
ustring fullname () const { return m_fullname; }

testsuite/swizzle/ref/out.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Compiled swizzle.osl -> swizzle.oso
2+
c.r: 4
3+
c.g: 3
4+
c.b: 2
5+
v0.x: 6
6+
v0.y: 7
7+
v0.z: 8
8+
v0.z->x: 6
9+
c4[0].r: 1
10+
c4[0].g: 2
11+
c4[0].b: 3
12+
c4[1].r: -1
13+
c4[1].b: -3
14+
c4[1].g: -2
15+

testsuite/swizzle/run.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python
2+
3+
command = testshade("swizzle")
4+

testsuite/swizzle/swizzle.osl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Test swizzle statements
2+
3+
#include "color4.h"
4+
5+
shader swizzle ()
6+
{
7+
color c = color(4,3,2);
8+
printf("c.r: %g\n", c.r);
9+
printf("c.g: %g\n", c.g);
10+
printf("c.b: %g\n", c.b);
11+
12+
vector v0 = vector(6,7,8);
13+
printf("v0.x: %g\n", v0.x);
14+
printf("v0.y: %g\n", v0.y);
15+
printf("v0.z: %g\n", v0.z);
16+
v0.z = v0.x;
17+
printf("v0.z->x: %g\n", v0.r);
18+
19+
color ca[2] = { color(-1,2,3), color(1,-2,-3) };
20+
color4 c4[2]; // FIXME: = { color4(color(1,2,3),4), color4(color(-1,-2,-3),-4) };
21+
22+
for (int i = 0; i < 2; ++i) {
23+
ca[i].r = -ca[i].r;
24+
c4[i].rgb = color(ca[i].x, ca[i].y, ca[i].z);
25+
printf("c4[%d].r: %g\n", i, c4[i].rgb.r);
26+
printf("c4[%d].%s: %g\n", i, i ? "b" : "g", i ? c4[i].rgb.b : c4[i].rgb.g);
27+
printf("c4[%d].%s: %g\n", i, i ? "g" : "b", i ? c4[i].rgb.g : c4[i].rgb.b);
28+
}
29+
}

0 commit comments

Comments
 (0)