customIPP FX

Re: customIPP FX

Postby Skinnytorus » Saturday, 10.November 2012, 10:32

I'm trying to "dissect" the lightning shader by remming out some parts of it, but once I rem out any part, it fails to compile. Are there any rules for simpifying shaders?

EDIT: I opened shader code and found this: dx: "...error X3004: undeclared identifier 'time'\n". Where do you declare this variable?
Skinnytorus
Operator
Operator
 
Posts: 1300
Joined: Monday, 06.February 2012, 17:46

Re: customIPP FX

Postby Skinnytorus » Saturday, 10.November 2012, 11:36

OK. Got it. Found all the variables! :)
But I can't figure out how you draw a simple sine wave to start with?...
Skinnytorus
Operator
Operator
 
Posts: 1300
Joined: Monday, 06.February 2012, 17:46

Re: customIPP FX

Postby ikam » Saturday, 10.November 2012, 15:54

here an example adapted from code here : http://glsl.heroku.com/e#4237.0

Code: Select all
sampler2D s0 : register(s0);

uniform float time : register(c9);  // var0
uniform float4 var1 : register(c10); // var1
uniform float4 var2 : register(c11); // var2
uniform float4 var3 : register(c12); // var3
uniform float4 var4 : register(c13); // var4



void main
(
 in float2 uv0 : TEXCOORD0,
 out float4 color0 : COLOR0,
 in float2 vpos : VPOS
)
{
 float scale = 16.0;
 float amplitude = 2.0;
 float wavelength = 0.8;
 float thickness = 0.01;
 float speed = 2.3;
 float glow = 4.0;
 
 // screen center
 float2 p = ( vpos.xy / resolution.xy * scale );
 p.y -= 0.5 * scale;
 
 // color
 float c = 0.0;
 
 float t = time * speed;
 p.y += sin( p.x * wavelength + t ) * amplitude;
 c += abs( thickness / p.y );

 // final color
 color0 = float4( c, c, c * glow, 1.0 );
}


Play with different math operations instead of mul and you will see differents results.
ikam
Operator
Operator
 
Posts: 911
Joined: Friday, 14.October 2011, 13:00
Location: France

Re: customIPP FX

Postby Skinnytorus » Saturday, 10.November 2012, 17:12

Wow! Thanks. :) I just can't understand how the sine is drawn without any input texture (pixel shader needs an input to work with, right?). Can you explain how it's done in some more detail?
Skinnytorus
Operator
Operator
 
Posts: 1300
Joined: Monday, 06.February 2012, 17:46

Re: customIPP FX

Postby ikam » Saturday, 10.November 2012, 17:58

Yop

nop, a pixel shader doesn't need a texture to work, it role is mainly to compute the color for each pixel on screen :p

for example :

Code: Select all
void main
(
 in float2 uv0 : TEXCOORD0,       
 out float4 color0 : COLOR0,
 in float2 vpos : VPOS
)
{
 // final color
 color0 = float4(1, 0, 0, 1);
}


simply colorize every pixel in red, the float4 color0 is final the rgba color for each pixel on screen, varying each channel color from 0 to 1, so we can say that 1 is the 255 usual max value used in rgb code, example for red color => 255,0,0.

uv0 and vpos are used to compute the pixel position
- uv0 with uv values (screen range from 0 to 1)
- vpos with screen dimensions (screen resolution in real pixel size)

so if you write for example :

Code: Select all
color0 = float4(1*uv0.y,0,0,1);


you obtain a black/red gradiant on height (uv0.y) because uv0.y is varying from 0 to 1 (0 is top, 1 is bottom of screen), so if multiply uv0.y per 1 result varying from 0 to 1 (exemple in middle screen => 0.5*1 = 0.5, so color of each pixel at screen center will be 0.5 for the red channel, so a dark red).

another example with vpos (real screen dimension), if you write :

Code: Select all
if(vpos.x > 400)
  color0 = float4(1,0,0,1);


it said, if current pixel position at screen.x position is greater than 400 so we colorize it in red.

you can compute position ranging from 0 to 1 for vpos if you divide it per screen resolution :

Code: Select all
float p = vpos.x / resolution.x;
color0 = float4(1*p,0,0,1);


and you obtain the same gradiant effect but based on screen dimension, it usefull to compute real shape size based on screen aspect.

So the role of the game is to compute for each pixel a color using its coordinate.

Using textures in shaders is an option, and works the same, you "read" the pixel color on the texture and you do what you want with (mix this color with the current screen color, substract, add etc... all rest is mathematics)

for example :

Code: Select all
sampler2D s0 : register(s0);

void main
(
 in float2 uv0 : TEXCOORD0,
 out float4 color0 : COLOR0,
 in float2 vpos : VPOS
)
{
 color0 = float4(1*uv0.x,0,0,1) * tex2D(s0,uv0);
}


tex2D return the pixel color of the s0 texture (here s0 is the current screen render, so actual screen pixels colors) using the uv0 coordinates (so the screen uv's coord) and we multiply this pixel color result with the current pixel color of screen.

So stack a cube mesh instead of a null scene node on customIPP input and the gradiant will be applied on the cube and the whole screen.

test with another textures on customIPP input

declared with :

Code: Select all
sampler2D s1 : register(s1);


and multiply again the result of this texture :

Code: Select all
 color0 = float4(1*uv0.x,0,0,1) * tex2D(s0,uv0) * tex2D(s1,uv0);


test different math operations instead mul, and you will experiment differents results.
ikam
Operator
Operator
 
Posts: 911
Joined: Friday, 14.October 2011, 13:00
Location: France

Re: customIPP FX

Postby Skinnytorus » Saturday, 10.November 2012, 19:12

Thank you so much for such a detailed post. Veeery handy for my shader background! :)
But I should have made myself a bit clearer though: In fact I wanna know the principle of drawing simple primitives (cube, triangle, circle, etc.) and curves on screen with pixel shader. Could you give me a couple of simple examples?
Skinnytorus
Operator
Operator
 
Posts: 1300
Joined: Monday, 06.February 2012, 17:46

Re: customIPP FX

Postby erbsen » Sunday, 11.November 2012, 10:49

This is some pretty serious stuff :-k but anyway thanks for your explanation. :)
erbsen
Operator
Operator
 
Posts: 873
Joined: Thursday, 15.July 2004, 23:00
Location: Os

Re: customIPP FX

Postby ikam » Sunday, 11.November 2012, 21:03

all is a question of pixel color at a specific position (uv based or vpos based).

so to draw a shape just determine where are the pixels you want to colorize. you do this with math operations, there is not really given functions like drawline, drawcube or something like that.

look at this code : http://www.iquilezles.org/apps/shadertoy/?p=shapes
ikam
Operator
Operator
 
Posts: 911
Joined: Friday, 14.October 2011, 13:00
Location: France

Re: customIPP FX

Postby Skinnytorus » Monday, 12.November 2012, 07:34

Thank you. I will try to at least understand the principles.
Skinnytorus
Operator
Operator
 
Posts: 1300
Joined: Monday, 06.February 2012, 17:46

Re: customIPP FX

Postby CybeREX » Friday, 06.June 2014, 20:28

First try with shader code...
Here https://www.shadertoy.com/new
I have example - simplest code ever I think =) I let try to convert in to wz4 code...
Code: Select all
uniform vec3      iResolution;           // viewport resolution (in pixels)
uniform float     iGlobalTime;           // shader playback time (in seconds)
uniform float     iChannelTime[4];       // channel playback time (in seconds)
uniform vec3      iChannelResolution[4]; // channel resolution (in pixels)
uniform vec4      iMouse;                // mouse pixel coords. xy: current (if MLB down), zw: click
uniform samplerXX iChannel0..3;          // input channel. XX = 2D/Cube
uniform vec4      iDate;                 // (year, month, day, time in seconds)

void main(void)
{
   vec2 uv = gl_FragCoord.xy / iResolution.xy;
   gl_FragColor = vec4(uv,0.5+0.5*sin(iGlobalTime),1.0);
}


Here is what I get:
dx: "D:\\Demoscene\\Wz4CE_dx9_x86\\Shader@0x00B7F540(24,19): error X3000: syntax error: unexpected token '-'\n"

Code: Select all
uniform float time : register(c9);  // var0
uniform float4 var1 : register(c10); // var1
uniform float4 var2 : register(c11); // var2
uniform float4 var3 : register(c12); // var3
uniform float4 var4 : register(c13); // var4


void main
(
 in float2 uv0 : TEXCOORD0,
 out float4 color0 : COLOR0,
 in float2 vpos : VPOS
)
{
   float2 uv = vpos.- 0.5 / resolution.xy;
   color0 = float4(uv,0.5+0.5*sin(Time),1.0);
}


what I do wrong?
PS I use custom IPP to write shader code with pre installed vertex shader in tutorial - custom IPP section.
I didn't write any code between =) But shader really surprise me.
CybeREX
Demomaker
Demomaker
 
Posts: 154
Joined: Thursday, 03.April 2014, 07:44

Re: customIPP FX

Postby CybeREX » Friday, 06.June 2014, 20:44

After search code again, I finnaly get it to work - here is results
Code: Select all
uniform float time : register(c9);  // var0
uniform float4 var1 : register(c10); // var1
uniform float4 var2 : register(c11); // var2
uniform float4 var3 : register(c12); // var3
uniform float4 var4 : register(c13); // var4


void main
(
 in float2 uv0 : TEXCOORD0,
 out float4 color0 : COLOR0,
 in float2 vpos : VPOS
)
{
   float2 uv = vpos.xy / resolution.xy;
   color0 = float4(uv,0.5+0.5*sin(time),1.0);
}

FirstGLSLtoHLSL.jpg
You do not have the required permissions to view the files attached to this post.
CybeREX
Demomaker
Demomaker
 
Posts: 154
Joined: Thursday, 03.April 2014, 07:44

Re: customIPP FX

Postby CybeREX » Friday, 06.June 2014, 21:05

We'll, go to the next level and pick up more complex code =)
Code: Select all
uniform vec3      iResolution;           // viewport resolution (in pixels)
uniform float     iGlobalTime;           // shader playback time (in seconds)
uniform float     iChannelTime[4];       // channel playback time (in seconds)
uniform vec3      iChannelResolution[4]; // channel resolution (in pixels)
uniform vec4      iMouse;                // mouse pixel coords. xy: current (if MLB down), zw: click
uniform samplerXX iChannel0..3;          // input channel. XX = 2D/Cube
uniform vec4      iDate;                 // (year, month, day, time in seconds)

void main()
{
   float v,t=v=.001;
   for (float s=.0; s<2.; s+=.01) {
      vec3 p=s*gl_FragCoord.xyz*t+vec3(.1,.2,fract(s+floor(iGlobalTime*25.)*.01));
      for (int i=0; i<8; i++) p=abs(p)/dot(p,p)-.8;
      v+=dot(p,p)*t;
   }
   gl_FragColor=vec4(v);
}


Translate to wz4 and I get error with xyz...
ikam master guru of code, what is wrong with xyz?
Code: Select all
uniform float time : register(c9);  // var0
uniform float4 var1 : register(c10); // var1
uniform float4 var2 : register(c11); // var2
uniform float4 var3 : register(c12); // var3
uniform float4 var4 : register(c13); // var4


void main
(
 in float2 uv0 : TEXCOORD0,
 out float4 color0 : COLOR0,
 in float2 vpos : VPOS
)
{
   float v,t=v=.001;
   for (float s=.0; s<2.; s+=.01) {
      float3 p=s*vpos.xyz*t+float3(.1,.2,fract(s+floor(time*25.)*.01));
      for (int i=0; i<8; i++) p=abs(p)/dot(p,p)-.8;
      v+=dot(p,p)*t;
   }
   color0=float4(v);
}


dx: "D:\\Demoscene\\Wz4CE_dx9_x86\\Shader@0x0F463E20(27,19): error X3018: invalid subscript 'xyz'\n"
CybeREX
Demomaker
Demomaker
 
Posts: 154
Joined: Thursday, 03.April 2014, 07:44

Re: customIPP FX

Postby ikam » Saturday, 07.June 2014, 06:16

nice job. Yes shaders are awesomes and you can produce really cool scenes or materials with them in wz.
I encourage you to continue to learn it and trying porting glsl examples to hlsl is a also good exercice ;)


First, to identify precisly the error line, when you display shadercode log, remember to use second colum of number in editor...

Errors in your code :

1/
Code: Select all
vpos.xyz

vpos is declared as a float2 (here : in float2 vpos : VPOS)
so you can't use the .z component in .xyz. VPOS can't be declared as float3 in hlsl so to fix code you can do that :
Code: Select all
float3(vpos.xy,1) or float3(vpos,1)  (it is the same)
it will convert a float2 to float3 with the .z set to 1.
A float3 is composed of 3 floats : float3(x,y,z) - where x y and z are 1 float.
but you can use a float2 to register a float3 : float3(float2, float) or float3(float2.x, 1, float2.y).... etc...


2/ "fract" function equivalent in hlsl is "frac", and it use same parameters, so just replace it.
doc : http://msdn.microsoft.com/en-us/library ... 85%29.aspx


3/
Code: Select all
color0=float4(v);

In glsl you can do things like that : float4 myvar = float4(1); it will set all components to 1 (x,y,z and w for a float4)
but in hlsl you need to specify all elements : float4 myvar = float4(1,1,1,1);
so fix it with : color0=float4(v,v,v,v);


complete fixed code if needed :

Code: Select all
uniform float time : register(c9);  // var0
uniform float4 var1 : register(c10); // var1
uniform float4 var2 : register(c11); // var2
uniform float4 var3 : register(c12); // var3
uniform float4 var4 : register(c13); // var4


void main
(
 in float2 uv0 : TEXCOORD0,
 out float4 color0 : COLOR0,
 in float2 vpos : VPOS
)
{
   float v,t=v=.001;
   for (float s=.0; s<2.; s+=.01) {
      float3 p=s*float3(vpos.xy,1)*t+float3(.1,.2,frac(s+floor(time*25.)*.01));
      for (int i=0; i<8; i++) p=abs(p)/dot(p,p)-.8;
      v+=dot(p,p)*t;
   }
   color0=float4(v,v,v,v);
}
ikam
Operator
Operator
 
Posts: 911
Joined: Friday, 14.October 2011, 13:00
Location: France

Re: customIPP FX

Postby ikam » Saturday, 07.June 2014, 07:20

I used your shader example to show howto use it in others ways than customipp : viewtopic.php?f=42&t=1447
ikam
Operator
Operator
 
Posts: 911
Joined: Friday, 14.October 2011, 13:00
Location: France

Re: customIPP FX

Postby ikam » Saturday, 07.June 2014, 09:30

I forgot, if you want to center the effect in previous example add this at beginning of main function :
Code: Select all
float2 center = -1.0 + 2.0 * vpos.xy / resolution.xy;
vpos = center * 1000;
ikam
Operator
Operator
 
Posts: 911
Joined: Friday, 14.October 2011, 13:00
Location: France


Return to Tutorials



Who is online

Users browsing this forum: No registered users and 30 guests

cron