Game Development Community

Brick Shader

by Anthony Rosenbaum · in Torque Game Engine Advanced · 06/26/2004 (7:11 am) · 22 replies

Over the past few days I've been working on a Brick shader. However I have come into some problems. First off my card supports vs1.1 and ps 1.1 shaders. The Brick algorithm I found requires the texCoord to be past to the pixel shader. The brick color is determined from the current TexCoord.

I am not sure if I am missing something or doing it wrong but I keep getting errors pertaining to the passed texcoords. If I pass a copy of the texcoords into a TEXCOORD1 semantic I get this error in the fragment shader

Quote:
warning X4704: literal values outside range -1 to 1 are clamped on all ps_1_x shading models
warning X4704: literal values outside range -1 to 1 are clamped on all ps_1_x shading models
warning X4704: literal values outside range -1 to 1 are clamped on all ps_1_x shading models
error X4507: program too complex: exceeded available constant registers.

If I use the actual texcoord passes in TEXCOORD0 I get this error. also in the fragment shader
Quote:
error X4520: can read from texcoord and use it for texlookup only in ps_1_4 and higher

Other things to note, how do we write const variables in? or alternate uniform or varying variables in my case brick height, mortar size, and color. Right now I am declaring them as another local variables inside the fragment shader which I think is consuming precious fragment operations.

I had used the algorithm in the vertex shader but it didn't want to work there either ( I think because there are only 4 vertex/side, ie not enough data )

Is it even possible to achomplish on this level of shader?

Anthony
Page «Previous 1 2
#1
06/26/2004 (10:39 am)
Can you post your shader code? You're probably trying to do stuff that's too complex for your card's capabilities.

You may need to just use a brick texture.
#2
06/26/2004 (10:47 am)
Sure here it is

vertex
#define IN_HLSL
#include "shdrConsts.h"
//-----------------------------------------------------------------------------
// Constants
//-----------------------------------------------------------------------------
struct VertData
{
	float4 position        : POSITION;
    float4 texCoord        : TEXCOORD0;
    float2 altCoord          : TEXCOORD1;
};


struct Conn
{
   float4 HPOS              : POSITION;
   float2 outTexCoord       : TEXCOORD0;
   float2 altCoord          : TEXCOORD1;
};


//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
Conn main( VertData In, uniform float4x4 modelview : register(VC_WORLD_PROJ),
			            uniform float4x4 texMat    : register(VC_TEX_TRANS1)
)
{
   Conn Out;
  Out.HPOS      = mul(modelview, In.position);
  Out.outTexCoord = mul(texMat, In.texCoord);
  Out.altCoord  =  mul(texMat, In.texCoord);
  return Out;
}
Pixel
//-----------------------------------------------------------------------------
// Structures                                                                  
//-----------------------------------------------------------------------------
struct ConnectData
{
   float2 texCoord          : TEXCOORD0;
   float2 altCoord          : TEXCOORD1;
};

struct Fragout
{
   float4 col : COLOR0;
};


//-----------------------------------------------------------------------------
// Main                                                                        
//-----------------------------------------------------------------------------
Fragout main(ConnectData IN,  uniform sampler2D baseTex      : register(S0)
)
{

   Fragout OUT;
   float2 altTex = IN.altCoord;
   
   float4 brickColor  = {0.5, 0.15, 0.1, 1.0 };
   float4 mortarColor = {0.5, 0.5, 0.5, 1.0 };
 	float width = 0.25;
	float height = 0.1; 
	float mortar = 0.01;

	float row = altTex[1] / height;

	float u_texCoor = altTex[0];
	
	if(row % 2 == 0)
		u_texCoor += width/2;
		
	float colu = u_texCoor /width;
	
	float2 brickCoord;
	brickCoord[0] = u_texCoor % width ;
	brickCoord[1] = altTex[1] % height;
	float4 col = brickColor;
	if(brickCoord[0] < mortar || brickCoord[1] < mortar)
		col = mortarColor;

 
 
   float4 diffuseColor = tex2D( baseTex, IN.texCoord );
   OUT.col = diffuseColor * col;
   return OUT;
}
#3
06/26/2004 (9:11 pm)
Yeah... That's going to expand to some pretty complicated shader code. Have you tried running it through the shader assembly tool that ships with DX9 to see what sort of assembly you're getting?
#4
06/27/2004 (4:47 am)
No, but which tool is that exactly?
#5
06/27/2004 (6:58 am)
Fxc, I believe.
#6
06/27/2004 (9:19 am)
Also consider one of the 3rd party shader tools from ATI (Render Monkey) or Nvidia (FXComposer). Like FXC, they also give line numbers when detecting errors, but they are more interactive and have a graphical user interfaced (FXC is a command line tool). There are also some non-hardware vender shader apps that do the same actions.
#7
06/27/2004 (9:19 am)
Wish I could help with the problem, but I've only had TSE and TGE for a few days so I'm a rank newbie :)

I did set up a shader and set the version to 2.0 though and it worked. Setting the pixel version to 1.1 gave me the same errors you got though.

J
#8
06/27/2004 (10:08 am)
SO the briock shader worked on 2.0? sign I need a new card
#9
06/27/2004 (10:13 am)
The problem is all the if statements. They multiply instruction count immensely, since the shader compiler has to fake the branching. With a bit of effort you could probably find a way to boil it down to a slightly more complex equation with many fewer branches.
#10
06/27/2004 (10:32 am)
But what about the literal values outside range -1 to 1 are clamped on all ps_1_x shading models
error
#11
06/27/2004 (11:39 am)
I believe, in ps_1_x, as the message states, all texture coordinates are clamped, which means passing values greater than 1 E.g. will be clamped. In your case, I don't think it will make any difference as you are only useing texture coords as texture coords and not as other data, though if the Texture Matrix does something funky, you might get unexpected results.
#12
06/27/2004 (11:53 am)
I read in 8.0 shaders which is what 1.1 are I belive, there is no support for branching at all . . .gotta rethink things
#13
06/27/2004 (1:26 pm)
Right...no support for branching, so instead when you compile the HLSL, every time the shader runs it does BOTH sides of the if statement, and then just applies the actual results achieved only if the condition is met. To do this takes a bit of wriggling on the compiler's part.
#14
06/27/2004 (9:24 pm)
And branching in general is bad for pixel shaders - because the branches can't be parallelized as efficiently. Perhaps some day the hardware will be able to happily deal with multiple pixel pipelines executing different branches of the code... but right now even PS3.0 hardware isn't going to be too thrilled with a complex branching shader.

Anyway, that's not to say that you can't do this shader on PS1.1 hardware, just that you're going to have to be very clever.
#15
09/03/2004 (3:44 pm)
Goodie Goodie I got my new computer and have finally gotten some sucess with the brick shader.
Check it out here

www.afterschoolcartoons.com/pres/brickShader.jpg
It isn't perfect but a start next it to get lighing involved. Hopefully I'll have something to show at IGC!!!
#16
09/04/2004 (2:01 pm)
Very cool... Nice job, Anthony. :)
#17
10/16/2004 (6:59 pm)
If your budget is low you can try a Nvidia 5200 card. Good solid GPU and support for 1 and 2.0x shaders.
#18
10/16/2004 (8:45 pm)
Ok. here's a real newbie question...

Is that brick texture being generated by the shader?
#19
10/17/2004 (4:53 am)
Yes it is Gonzo
#20
10/17/2004 (5:32 am)
Well, color me brick headed, I didn't realize shaders were quite so interesting. I thought they were more for texturing effects like glowing or coloring and such. That rocks.


I know that was just a test, but I would increase your horizontal to 2.75 for a more realistic looking brick pattern. Still that rocks.
Page «Previous 1 2