Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / help with passing texture to graphics pipeline (shader) + other stuff

Author
Message
Aralox
19
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 24th Sep 2010 15:37
Hey guys, Ive been trudging along on my learn-to-write-shaders quest for about a week now, and have been making heaps of progress thanks to the wealth of info here and online in general. Ive only been working with FXComposer however, and have only started to intergrate my shaders with DBP.

Question 1: I want to use a texture loaded in and assigned to an object within darkbasic in a shader (eg. just a basic texturing shader), rather than specifying the file in the shader. I know i can set that parameter in load effect (http://forum.thegamecreators.com/?m=forum_view&t=172111&b=1) but how do i then access the set object texture from within the shader? this looks like the code for it:
(which is subsequently followed by a sampler), but how does it work? what does the <> do?

Question 2: Is it possible to apply the same shader (eg diffuse light) to multiple objects, but with different textures?

What i want to achieve is:
-different textures for each limb in my Map object
-movable/rotatable lights (eg flashlight) within my game (not actual lights, just points and rotations stored within a dummy object (or a matrix, but im not too sure about that))
-passing data about these 'lights' to the shader(s) which will light the texture of the Map object limbs accordingly
-I want to achieve this using .x files, created in blender. I dont want to build my level in another program, and texture it in there (and probably exporting it as a .dbo with all my textures in one nice file). Doing that means i will never really have full control over whats going on, and i wont learn the intricacies of handling textures with shaders.

I tried (and am trying) having a look at Evolved's flashlight shader code, but yeah i need to get past the texture-passing, and

Question 3: what is the significance or need for the multiple passes in his shader?

Sorry for the long post. Im only posting because i really need help (usually the search function solves my problem for me )
Thanks,
Aralox



http://www.freewebs.com/aralox/
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 24th Sep 2010 17:17 Edited at: 24th Sep 2010 17:18
A1:

The textures are automatically filled by DBP. By declaring your first texture, it will be assigned to the texture register 00, which is texture stage 0 for DBP.

To actually use the texture, you will need a sampler.


Afterwards, you can use tex2D() to sample this texture, and find the color that corresponds with certain UV coordinates, usually passed by the function. An example of a pixel shader could be:


(produces desaturated images)

TEXCOORD0 holds the texture coordinates (UV-coordinates).

The color that tex2D() returns, depends on the sampler states of that sampler. Those sampler states will determine if the GPU just uses the closest pixel it can find, or if it will sample the color from all neighboring pixels (linear, anisotropic, etc.) You can specify those with the creation of the sampler:



A2:
Yes. The texture used by the pixel shader depends on the texture that is currently set by the Direct3D device. DBP does this for us of course, which is why we never notice it actually works like that. In our example, it means that g_Tex will always be the texture of the object that is currently rendered.

By creating a new texture, you're actually creating a reference to the next texture stage. So if you made 3 textures in your shader, it would refer to texture stage 0, 1 and 2.

For any shader geniuses out there, feel free to correct me. It is what I have understood from it up till now.

Cheers!
Sven B

Dr Tank
17
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 24th Sep 2010 21:28 Edited at: 24th Sep 2010 21:30
Sven's covered most of this.

I think multi passes can get around the instruction limit. If you try to do too much in one pass, the shader won't work. I haven't used multi passes yet but intend to primarily for this reason.

Another thing is that they can make the shader shorter, which is pretty important since they can take a long time to load (they're compiled at runtime I think!). IIRC, with evolved's multiple lights, there's a different technique for each different number of total lights. If each had a separate single pass technique, their total length would go with n squared - something like n*(n-1)/2, where n is the total number of lights. With a separate pass for each light, the length of the shader code goes as n.
Aralox
19
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 25th Sep 2010 12:29
Thanks Sven B and Dr Tank for the replies, and sorry about my late one.
What you said about the sequential texture passing really helped Sven, its alot more clear now what goes on. This means that we have so much control over our objects and shaders, its really freaking awesome. The code for it is a little more confusing than you mentioned, however.
In the shader, <string ResourceName = "";> has to be called in the texture declaration statement. After a bit of reading (http://forum.thegamecreators.com/?m=forum_view&t=28992&b=1 few posts down) i learned that the <> are used for 'annotations' which are application specific. It makes sense then that i have to use them to signal to dbp where the texture is going. (plus in the sampler, the texture name has to be in <> brackets) What i dont understand is the significance of "string ResourceName = """

Dr Tank, i really dont understand what you mean by
Quote: "If each had a separate single pass technique, their total length would go with n squared - something like n*(n-1)/2, where n is the total number of lights."

Could you elaborate?
And, isnt it faster if a shader had a single pass with alot in it rather than multiple passes with little in them?
And to handle multiple lights, couldnt I set a constant in the shader eg numLights, then have an array declared in the shader, which receives data from my game about the nature of the lights (eg pos), which is then calculated within a for-next loop in the shader? All in one pass?

I have another question, Q4: Why does a vector4 need to be passed (set effect constant vector) to a corresponding float3 in the shader?

and Q5: what does smoothing actually do? (eg set smooth in blender) My guess is that it turns on interpolation of vertex data in the pixel shader: i.e. makes per-pixel lighting stuff like speculars work. I think that because my model's specular was blocky/polygon-lighted when smoothing was off.

Lastly, i basically achieved what I said i wanted to achieve in my first post - i wrote a little program where the same (specular+texture) shader is applied to different limbs of a Map object with different textures. The cool part however, is that when i pass the light position to the shader, it affects all the limbs. Its kinda messy, but run it to see what im talking about. My next aim is to work out some kind of shadow shading, or just light occlusion (i.e. light blocked by parts of the model).

Thanks,
Aralox



http://www.freewebs.com/aralox/
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 25th Sep 2010 13:24 Edited at: 25th Sep 2010 13:25
Unfortunately I haven't found anything in the MSDN help files about a resource name so I won't be able to help you in that area. I've seen examples where the resource name is referenced to a texture, so it might be a way to say to DBP that that texture should be set to a fixed texture.

A4:
One constant register is always a float4, regardless of what you put in it. Eg a float will take up one register, but the 3 floats behind it are ignored. Same for a float3: the first 3 floats are used, and the last one is ignored. So instead of having a command for every single type there is, it's faster and easier to have one command that does the trick for all.

A5:
Could you specify this a little more? I have never used blender, and I'm not sure what this has to do with pixel shaders. If I'm not mistaking, smoothing has more to do with rendering geometry than texturing.

Cheers!
Sven B

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Sep 2010 19:02
When you load a shader in DBPro you have the option of using the textures specified in the shader (if any) or those already applied to the object. That's the purpose of the flag in the usual syntax:



Quote: "And, isnt it faster if a shader had a single pass with alot in it rather than multiple passes with little in them?"


Yes and no. Dr Tank has already told you one reason for not using one pass. If the calculations are exactly the same then a single pass is faster. However, some calculations are in fact faster when split into two passes. An example is a blur shader. It is much faster to split the blurring into two passes - one doing a horizontal blur, the other doing a vertical blur using the output of the first pass as input. Just compare the arithmetic involved in doing a full 5x5 step box filter blur in one pass with the arithmetic involved in two separate 5 step line filter blurs in two passes. The larger the filter the greater the saving.
Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 26th Sep 2010 14:01
Set smooth determines how light in general is handled by each face, it will affect specular lighting, but is not limited to that and will affect diffuse lighting as well. Specular lighting will affect an object regardless of whether it is being smoothed or not, smooth just determines how the spec light and any other light will affect it with regards to face edges.

when smooth is on, lighting is shared among all connected faces set to smooth: it gives the appearance of being rounded/smooth and doesn't show the hard edges of the polygons unless the angle is very sharp.

when smooth is off, lighting is calculated for each face individually and the polygon edges will be visible anytime two faces meet at an angle.

If you want a quick visual, load up blender, make yourself a low poly sphere or cylinder 6 or 8 sides and toggle smooth on and off with just the default light setup.


Aralox
19
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 26th Sep 2010 14:06
(Im frantically reading up and learning what a 'step box filter' is and how blurring/post-processing shaders work, before framing an adequate reply)

@Sven - the smoothing makes models in blender look 'smoother' by lighting them on a per-pixel basis rather than per-vertex (i think), so you dont see hard creases. this link has a few pictures http://www.blender.org/documentation/htmlI/x2681.html. A model in dbp that was 'unsmoothed'/solid in blender looked blocky in dbp, and my per-pixel lighting didnt work with it; i.e. my specular was blocky.



http://www.freewebs.com/aralox/
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Sep 2010 14:20
Quote: "Im frantically reading up and learning what a 'step box filter' "


My unusual use of terminology probably won't help.

Try looking up Gaussian filters, e.g.

http://en.wikipedia.org/wiki/Gaussian_blur

especially the comments under Implementation.
Aralox
19
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 27th Sep 2010 08:35 Edited at: 27th Sep 2010 11:06
Ahh i get it now. Man it just gets more and more interesting. I get how gaussian blurring works, and sort of get why its better to do two passes.
(if any shader-nubs like me need some links for learning, here are a couple: http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/
http://www.gamedev.net/community/forums/topic.asp?topic_id=529461
Im still not sure how multiple passes benefits lighting, but i think ill write a shader with multiple lights and have a look - itll probably clarify things.

Thanks for the answer about the vector4 sven.

And heres another, Q6: For post-processing effects in dbp, do i have to use a textured plane positioned right in front of the camera? Cant i use a shader to send stuff to the backbuffer?

EDIT: Im trying to write a blur shader that does 2 passes on a texture, and so far i have the horizontal pass working nicely, but im really struggling with rendering the first pass to a texture and using it for the second pass. Is it possible to do this all within the shader or do i have to pass the output texture from the horizontal pass into DBP, then run a vertical pass shader on it? (which is what evolved's shader seems to do)



http://www.freewebs.com/aralox/
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Sep 2010 13:29
Quote: "Im still not sure how multiple passes benefits lighting"


I don't think it does - and it will probably slow things down a bit. You may need multiple passes for PS1 and PS2 because of the rather tight instruction count limits, but if you have a PS3 capable card then a single pass is probably sufficient.

Quote: "but i think ill write a shader with multiple lights and have a look - itll probably clarify things"


Probably depends on what you're trying to do - lighting with normal mapping and 8 lights will probably not compile with PS2.

Quote: "For post-processing effects in dbp, do i have to use a textured plane positioned right in front of the camera? Cant i use a shader to send stuff to the backbuffer?"


Not sure about that one. I vaguely recall a recent update (U7.3 perhaps?) introduced the idea of screen effects but I haven't used those commands yet. Something for me to check out.

Quote: "but im really struggling with rendering the first pass to a texture and using it for the second pass. Is it possible to do this all within the shader"


Yes. Have a look at the "blur.dbs" shader that comes with Dark Shader. Once you've got that working for a simple scene see if you can improve it by using the two pass method (i.e. horizontal+vertical blur). Actually that shader also uses multiple passes in a different way - have a look at the Help file under Editing Shaders>Compatibility with Dark Shader for further details.
Aralox
19
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 27th Sep 2010 14:50 Edited at: 27th Sep 2010 14:54
Thanks for the reply GG
I guess Evolved was going for good backwards compatibility with his multiple passes. My intention is to have a light flash at the gun everytime a shot is fired, in my fps game. Its sort of a clone of star wars battlefront, so theres going to be quite a few players on the field at any time (about 32). However i intend to make it that only the player and players close to him will spawn lights on gunshot. Now i think about it, i only need to show about 3 or 4 lights on gunshot, the player probably wont notice. Hmm and i could have a ambient-light-level system where the light would preferentially be shown if the player is in darkness, but i digress.

What you might be thinking of is lee's article here :http://www.thegamecreators.com/pages/newsletters/newsletter_issue_87.html (scroll down abit)
<<thats the article, if you want a quick look.


I havent got DarkShader could you post up the source for that shader (or if theres legal issues, something similar/something related to
rendering the screen to a texture within the shader and re-using in in the second pass)? I would really appreciate it
Aralox

EDIT: put carriage returns in the code-blocked article



http://www.freewebs.com/aralox/
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Sep 2010 17:58
That is the intermediate render target stuff I was talking about - except I didn't understand what Lee was talking about in that particular article. DBPro has already had those features for quite some time so I really don't understand the fanfare for U7.5.

Perhaps it was an opportunity to break something that was previously working.

Quote: "I havent got DarkShader"


Get it.

Quote: "could you post up the source for that shader (or if theres legal issues, something similar/something related to
rendering the screen to a texture within the shader and re-using in in the second pass)? I would really appreciate it"


Gimme da codez!

Here you go - attached.
Aralox
19
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 30th Sep 2010 10:38 Edited at: 30th Sep 2010 10:40
Thank you green gandalf
I think I sort of understand now whats going on with the blur shader/post-processing, after much studying of the code you posted. Ive been trying to optimise it, by only using one camera, and managed to simplify it abit.
Instead of using the second camera to run a shader on and update an image, then using a plane to show the image, i used camera 0 to run the effect and update the image, and pasted the image on the screen. I got a similar frame rate, however. I really dont like how low the fps is (about 70, as opposed to a regular 300 with basic 3d programs). I tried using texture screen instead of paste image, but i couldnt figure out how to get the text to render on top

Heres the different version of the dbp blur program you posted


Thanks, and apologies for the late reply
Aralox


EDIT: Quick question, how do you do colors for the code-blocked words here on the forum?



http://www.freewebs.com/aralox/
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Sep 2010 13:05 Edited at: 30th Sep 2010 13:10
Quote: "I really dont like how low the fps is (about 70, as opposed to a regular 300 with basic 3d programs)."


That's unavoidable with things like bloom and blur since you are usually rendering the scene at least twice and adding lots of extra processing within the main blur/bloom code.

The double render will more or less halve the fps on its own. Bloom can be faked using pre-rendered ghosting images for things like street lights etc but generally works much the same way, i.e. render the scene as usual then post-process that image to add bloom or blur.

The extra processing per render can mount up too. For example, rendering a simple 3D scene without any fancy bump mapping or similar effects will typically use one texture lookup per pixel. A 5x5 blur shader split into a horizontal and vertical pass will use 5 texture lookups per pixel per pass - and the two passes more or less count as two renders as well.

So, all things considered, an fps of 70 isn't bad if you started with 300 without all the fancy stuff. It all comes down to basic arithmetic really - just count the total amount of work that is done per screen render (<total screen pixels> x <instructions per pixel> ), before and after adding the effect, and you can roughly predict the change in fps.

Edit Regarding the code text colours, are you talking about this thread?

Forum syntax highlighting
Aralox
19
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 30th Sep 2010 13:11 Edited at: 30th Sep 2010 13:11
Thats a good point - in this blur shader, doing 3 texture lookups per pass for two passes, is six times more than it would usually. Whats more im fairly sure these are additional texture lookups, as without a screen effect the screen texture would'nt even be shader-processed (im assuming (maybe that quad shader or whatever comes into play here?))
So i guess that 70 is pretty good.
(but i still dont like it )



http://www.freewebs.com/aralox/

Login to post a reply

Server time is: 2026-07-23 14:21:56
Your offset time is: 2026-07-23 14:21:56