pp pixelation with proper blurring

Posted By: 3run

pp pixelation with proper blurring - 12/09/17 11:43

Hey guys!

I was wondering if any one can help me out with pixelation shader. I could do this by setting camera->bmap to a small sized panel, but I need to get proper pixelation with blurring (to keep lines, edges etc correct after downsampling, if that's how it's called).

Greets.
Posted By: Superku

Re: pp pixelation with proper blurring - 12/09/17 17:06

Do you have an example of what you want to achieve (and optionally an example of how it's NOT supposed to look like)?
Posted By: 3run

Re: pp pixelation with proper blurring - 12/09/17 17:32

Hey!

I had this kind of a shader made by MasterQ32, but it was also lost with other stuff I had on my laptop.
Results looked like this (please ignore the lines):

Notice how edges and lines are pixelated correctly. The hole effect used to blur the screen, but I added sharpen shader above it all and it was pretty good.

Greets!
Posted By: txesmi

Re: pp pixelation with proper blurring - 12/12/17 11:32

Hi,
In order to get a proper antialias (that is how it is called), I guess your best choice is to perform two linearly interpolated 1:2 downsamples.



Click to reveal..

Code:
#include <acknex.h>
#include <default.c>

#define PRAGMA_PATH "%EXE_DIR%templatesimages"

void main ()
{
	video_mode = 8;
	wait(1);
	level_load ( "" );
	ENTITY *entSky = ent_createlayer("skycube+6.tga", SKY | CUBE, 1);
	ENTITY *ent = ent_create(SPHERE_MDL, nullvector, NULL);
	camera->x = -30;
	camera->bmap = bmap_createblack(screen_size.x, screen_size.y, 24);
	
	VIEW *camDown[2];
	VIEW *camPrev = camera;
	int i=0;
	for (; i < 2; i += 1) {
		camDown[i] = view_create(1);
		var _fraction = pow(2, i + 1);
		camDown[i]->size_x = screen_size.x / _fraction;
		camDown[i]->size_y = screen_size.y / _fraction;
		camDown[i]->bmap = bmap_createblack(camDown[i]->size_x, camDown[i]->size_y, 24);
		MATERIAL *_mtl = mtl_create();
		_mtl->skill1 = floatv(camDown[i]->size_x);
		_mtl->skill2 = floatv(camDown[i]->size_y);
		_mtl->skin1 = camPrev->bmap;
		effect_load(_mtl, "downsample.fx");
		camDown[i]->material = _mtl;
		camDown[i]->flags |= SHOW | PROCESS_TARGET;
		camPrev->stage = camDown[i];
		camPrev = camDown[i];
	}
	VIEW *camLast = view_create(1);
	camLast->material = mtl_create();
	effect_load(camLast->material, "upsample.fx");
	camLast->flags |= SHOW | PROCESS_TARGET;
	camPrev->stage = camLast;
	
	while (!key_esc) {
		if (key_1)
			DEBUG_BMAP(camera->bmap, 1, 1);
		if (key_2)
			DEBUG_BMAP(camDown[0]->bmap, 1, 1);
		if (key_3)
			DEBUG_BMAP(camDown[1]->bmap, 1, 1);
		wait(1);
	}
	
	sys_exit(NULL);
}



downsample.fx
Code:
float4 vecSkill1;

texture mtlSkin1;
sampler ColorSampler = sampler_state { Texture = <mtlSkin1>; MipFilter = Linear; MinFilter = Linear; MagFilter = Linear; AddressU = Clamp; AddressV = Clamp;  };

float4 PS(in float2 inPos: VPOS) : COLOR0 {
	float2 coor = (inPos.xy + 0.5f) / vecSkill1.xy;
	return tex2D(ColorSampler, coor.xy);
}

technique downsample{
	pass p0 {
		VertexShader = null;
		PixelShader  = compile ps_3_0 PS();
	}
}



upsample.fx
Code:
float4 vecViewPort;

texture TargetMap;
sampler ColorSampler = sampler_state { Texture = <TargetMap>; MipFilter = Point; MinFilter = Point; MagFilter = Point; AddressU = Clamp; AddressV = Clamp; };

float4 PS(in float2 inPos: VPOS) : COLOR0 {
	float2 coor = (inPos.xy + 0.5f) / vecViewPort.xy;
	return tex2D(ColorSampler, coor.xy);
}

technique upsample {
	pass p0 {
		VertexShader = null;
		PixelShader  = compile ps_3_0 PS();
	}
}




Three downsamples from a double sized camera gives you a 8 steps gradient instead of a 4 steps gradient.



Salud!
Posted By: 3run

Re: pp pixelation with proper blurring - 12/12/17 15:08

I love it! Thank you very much, man! You are a shader master! laugh

Edit: one question, how do I update it then I change resolution? My guess is that I need to resize the camDown[i]->bmap, but am I correct? If so, then I need to think of a way to changing the size of it's bmap, rather than recreating it over and over again (I guess).

Edit2: also, how do I remove it from the chain if I need to? Removing everything created (material, views and their bmaps) results to some very strange but interesting results grin I tried to make it work with pp_add and pp_remove, but no luck, since I'm too noob related to shader things frown

Some screens to show you how it looks like (I love it!)



My best regards!
Posted By: jumpman

Re: pp pixelation with proper blurring - 12/12/17 17:43

awesome, would love to see a video!
Posted By: 3run

Re: pp pixelation with proper blurring - 12/12/17 19:44

Thank you man! Hopefully soon I'll post a video or a small demo for testing.
Posted By: Kartoffel

Re: pp pixelation with proper blurring - 12/12/17 20:31

Great to see the progress. Not a huge fan of the downsampled look, though.

regarding txesmi's suggestion: Personally, I'd only use one downsampling pass with a 2x2 blur kernel utilizing linear texture filtering. It would basically give you the same result using less texture samples and only a single pass. On the other hand, I don't think it'll blow the budget to leave things as they are. grin
Posted By: 3run

Re: pp pixelation with proper blurring - 12/17/17 22:03

Originally Posted By: 3run
Edit: one question, how do I update it then I change resolution? My guess is that I need to resize the camDown[i]->bmap, but am I correct? If so, then I need to think of a way to changing the size of it's bmap, rather than recreating it over and over again (I guess).

Edit2: also, how do I remove it from the chain if I need to? Removing everything created (material, views and their bmaps) results to some very strange but interesting results grin I tried to make it work with pp_add and pp_remove, but no luck, since I'm too noob related to shader things frown


txesmi@ man, I still hope that you will find some spare time to help me out with this grin


Greets!
Posted By: Kartoffel

Re: pp pixelation with proper blurring - 12/18/17 14:48

Removing: Basically you have to remove the postprocess views (including resetting the .stage pointers) and remove all bmap-buffers that were used.

When resizing, for each view that changes size: Remove the bmap, create a new one with the appropriate dimensions and update all your pointers. This has to be done within one frame, otherwise you have to prevent the post-process stages from rendering (basically everything that uses your bmap would break stuff as the pointer is no longer valid).
Posted By: 3run

Re: pp pixelation with proper blurring - 12/18/17 16:00

Kartoffel@ thank you! laugh I'll give it a try!

Best regards!
Posted By: Kartoffel

Re: pp pixelation with proper blurring - 12/18/17 16:14

Oh I forgot to add: There sometimes were problems that removing a bmap didn't work properly after it was used as a rendertarget. After a few times of recreating the rendertaret directx throws an error. I'm not sure under which conditions this happens, though. (maybe when not removing everything properly, so the rendertarget is still referenced somewhere from the graphics api and not removed as a result. just a wild guess.)
Posted By: txesmi

Re: pp pixelation with proper blurring - 01/23/18 13:30

Originally Posted By: 3run
txesmi@ man, I still hope that you will find some spare time to help me out with this grin


I am sorry, I wanted to do some tests before answering but forgot it in the way. Better late than never blush

The main idea was to set the largest render target from the beginning and modify the views size only, instead of rebuilding everything each resolution change. As spected, there is no need of deleting anything. The rendering chain, main cameras render target inclusive, survives to a resolution change, and also to a blank level load as long as I tested it. Very few changes were needed to acoplish the goal. I added two functions that build and remove the whole rendering chain too. I guess that the negative side-effect comes from the fact that the whole render targets are cleared before rendering, a bunch of not visible pixels are also cleared on windowed video modes. Nothing to worry about, I think. Maxime when you leave render targets in peace forever.

pixelation01.zip

Salud!
Posted By: 3run

Re: pp pixelation with proper blurring - 01/23/18 20:08

Hey man!

It works pretty great! Thank you very much for your time! laugh The only thing I can notice so far is that 'axe' effect sometimes is blinking or chancing it's length, is that related to pixelation, what do you think?

Edit: nahh, it seems that it's just related to framerate laugh

Best regards!
© 2024 lite-C Forums