Jump to content

"vector a={0,1,0}"or"vector a=(0,1,0)?


Recommended Posts

#pragma hint clr1 color
#pragma hint clr2 color
surface
shader04(vector clr1 = {1,0,0},clr2 = {0,0,1})
{
vector p1 = (0.5,0.5,0);
vector p2;
p2.x = s;
p2.y = t;

float dis = distance(p1,p2);
Cf = lerp(clr1,clr2,dis);
}

but when i input such expression as "vector p1 = (0.5,0.5,0)",no errors will take place,i don't know what's the reason.is it a exception?thx

Link to comment
Share on other sites

but when i input such expression as "vector p1 = (0.5,0.5,0)",no errors will take place,i don't know what's the reason.is it a exception?thx

Interesting. I thought the compiler would catch this and give a syntax error, but it appears that it lets it pass (even with full warnings turned on).

However, it *is* a syntax error, so don't write it like that :)

You will notice that even though the compiler doesn't complain, the assignment is incorrect -- it will get a constant value of 0.5 instead of {0.5,0.5,0}.

The opcode generated for this shader:

surface test () { Cf = (0.1,0.2,0.3); }

is the following:

_name   test
	_decl	&constcf0	float	const	0.1
	set@VI	Cf	&constcf0
_code_end

Which clearly shows that vcc interprets the "(0.1,0.2,0.3)" expression as a single float (whose type it inferred from the number), and assigns it the value of the first member in the bracketed list. In other words, not what you intended.

I should submit a bug I think...

Link to comment
Share on other sites

Er, this compiles fine on my C compiler as well:

int main() { double a = (0.1, 0.2, 0.3); return 0; }

The reason it works is because of the way the comma operator works in C, it's supposed to return the rightmost value. vcc is just basically a C parser.

Now for a real life story. At a place I worked at, there was a co-op who insisted on writing C code that did not use braces through judicious abuse of the comma operator. ie. we had functions, if blocks, loops, etc written like this:

if (a > 5)
		printf("%d\n", a),
		printf("another statement"),
		printf("end of if");

This resulted in code impossible to debug (in Visual Studio) because the IDE would think it's all on 1 line.

Link to comment
Share on other sites

Now for a real life story. At a place I worked at, there was a co-op who insisted on writing C code that did not use braces through judicious abuse of the comma operator. ie. we had functions, if blocks, loops, etc written like this:

Oh man... that's just whacked.

I've never seen the comma operator abused like that -- much less done on purpose while writing actual code. Except maybe for multiple initializations in the params of a for loop, as in for (i=0, j=10; <cond>; i++, j-- ), but that's about it.

Still. There seem to be some differences between the C parser and vcc:

In C, the following evaluates to 0.3:

double a = (0.1,0.2,0.3);

Whereas in VEX, a similar assignment evaluates to 0.1:

surface test() { Cf = (0.1,0.2,0.3); }
//
// ---- this produces the opcode:
// _name   test
//	 _decl	&amp;constcf0	float	const	0.1
//	 set@VF	Cf	&amp;constcf0
// _code_end

Also, in VEX, an attempt to write that crazy assignment as a default for the parameter of a context function *does* produce the expected syntax error.

This:

surface test( vector a = (0.1,0.2,0.3) ) { }

Generates this:

"test.vfl" line 1 ERROR (1000) Syntax error
surface test( vector a = (0.1,0.2,0.3) ) { }
										   ^

So it would seem that vcc has the ability to detect the problem (at least in some cases), and I think it would be better if it always flagged it as a syntax error, don't you think? (unless all people writing VEX are like your co-op friend and like to do this kind of thing on purpose :) )

Link to comment
Share on other sites

The funny thing was that this co-op was off-stream from me while I was a co-op at the time (ie. I came and did 4 months, right after he finished his 4 months). Apparently no one had noticed that he did all this code in this clever style while he was there. It was discovered after he left. So for weeks afterwards, on various people's weekly reports, there would be a bullet point regarding the elimination of the comma operator. :) Incidentally, this same person also seemed to have this idea that putting multiple statements within a line would produce more optimized code because everywhere, there would be embedded use of equal operators. I can't come up with something right now but along the lines of stuff like this

a = (b = c++) + 5;

Very hard to read and nearly impossible to debug. For one particular piece of his code that I was tasked to fix, I gave up and just rewrote it. :)

Link to comment
Share on other sites

At a place I worked at, there was a co-op who insisted on writing C code that did not use braces through judicious abuse of the comma operator.

Sounds like someone needed to be whacked over the head with a big book entitled, "Just because you can do something...."

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...