specular exponent in pixel shaders

3c6597370b476903ed475f70b4b3ce31
0
john 102 Sep 04, 2004 at 16:27

Is there a way to get around the precision issues caused when doing per-pixel specular lighting in pixel shaders? The problem is, when doing the specular exponent in shaders, it causes banding and most of the time the specular value is ugly. What do you guys suggest?

30 Replies

Please log in or register to post a reply.

354419c232983843ec4434f002922f00
0
Altair 101 Sep 04, 2004 at 18:31

Which shader model are you using?

3c6597370b476903ed475f70b4b3ce31
0
john 102 Sep 06, 2004 at 16:41

I’m using pixel shader 2.0

46407cc1bdfbd2db4f6e8876d74f990a
0
Kenneth_Gorking 101 Sep 07, 2004 at 09:08

Try to use floats, if possible, and check your computations: you might be using half datatypes somewhere, which gives some banding.

354419c232983843ec4434f002922f00
0
Altair 101 Sep 12, 2004 at 15:59

With ps2.0 you can make a look-up to a specular texture, where x=saturated dot prod between light vector and view-reflection about the normal, and y=specular exponent. Alternatively you can use pow instruction.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 12, 2004 at 18:43

both solutions shouldn’t have any precicion issues. the specular texture was possible with ps1.4, too, and made faked solutions with ps1.1 - ps1.3 look really ugly.

there should not be any issues with ps2.0 at all..

354419c232983843ec4434f002922f00
0
Altair 101 Sep 12, 2004 at 20:25

You can use the specular look-up even on ps1.1, but that’s more tricky and you have to use half-vector approach which doesn’t quite give correct results. ps2.0 has no challenge ;)

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 12, 2004 at 20:45

it will not be per pixel correct as the code will not be independent on the vertex-layout of your mesh (you can’t just subdivide and get the same image..).

i’m only talking about solutions that give a real solution, not some approx.. else yes, 1.1 can fix the precision issue like 1.4 can..

354419c232983843ec4434f002922f00
0
Altair 101 Sep 13, 2004 at 02:44

Quite often approximations are real solution in practice :) Half-vector specular lighting is very commonly used technique.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 13, 2004 at 04:35

it’s just a very commonly used technique because on pre-ps1.4, other solutions simply don’t fit well into the shaders at all.. doesn’t make it useful. (per-vertex-half-vector with interpolation, that is.. you can evaluate it per pixel, thats a non-issue and of course and works fine).

354419c232983843ec4434f002922f00
0
Altair 101 Sep 13, 2004 at 21:22

@davepermen

it’s just a very commonly used technique because on pre-ps1.4, other solutions simply don’t fit well into the shaders at all.. doesn’t make it useful. (per-vertex-half-vector with interpolation, that is.. you can evaluate it per pixel, thats a non-issue and of course and works fine). [snapback]11344[/snapback]

Not only because it suits well to <ps1.4, but because it’s also cheaper in latter models as well and quite often suitable solution. Depends on the case what kind of solution you should use (i.e. your poly density).

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 14, 2004 at 06:34

sure. anyways, i’d continue to only suggest general solutions that work no mather in what situation, and, if later needed, optimisations for special situations.. this is one.

354419c232983843ec4434f002922f00
0
Altair 101 Sep 14, 2004 at 12:42

When working with artists, it’s actually better to go other way around. If you start with half-vector approach they can tweak the data from the start to reach required accuracy. Anyway, it of course depends how much you can gain by using half-vector approach (performance/quality/compatibility) if you should see the trouble of supporting both approaches.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 14, 2004 at 12:45

well, i never worked personally with pre-dx9 hw-shaders at all, so i won’t ever care anymore about it.. requiring dx9 isn’t a big issue anymore, as long as the other requirements are quite high-end, too (means, it’s worth the dx9-requirements), and then, those tweaks won’t ever mather.

354419c232983843ec4434f002922f00
0
Altair 101 Sep 14, 2004 at 20:49

It’s not big issue if you are not planning to sell a lot of copies ;) As I said, even in latter shader models you want the shading to perform well.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 15, 2004 at 04:32

you don’t have any performance advantage in not doing it correctly in dx9. thats what i’m saying.

354419c232983843ec4434f002922f00
0
Altair 101 Sep 15, 2004 at 17:17

Oh really? Can you tell me how do you do that without any performance hit?

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 15, 2004 at 17:43

just put the ordinary equation in.. i have not discovered any real performance-hit.. the math is minimal to do it ‘correctly’ (correct phong).

354419c232983843ec4434f002922f00
0
Altair 101 Sep 15, 2004 at 18:17

Heh, well last time I checked (nvshaderperf), on nv40 calculating reflection vector in pixel shader results 2 cycle performance hit, which may be significant depending on what you are doing.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 15, 2004 at 18:25

you can still do halfangle (if you double the angle afterwards again:D). but teh ending math is about the same then. if you do halfangle per pixel, it works great. but for ps1.3 you have to do it per vertex, where it’s inherently wrong. thats why i didn’t suggest it by myself.

reflection is a dot, a mul, a sub.. halfangle requires two normalized vecs to get added, renormalized, then dotted, and then the angle-cosine doubled with some trigonometric identity.. takes longer. at least, on paper.

354419c232983843ec4434f002922f00
0
Altair 101 Sep 15, 2004 at 19:16

Yeah, the whole point of using the half-vector solution is to do it only per vertex, but as said the error you have isn’t that big for reasonably well tesselated geometry. If you are targeting high-end PCs anyway, most of your geometry will be tesselated enough thus per vertex half-vector solution will working without noticable errors and in better performance. I don’t see any point in computing half-vector over reflection vector per pixel though.

Yes, reflection is dot+mad, but then that’s 2 cycles on nv40 since it got only one addition unit (in su2) according to unofficial sources ;) That’s also what nvshaderperf reports, though sometimes if your shader code isn’t tight enough it’ll end up using loose cycles thus hiding the overhead.

A7a9705b61b576581d64e675a3349855
0
shining 101 Sep 16, 2004 at 05:07

@davepermen

i have not discovered any real performance-hit.. the math is minimal to do it ‘correctly’ (correct phong). [snapback]11563[/snapback]

If your performance isn’t pixel shader bound, please feel free to do all the fancy math you want with no performance hit ;7

( I just had to use that creepy smiley face )

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 16, 2004 at 21:52

you know, altair, if i do perpixellighting, you can be _VERY_ sure that its done per pixel. of course i try to extract both constants and linear interpolants. but else, those fakes, i simply don’t like. i prefer to not do specular then (and use a non-bump envmap, or something.. :D)

354419c232983843ec4434f002922f00
0
Altair 101 Sep 22, 2004 at 12:03

Half-vector specular is done per pixel and only the half-vector is computed per vertex. Computer graphics in games is all about faking ;) Best fakes are the ones that have alot of benefits with neglible artifacts.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 22, 2004 at 15:45

it’s not all about faking. i don’t care about faking at all. if it is not mathematically equivalent, then i won’t care.

doing the half-vector per vertex is a very ugly fake imho.

the days of such fakes should finally get over and behind, and we should be able to move on to solve real lighting, shading, shadowing problems.. where the only limit is the accuracy/speed.. but the algorithms are not biased.

once we have such an engine up and running.. even while only for small scenes and quite slow on highest end, we are there.. then, only performance can scale up, and the more performance, the higher the quality settings can be done.

354419c232983843ec4434f002922f00
0
Altair 101 Sep 22, 2004 at 18:53

You don’t care about faking because you don’t work in the game industry and don’t know what it’s alike, but let me tell you that with that kind of idealistic attitude you don’t get very far. What’s wrong with a technique you use if in the end of the day you see no difference? :rolleyes: I guess you haven’t really tried in what’s the difference between the two techniques in practice?

6673a7d3bfd3d1db5e05c5676cc040b6
0
Goz 101 Sep 23, 2004 at 12:26

@davepermen

it’s not all about faking. i don’t care about faking at all. if it is not mathematically equivalent, then i won’t care.

doing the half-vector per vertex is a very ugly fake imho.

the days of such fakes should finally get over and behind, and we should be able to move on to solve real lighting, shading, shadowing problems.. where the only limit is the accuracy/speed.. but the algorithms are not biased.

once we have such an engine up and running.. even while only for small scenes and quite slow on highest end, we are there.. then, only performance can scale up, and the more performance, the higher the quality settings can be done.

[snapback]11848[/snapback]

Erm … so why are you using a half-vector? Why not calculate the true reflection vector per pixel? Its not that slow … 2N(N.L) - L

I don’t see why you are so picky about interpolating from the vertices if you are going to use a “hack” to calculate your angle of specular reflection anyway …

F7a4a748ecf664f189bb704a660b3573
0
anubis 101 Sep 23, 2004 at 14:32

the half nagle vector seems to be a valid hack since neither phong nor blinn-phong shading are actually physically plausible

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Sep 23, 2004 at 21:37

@Altair

You don’t care about faking because you don’t work in the game industry and don’t know what it’s alike, but let me tell you that with that kind of idealistic attitude you don’t get very far. What’s wrong with a technique you use if in the end of the day you see no difference? :rolleyes: I guess you haven’t really tried in what’s the difference between the two techniques in practice? [snapback]11853[/snapback]

hehe..

i have worked with perpixellighting since gf2. i know all the different level of hacks, combinations of it, and all the stuff, and how they look individually, and combined. i had to use the halfangle hack long enough to be happy i don’t need anymore.

this is not about idealistic. this is about teaching the right thing, and then, on a later stage, if we see we get performance or implementation problems, start to look at where we can replace it with an improper fake algorithm, and estimate the error, to try to minimize it.

i’ve done it. most people don’t even realize how hacky halfangle is.. then again, a lot don’t even note if you just use vertex lighting…

354419c232983843ec4434f002922f00
0
Altair 101 Sep 24, 2004 at 02:33

Goz,

Depends what you consider “that slow”. I would consider 10% of performance loss for nothing as significant. But of course it depends what you are doing and how you are doing it if the performance hit is more or less than that.

Dave,

If you give artists the fast implementation from the start with good technical guidance, they’ll work their way around to make specular lighting to look good with provided technique. You can’t make that kind of decision, or rather have optimal assets for it created close to the end of the project. I wish you could, but unfortunately that’s not the way it works in practice. Anyway, I guess the OP you want to teach is long gone ;)

6673a7d3bfd3d1db5e05c5676cc040b6
0
Goz 101 Sep 24, 2004 at 09:12

@Altair

Goz, Depends what you consider “that slow”. I would consider 10% of performance loss for nothing as significant. But of course it depends what you are doing and how you are doing it if the performance hit is more or less than that.

Sorry dude … forgot the “<sarcasm>” tags there ;)