Archived

This topic is now archived and is closed to further replies.

Kotumo

Fading in DirectX

Recommended Posts

Kotumo    122
Hi. I''m trying to write a fader in DirectX. This SHOULD be really easy, and my proc works, but oh so slow. I want it to fade all pixels so that they all hit black at the same time. I use a double that is 1.0 in the beginning and 0.0 when the picture is black. I''m decreasing s with a constant, and a bigger constant = faster fade. I suppose this is not the way to go. Far to many muls and shift-left|right''s. I know I shouldn''t lock/unlock to much either, but this way I only lock/unlock once pr. frame. How can I make this go faster?? //---------snipsnip--------- double s = 1.0f; if(DDLockBackSurface()) { screen = (unsigned long *)ddsdMain.lpSurface; lpitch = ddsdMain.lPitch >> 2; for(int j = 0; j < SCREEN_HEIGHT; j++) for(int i = 0; i < SCREEN_WIDTH; i++) { color = screen[j*lpitch+i]; r = (color>>16); g = (color>>8); b = color; r = (unsigned char)r*s; g = (unsigned char)g*s; b = (unsigned char)b*s; color = (r<<16)+(g<<8)+b; screen[j*lpitch+i] = color; } DDUnlockBackSurface(); } //---------snipsnip--------- Sincerely Jarle Holtan

Share this post


Link to post
Share on other sites
NightShade    122
Yeah that''s how I first tried it.. The problem is mostly that moving that much memory - reading it then rewriting a new value, is very slow between video memory and the cpu.

I went to a faster way of doing fades by using a 3d polygon (split in half into two triangles of course) plastered in front of the screen using D3D. By varying the opacity of the current material before you draw the big polygon, you get fading. Of course this requires you to set up Direct3D in your app, and it would require the user to have a 3d card.

I think there''s another way to do it with stencil buffers (back when I was doing this, not all cards supported them), and I never investigated this (as my app was 3d already).

The way you currently are doing it, you can probably speed it up some by, instead of reading and writing one pixel at a time, read a line into an array (one scan line in length allocated on the stack seemed to work pretty well), operate on the line, and write the line back out to memory. Then again, if you blt the whole thing to a system memory surface, and operate on that surface, then blt back, it might be faster. You might want to get rid of those floats too but I think the bulk of the speed hit is in moving the memory around, and really, any way I did it using just 2d was still really slow compared to letting the 3d card handle it.

I''d be interested to see any other ways people do this. For such a general and ubiquitous effect you''d think there would be a ton of info on how to do it.




-ns-

Share this post


Link to post
Share on other sites
The_[PI]_ehT    122
O''course that''s the easier way, but you could also try to use ASM ops and replace the float by an __int8; but of course then you had to think for a new algorithm

greetz
pi~

Share this post


Link to post
Share on other sites
Slinger    122
Instead of trying to fade by modifying the brightness of each pixel, try using DirectDraws Gamma Controls to fade for you.

Check out This link for a tutorial.
http://www.gamedev.net/reference/articles/article998.asp

I hope this helps

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The way I''ve done fading is to use the Gamma or RGB ramp values for the primary surface. This allows you to fade in and out and even preserve the screen data. One thing to remember is to use V-synch during the fade or you get wierd flicky things across your screen. (Yeah I know, really technical) Sorry I don''t have any code as an example but you can look it up, it''s pretty simple.

Share this post


Link to post
Share on other sites
BitBlt    386
Another option you have is to use look-up tables instead of calculating all the values on the fly. This could mean eating up a good bit of memory but it''s a thought.

Also, keep in mind that not all video cards support gamma ramps and stuff. Stinks.



"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka

Share this post


Link to post
Share on other sites
smkc_be    134
Here''s a great alpha blending tutorial if you don''t want to fade the whole screen.

http://www.gamedev.net/reference/articles/article817.asp

Share this post


Link to post
Share on other sites
Succinct    169
heh, dunno about dx, but what i did was:

1) if your gonna go and operate on every pixel, you have no choice but to do it in asm, otherwise even dual-processored t-birds won''t be able to handle it
2) just like night shade said: plaster an alpha blended quad in the front of the view
3) in ogl i just perturbed the alpha component at the beginning of every frame. you can select a color and it will effect all subsequent drawing opertations. don''t know how well this applys dx, though

Thank you for your bandwidth.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~Succinct Demos Online~


"<DiLLiGaS> I''m suprised nobody takes M$ to court for rape... The OS keeps going down on you w/o your permission."

Share this post


Link to post
Share on other sites