|
serial
|
 |
« on: November 20, 2007, 12:16:06 PM » |
|
So I started playing around with shaders a little bit this weekend. I bought a couple of books a few months ago. I read through the various tutorials and looked around online for other tutorials.
I got Zaks simple gradient shader working. I modified it to take a COLOR instead of DIFFUSE. So I could change that setting in the shader with a slider on a form. That is about as far as I got.
I tried the typical way of looking at what other people are doing to learn it sort of thing and came to the conclusion that there is no set way of doing things in shaders. Just like normal programming there is about a million differant ways of doing something and only a few really good ways of doing something.
A lot of the information gets in to more advanced shader programing to quickly. Mostly looking for tutorials that explain how to setup things like texture samplers.
I'm having a tough time of associating the purpose of SEMANTICS. Some of them seem to allocate registers and others seem more like they define a variable in the shader by some sort of type.
I'm starting to understand this stuff but I think I'm not fully understanding the purpose of semantics.
Does the VS output and PS Input need to match?
Most of my modifications so far have resulted in invisible objects so far.
|
|
|
|
|
Logged
|
|
|
|
|
Zaknafein
|
 |
« Reply #1 on: November 20, 2007, 12:57:27 PM » |
|
Sorry if you already read this, but I don't think you did, based on the question you ask. It's my tutorial I made about a year ago, goes from the beginning to a full Blinn-Phong shader. http://wiki.truevision3d.com/tutorialsarticlesandexamples/programming_hlsl_shadersIt explains texture sampling (although not in depth) and the "why" and "how" of semantics. Tell me if things aren't clear while going through it! Also if your object disppears, it probably means that the shader doesn't compile. You should try SFxE, the TV3D shader compiler, or ATi's RenderMonkey or nVidia's FX Composer. Or if you have Visual C++ 2003/2005, you can use the DirectX SDK's fxc.exe to compile them from the VC++ IDE : http://www.truevision3d.com/forums/shader_development/shader_faq-t6277.0.html;msg94308#msg94308
|
|
|
|
« Last Edit: November 20, 2007, 01:12:54 PM by Zaknafein »
|
Logged
|
|
|
|
|
sybixsus
|
 |
« Reply #2 on: November 20, 2007, 01:16:20 PM » |
|
Semantics are a way of having TV supply data directly to the shader without you having to use SetEffectParamVector3 and all those. Essentially, TV looks for certain semantics ( think of it like a second variable name ) and if it finds them, passes the correct data.
So, for example, if you apply a texture to your mesh in texture stage 0, then TV can pass that texture to the shader without you lifting a finger, so long as you use the TEXTURE0 semantic.
This holds true for textures, lights, materials, and all sorts of stuff, but it's particularly useful with the common matrices that shaders need. You'll notice many shaders use the WVP, or worldviewprojectionmatrix, and also the world matrix, view matrix, inverse view matrix etc. Well TV will pass all of these automatically if you use the correct semantic. ( Actually TV supports several variations of each semantic, so many shaders you find on the net will work without alteration. ) The Shader FAQ at the top of this forum has most of the semantics, if not all.
|
|
|
|
|
Logged
|
|
|
|
|
serial
|
 |
« Reply #3 on: November 20, 2007, 11:31:29 PM » |
|
Thank You for the information Yes I've gone through the tutorials a couple of times. I'm starting to understand more as I go through them. However, Its like I'm missing a few fundamental pieces of information. The tutorials themselves are helpful. However, they seem to assume that that reader have a certain level of knowledge with shaders. For example the Blinn-Phong shader uses a sampler. It doesn't really show where the sampler goes in the code. I've had to look around a little to figure that part out. The tutorial could use a copy of the completed shader maybe at the end of the tutorial so people can see where everything is supposed to go. I'm just playing around. I started off with the basic gradient shader and I'm attempting to convert it to a shader that just applies a texture to an object. I've looked at several other shaders that use texture and everything looks right to me. It seems like I'm missing something simple. I looked in the debug file and it has this error SHADER MANAGER : Error : g:\simple.fx(  : error X3000: syntax error: unexpected token 'samp' This is the shader float4x4 matWorldViewProj : WORLDVIEWPROJECTION;
texture tex : TEXTURE0; sampler2d samp = sampler_state { Texture = (tex); };
struct VS_INPUT { float4 position : POSITION; float2 texCoord : TEXCOORD0; };
#define VS_OUTPUT VS_INPUT #define PS_INPUT VS_INPUT VS_OUTPUT VS(VS_INPUT IN) { VS_OUTPUT OUT; OUT.position = mul(IN.position, matWorldViewProj); OUT.texCoord = IN.texCoord; return OUT; }
float4 PS(PS_INPUT IN) : COLOR { float4 rgbcolor = tex2d(samp, IN.texCoord); return rgbcolor;
}
technique TSM1 { pass P0 { VertexShader = compile vs_2_0 VS(); PixelShader = compile ps_2_0 PS(); } }
Its starting to make sense. Just can't figure this one out. Seems a little too vague.
|
|
|
|
|
Logged
|
|
|
|
|
Waterman
|
 |
« Reply #4 on: November 21, 2007, 01:53:58 AM » |
|
I can't see the problem in your simple.fx either. Are you sure the error belongs to this version of the shader? The TV3D debug log usually gives the number of the failing shader code row, where is that number?
The only difference compared my my own shaders are:
texture texSurface <-------- i usually set the texture from code (no ": TEXTURE0") <string TextureType = "2D";>; <------------ i usually have this; i say it's 2d
sampler2D samSurface = sampler_state {Texture = <texSurface>; <------------ i usually use < > to enclose the texture variable };
But your shader should be functional. Odd... Maybe the younger eyes find something :-).
|
|
|
|
|
Logged
|
Things should be described as simply as possible - but not simpler [A. Einstein]
|
|
|
jviper
Community Member

Posts: 2130
Discipline in training
|
 |
« Reply #5 on: November 21, 2007, 02:49:50 AM » |
|
Perhaps "samp" is a keyword
|
|
|
|
|
Logged
|
JAbstract.....Don't just imagine, make it happen!
|
|
|
|
Zaknafein
|
 |
« Reply #6 on: November 21, 2007, 07:07:49 AM » |
|
HLSL keywords are case sensitive, it's "sampler2D" with a capital D.  But you can drop the "2D", "sampler" does fine for 2D textures.
|
|
|
|
|
Logged
|
|
|
|
|
serial
|
 |
« Reply #7 on: November 21, 2007, 09:52:11 AM » |
|
Thanks for the help. It compiles now. Don't know if it works the way I want to yet. I will have to do some more work on this I think :-D.
However, yes I needed to be using 2D instead of 2d. The reason I had sampler2d was because I had tex2d in another place that was giving me problems. Same basic error for both. The description for the error tex2d was more descriptive. saying 'Unknown Function'
Will have to play around with this a little more tonight. Thank you for the help :-D
|
|
|
|
|
Logged
|
|
|
|
|
serial
|
 |
« Reply #8 on: November 21, 2007, 12:22:58 PM » |
|
If I do a mesh.settexture("texture") does that fill the TEXTURE0 semantic?
|
|
|
|
|
Logged
|
|
|
|
|
Zaknafein
|
 |
« Reply #9 on: November 21, 2007, 12:58:34 PM » |
|
Yup, and if you want to use 1/2/3 you need to use SetTextureEx and specify the layer.
|
|
|
|
|
Logged
|
|
|
|
|
serial
|
 |
« Reply #10 on: November 21, 2007, 11:18:35 PM » |
|
Thanks.
Well its working.
I'm attempting to use this shader on a landscape. However, Its tiling the shader output across the chunks.
So what is the best way to go about 'expanding' the shader over the entire terrain instead of each individual chunk?
|
|
|
|
|
Logged
|
|
|
|
|
Zaknafein
|
 |
« Reply #11 on: November 22, 2007, 12:15:01 AM » |
|
Have you tried ExpandTexture/ExpandSplattingTexture? The shader uses the landscape's texture coordinates which are setup by TV functions... Play with those and you should get what you want.
|
|
|
|
|
Logged
|
|
|
|
|
serial
|
 |
« Reply #12 on: November 22, 2007, 01:56:50 AM » |
|
I played with them. But they don't work on custom shaders very well. They seem to be coded with TVs internal Landscape shader in mind. They both require texture IDs to be input. ExpandSplattingLayer seems to be a bust because it looks like it maps it self to layers based on Textures ID and you can't specify a layer.
Its starting to looking like I'm going to have to do an ExpandTexture to give the shader the ability to texture the entire landscape, then do some UV stuff in the shader somehow.
Either that, or a few commands need to be added that will allow us to adjust the UVs of the landscapes.
|
|
|
|
|
Logged
|
|
|
|
|
serial
|
 |
« Reply #13 on: November 23, 2007, 03:29:48 AM » |
|
GOT IT WORKING!!
I used ExpandTexture(0,0,TotalNumberofChunks - 1) before setting my shaders textures. ExpandTexture works because it looks like its mapped to the semantic TEXCOORD0. ExpandSplattingTexture is probably mapped to the other TEXCOORD semantics but it uses texture IDs to referance those. I think I could use ExpandSplattingTexture if I were to use the AddSplattingLayers and set textures there. But its not an ideal solution.
I'm worried about ExpandTexture because I think I'm just lucky it works the way it does. The DEV team could change how it works in the future which would break my shader.
I'm curious if a command or overload of ExpandTexture could be added that would work like this.
ExpandTexture(Layer as Integer)
You would simply provide a layer number that would Expand the texture to cover the entire landscape.
I have a good start on my shader. It currently splats 4 textures and requires only one alphamap.
As expected my FPS is much higher using my shader. Not saying its better but it definatly is easier to use.
|
|
|
|
|
Logged
|
|
|
|
|
serial
|
 |
« Reply #14 on: November 23, 2007, 03:37:04 AM » |
|
Here is the code for the shader. Its a lot simpler than I thought it would be. I might be able to optimize it more. Use the SetTexureEX command 0 Alpha 1 Layer1 2 Layer2 3 Layer3 4 Layer4 Other Settings are available L1Scale L2Scale L3Scale L4Scale Will adjust the scale of the shader. Use SetEffectParameterFloat to set those. If I could get some experts to take a look at it and make suggestions on making it better I'd appriciate it. float4x4 matWorldViewProj : WORLDVIEWPROJECTION;
float L1Scale = 1; float L2Scale = 1; float L3Scale = 1; float L4Scale = 1;
texture alpha : TEXTURE0; texture layer1 : TEXTURE1; texture layer2 : TEXTURE2; texture layer3 : TEXTURE3; texture layer4 : TEXTURE4;
sampler2D alphaSamp = sampler_state { Texture = (alpha); AddressU = Wrap; AddressV = Wrap; };
sampler2D layerOne = sampler_state { Texture = (layer1); AddressU = Wrap; AddressV = Wrap; };
sampler2D layerTwo = sampler_state { Texture = (layer2); AddressU = Wrap; AddressV = Wrap; };
sampler2D layerThree = sampler_state { Texture = (layer3); AddressU = Wrap; AddressV = Wrap; };
sampler2D layerFour = sampler_state { Texture = (layer4); AddressU = Wrap; AddressV = Wrap; };
struct VS_INPUT { float4 position : POSITION; float2 AlphaTC : TEXCOORD0; };
#define VS_OUTPUT VS_INPUT #define PS_INPUT VS_INPUT VS_OUTPUT VS(VS_INPUT IN) { VS_OUTPUT OUT; OUT.position = mul(IN.position, matWorldViewProj); OUT.AlphaTC = IN.AlphaTC; return OUT; }
float4 PS(PS_INPUT IN) : COLOR { float4 l1 = tex2D(alphaSamp, IN.AlphaTC).r * tex2D(layerOne, IN.AlphaTC * L1Scale); float4 l2 = tex2D(alphaSamp, IN.AlphaTC).g * tex2D(layerTwo, IN.AlphaTC * L2Scale); float4 l3 = tex2D(alphaSamp, IN.AlphaTC).b * tex2D(layerThree, IN.AlphaTC * L3Scale); float4 l4 = tex2D(alphaSamp, IN.AlphaTC).a * tex2D(layerThree, IN.AlphaTC * L4Scale); float4 final = l1 + l2 + l3 + l4; return final;
}
technique TSM1 { pass P0 { VertexShader = compile vs_2_0 VS(); PixelShader = compile ps_2_0 PS(); } }
I'm very proud of this shader. Its my first attempt at a shader and think its going rather well. :-D
|
|
|
|
« Last Edit: November 23, 2007, 03:38:36 AM by serial »
|
Logged
|
|
|
|
|