Skip to content

Commit a506425

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

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
@@ -296,6 +296,7 @@ TESTSUITE ( aastep allowconnect-err and-or-not-synonyms arithmetic
296296
struct-isomorphic-overload struct-layers
297297
struct-operator-overload struct-return struct-with-array
298298
struct-nested struct-nested-assign struct-nested-deep
299+
swizzle
299300
ternary
300301
testshade-expr
301302
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
@@ -805,11 +805,37 @@ ASTstructselect::print (std::ostream &out, int indentlevel) const
805805

806806

807807

808-
ASTfieldselect* ASTfieldselect::create (OSLCompilerImpl *comp, ASTNode *expr,
809-
ustring field) {
808+
ASTNode* ASTfieldselect::create (OSLCompilerImpl *comp, ASTNode *expr,
809+
ustring field)
810+
{
810811
if (expr->typespec().is_structure_based())
811812
return new ASTstructselect (comp, expr, field);
812813

814+
const TypeSpec &type = expr->nodetype() != structselect_node ? expr->typespec() :
815+
static_cast<ASTstructselect*>(expr)->fieldsym()->typespec();
816+
817+
if (type.aggregate() == TypeDesc::VEC3) {
818+
int component = -1;
819+
switch (field[0]) {
820+
case 'r': component = 0; break;
821+
case 'g': component = 1; break;
822+
case 'b': component = 2; break;
823+
824+
case 'x': component = 0; break;
825+
case 'y': component = 1; break;
826+
case 'z': component = 2; break;
827+
828+
default: break;
829+
}
830+
if (component != -1) {
831+
if (expr->nodetype() == index_node) {
832+
static_cast<ASTindex*>(expr)->extend(new ASTliteral (comp, component));
833+
return expr;
834+
}
835+
return new ASTindex (comp, expr, new ASTliteral (comp, component));
836+
}
837+
}
838+
813839
comp->error (comp->filename(), comp->lineno(),
814840
"type '%s' does not have a member '%s'",
815841
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
@@ -580,6 +580,13 @@ class ASTindex : public ASTNode
580580
ustring destname, ustring srcname,
581581
Symbol *index);
582582

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

601608
public:
602-
static ASTfieldselect* create (OSLCompilerImpl *comp, ASTNode *expr,
603-
ustring field);
609+
static ASTNode* create (OSLCompilerImpl *comp, ASTNode *expr, ustring field);
604610

605611
ustring field () const { return m_field; }
606612
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)