Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (ozgur, howardR, AndrewAMD, exile, Ayumi), 725 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
blit_process #421033
04/08/13 20:21
04/08/13 20:21
Joined: Jul 2001
Posts: 6,904
H
HeelX Offline OP
Senior Expert
HeelX  Offline OP
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
The function bmap_process applies a shader to a source-bitmap and writes the result into a target bitmap. This way, images can be processed very fast, because a shader it is executed on the GPU for this. This is not only very fast, it is LIGHTNING FAST, compared to manipulating pixels with pixel_for_bmap and so on. The downside is, though, that the destination image is automatically converted into a render target. This is problematic in a lot of cases, especially when you want to access pixels afterwards. The manual just says

Quote:
"For accessing pixels of the bitmap afterwards, it can be converted back to a pixel format (bmap_to_format) or blitted onto another bitmap (bmap_blit)."

Here is a small snippet of a function, which I call "blit_process", which applies a shader to a bitmap WITHOUT changing it into a render target. It comes with an example shader, that rotates a bitmap about 90 degrees clockwise.

NOTICE: This function is not intended for calling it every frame, but for single image manipulations!

Code:
// Processes the content of <bmap> with a shader without (!) changing it to a rendertarget
// as bmap_process does. Returns <bmap> if everything went right; NULL otherwise.
//
BMAP* blit_process (BMAP* bmap, MATERIAL* shader)
{
    if (bmap == NULL || shader == NULL)
        return NULL;
        
    int w = bmap->width;
    int h = bmap->height;
    
    if (w <= 0 || h <= 0)
        return NULL;
    
    // create temporary bitmap as RT for the shader
    BMAP* to = bmap_createblack(w, h, bmap_format(bmap));
    
    // apply shader to bmap and render into target
    bmap_process(to, bmap, shader);
    
    // update source bitmap with contents of RT
    bmap_blit(bmap, to, NULL, NULL);
    
    ptr_remove(to); // delete RT
    return bmap;
}



demo code:
Code:
MATERIAL* mtlRot90 = {
    effect = "rot90.fx";
}

int main ()
{
    wait(1);
    
    BMAP* test = bmap_create("test.png");
    video_set(test->width, test->height, 0, 0);
    
    while (!key_space)
    {
        draw_quad(test, nullvector, NULL, NULL, NULL, NULL, 100, 0);
        wait(1);
    }
    
    while (key_space) wait(1);
    
    blit_process(test, mtlRot90);
    
    while (!key_space)
    {
        draw_quad(test, nullvector, NULL, NULL, NULL, NULL, 100, 0);
        wait(1);
    }	
}



rot90.fx:
Code:
Texture TargetMap;
sampler2D smpSrc = sampler_state { texture = <TargetMap>; };

float4 PS (float2 tex : TEXCOORD0): COLOR
{
    float alpha = radians(90);
    
    float2x2 R = { cos(alpha), -sin(alpha),
                   sin(alpha),  cos(alpha)};
                   
    float2 uv = mul(tex.xy - 0.5, R) + 0.5;
    
    return float4(tex2D(smpSrc, uv).rgb, 1);
}

technique t
{
    pass p
    {
        PixelShader = compile ps_2_0 PS();
    }
}



demo result:

src: "Lynx or Bobcat" by Karen Arnold (public domain)

I hope this is useful for somebody smile

Last edited by HeelX; 04/08/13 20:58.
Re: blit_process [Re: HeelX] #421035
04/08/13 20:31
04/08/13 20:31
Joined: Oct 2008
Posts: 341
R
ratz Offline
Senior Member
ratz  Offline
Senior Member
R

Joined: Oct 2008
Posts: 341
can you make an picture for non commercial Game Studio user ?

Re: blit_process [Re: ratz] #421037
04/08/13 20:55
04/08/13 20:55
Joined: Jul 2001
Posts: 6,904
H
HeelX Offline OP
Senior Expert
HeelX  Offline OP
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
Originally Posted By: ratz
can you make an picture
Sure, although it is a pretty simple rotation only wink --- added to the first post

Re: blit_process [Re: HeelX] #421039
04/08/13 21:31
04/08/13 21:31
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Thanks for sharing!
I love useful functions smile

Edit: but for a simple 90°-rotation I'd just use:
return float4(tex2D(smpSrc, float2(tex.y, 1 - tex.x)).rgb, 1);
tongue

Edit2: anyway, thanks for the rotation-matrix-calculation-formula grin
with it I finally managed to get a rotation-blur shader working ( I tried many times smirk )

Last edited by Kartoffel; 04/08/13 22:05.

POTATO-MAN saves the day! - Random
Re: blit_process [Re: Kartoffel] #421051
04/09/13 07:08
04/09/13 07:08
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
thanks, I just wanted to start to play with bmap_process, to test it for making AI maps like obstacle map and different potential fields out of a tile map...


Free world editor for 3D Gamestudio: MapBuilder Editor

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1