The MASM Forum

General => The Laboratory => Topic started by: Siekmanski on February 07, 2018, 09:23:15 AM

Title: Fun with Pixel Shaders and maths
Post by: Siekmanski on February 07, 2018, 09:23:15 AM
These are my first steps into the world of Pixel Shaders.
Suggestions and/or remarks are always welcome.

My goal was to use pixel shaders in Direct3D9.
Unfortunately d3d9.dll ( which is standard present on all windows operating systems from 2003 till now ) doesn't have a shader compiler function.
The fully functional DirectX9 shader compiler function for shader version 3.0 is available in the wrapper libs D3DX9_32.dll up to D3DX9_43.dll.

So I programmed a small ShaderCompiler program that searches if one of the 12 D3DX9_xx.dlls is available on the developers computer and calls the compiler function by using "LoadLibrary" and "GetProcAddress".
The compiled pixel shader code can be saved as pre-compiled GPU code.

The pre-compiled GPU code can be included and executed from your program without the presence of one of the 12 D3DX9_xx.dlls on the end-users computer.
This way we don't bother the end-user with missing D3DX9_xx.dll messages.

As a pixel shader developer your computer must have at least one of the 12 D3DX9_xx.dlls present to compile the pixel shader code.

You can download the latest DirectX9 End-User Runtimes here:
DirectX End-User Runtimes (June 2010)
https://www.microsoft.com/en-us/download/details.aspx?id=8109

Shadertoy, https://www.shadertoy.com/ is a great source to learn pixel shader progamming.
At this moment there are more than 20.000 examples.
You have to convert the WebGL or GLSL to DirectX9 HLSL or GPU assembly.

Here are some great Youtube Video tutorials:
Shadertoy for absolute beginners https://www.youtube.com/watch?v=u5HAYVHsasc&t=829s

The following 3 examples are also in the source code attachment (converted to HLSL),

Simple3D -> https://www.youtube.com/watch?v=dKA5ZVALOhs
Camera3D -> https://www.youtube.com/watch?v=PBxuVlp7nuM
Tutorial palm tree -> https://www.youtube.com/watch?v=0ifChJ0nJfM

A great site about ray-marching:
http://jamie-wong.com/2016/07/15/ray-marching-signed-distance-functions/

The attachment contains 2 sources,
The ShaderCompiler source and the ShaderWindow source to execute the pre-compiled pixelshader.

There are 22 examples, most of them I have converted to HLSL from shadertoy examples.
Title: Re: Fun with Pixel Shaders and maths
Post by: caballero on February 07, 2018, 08:01:38 PM
Siekmanski, thank you for the stuff and resources. Great work. I'll have a close look when I have time :t

Amazing demos!
Title: Re: Fun with Pixel Shaders and maths
Post by: daydreamer on February 08, 2018, 12:32:16 AM
Great work Marinus  :t
is kinda CPU coded perlin noise procedural easiest to port to pixelshaders?
Title: Re: Fun with Pixel Shaders and maths
Post by: AW on February 08, 2018, 01:48:42 AM
Nice demos  :t
The usual stupid question: Why don't you use the 8 years old dx11 instead of the 16 years old dx9? You could reduced the number of shader compilers to 2, I believe.
Title: Re: Fun with Pixel Shaders and maths
Post by: Siekmanski on February 08, 2018, 03:08:22 AM
@aw27, I like to keep things as small and simple as possible.
Because dx9 also is available on XP and dx11 doesn't, dx9 don't need additional ddl's installed the way I use it.

@daydreamer2, yes it can be done.
Here's an example of procedural Perlin Noise,
Title: Re: Fun with Pixel Shaders and maths
Post by: caballero on February 08, 2018, 03:27:09 AM
Siekmanski, you are gaining a place next to the programmers' iron throne
(http://www.laverdad.es/noticias/201503/11/media/cortadas/GF0LO401--490x578.jpg)
Title: Re: Fun with Pixel Shaders and maths
Post by: jj2007 on February 08, 2018, 06:33:01 AM
Siekmanski, you are gaining a place next to the programmers' iron throne
Right :t
Title: Re: Fun with Pixel Shaders and maths
Post by: caballero on February 11, 2018, 01:43:41 AM
Hello. This is my first attemp to pixel shaders programming. Here is a stupid example did in a hurry using the Siekmanski's pixel shaders compiler in D3D. Source code included.

I'd like to make any other more complex example. But there are many unknown things for me still as how to make random numbers and how to get colors from other rows/columns than the actual.

Regards.
Title: Re: Fun with Pixel Shaders and maths
Post by: Siekmanski on February 11, 2018, 03:34:59 AM
It is handy and logical to treat every window resolution as if they have all the same width and height.
By normalizing the screen viewport coordinates ( fragCoord.xy / iResolution.xy ) the width and height are both from 0.0 to 1.0. So coordinate 0.5,0.5 is exactly in the middle of the screen regardless the window resolution.
Pixel shaders are different in the way we normally paint a pixel on the screen just by telling to paint it at coordinate 100,230.
Now Pixel shaders:
ps_main is called for every pixel in the window, starting at left-top position (0.0,0.0) tracing all scanlines till right-bottom (1.0,1.0). You decide what color you paint at the current position given by the ps_main routine.
And of course you want to do that by using fancy math formulas.  :t
I wondered that myself and tested this by writing the Basic HLSL examples, look at the Basic3.hlsl example.

Made a sort of fake random generator,
Title: Re: Fun with Pixel Shaders and maths
Post by: caballero on February 11, 2018, 04:02:57 AM
Nice!

The "main" routine always returns a color for the fragCoord.xy to paint it in some canvas. What I want also is getting the color I have already painted in some coordinate here. I have thought on making a matrix for that, but maybe there is any other simpler way.
Title: Re: Fun with Pixel Shaders and maths
Post by: Siekmanski on February 11, 2018, 05:33:42 AM
Don't know, I'm just like you a beginner in pixel shading.
At the beginning I have studied the YouTube tutorials ( links in my first post ) to get an idea how pixel shaders behave.
To create something we have to do some backwards thinking I guess. In fact this is a logical, fast and short way to solve complex stuff.
I already use this approach for a long time, when I code new routines.
I'll try to explain what I mean by this. ( if you could understand Dutch it would be easier for me  :biggrin: )

A stupid example but I hope you get my drift:

A = the bottom of a tree.
B = one of the many leaves on that tree.

Q: How to find the shortest way between A and B?

From A to B, you get lost when choosing the wrong branches to reach the particular leaf at B and have to go back and start over again and over again until you get to the right leaf at B.

From B to A, you never get lost on a wrong branch because you have only one branch to choose from and end up in a straight way to A.

In pixel shader code this is kind of the same strategy because you know at the start where to paint the pixel, so work your way back to decide what color you want there.
Title: Re: Fun with Pixel Shaders and maths
Post by: Raistlin on February 11, 2018, 06:00:09 AM
Sounds like you are like me, Aspergers... Passion
dictates pseudo idealism and then solution follows.....If not,
must be as all else (comments) says, PURE Genius.  :greenclp:
PPS: I do not mean one is exclusive of the other, that would be like
telling myself I am brilliant and stupid at the same time. Come to think of it....
possibly the correct diagnosis.   
Title: Re: Fun with Pixel Shaders and maths
Post by: Siekmanski on February 11, 2018, 06:41:02 AM
My wife and my friends often call me an idiot.  :biggrin:
Title: Re: Fun with Pixel Shaders and maths
Post by: caballero on February 11, 2018, 09:12:24 AM
Well, it is easy, just use a matrix as double buffer. Just wonder if there is a standard way to get the color, not just to set it. Never mind... I'll try to do it when I have some time.

> if you could understand Dutch...
My God, I think I'm too old for that :lol:. Some years ago I shared a flat with some Erasmus Germans, they just taught me to say some swear words :greensml:. Fortunately, they spoke Spanish very well. One day they forced us to read a text in German to an American and me while they were videotaping us. I do not know how someone can read so many consonants together  :shock:.
Title: Re: Fun with Pixel Shaders and maths
Post by: Siekmanski on February 11, 2018, 09:55:17 AM
Well, it is easy, just use a matrix as double buffer. Just wonder if there is a standard way to get the color, not just to set it. Never mind... I'll try to do it when I have some time.

It is possible to create a texture in floating point format and use that as a lookup table and access it from the pixel shader.
This is very fast because you can place in video memory.
I'll implement textures in the next week when time permits. I think it's cool to do texture deformations with the pixel shader.  8)

Quote
> if you could understand Dutch...
My God, I think I'm too old for that :lol:. Some years ago I shared a flat with some Erasmus Germans, they just taught me to say some swear words :greensml:. Fortunately, they spoke Spanish very well. One day they forced us to read a text in German to an American and me while they were videotaping us. I do not know how someone can read so many consonants together  :shock:.

 :lol:
Then I would let you read these long Dutch words,
aansprakelijkheidswaardevaststellingsveranderingen ( 50 chars translation: liability value changes )
This one is cool,
Hippopotomonstrosesquippedaliofobie ( 35 chars translation: phobia for long words )

Longest Dutch palindrome,
koortsmeetsysteemstrook. ( 23 chars translation: fever measurement system strip )
Title: Re: Fun with Pixel Shaders and maths
Post by: HSE on February 11, 2018, 11:58:50 AM
Amazing... the long words!

Ah! Also the shader engine  :biggrin: :biggrin: :biggrin:
Title: Re: Fun with Pixel Shaders and maths
Post by: caballero on February 12, 2018, 03:59:25 AM
Those Polish I know tell me to say the next tongue twister:
"chrząszcz brzmi w trzcinie" that means "the beetle sounds in the reed"

but I always say :lol::
"ksiądz brzmi w trzcinie" that means "the priest sounds in the reed"

For me, the sound is the same. So, my counterattack is fierce :bgrin:. Recite the following tongue twister:
Quote
El arzobispo de Constantinopla
se quiere desarzobispoconstitanoponitalizar
el buen desarzobispoconstitanoponitalizador
que lo desarzobispoconstitanoponitalice
buen desarzobispoconstitanoponitalizador
será.

A word with 33 characters, not 55, but it’s not bad. You won’t see it in the RAE, but it is universal.
Title: Re: Fun with Pixel Shaders and maths
Post by: daydreamer on February 12, 2018, 10:56:31 PM
Sounds like you are like me, Aspergers... Passion
dictates pseudo idealism and then solution follows.....If not,
must be as all else (comments) says, PURE Genius.  :greenclp:
PPS: I do not mean one is exclusive of the other, that would be like
telling myself I am brilliant and stupid at the same time. Come to think of it....
possibly the correct diagnosis.
I recommend you all read scott adams, the Dilbert principle
its cartoons mixed with background story for those cartoons and anymous people who have mailed him about crazy things on their job, thats even more crazy than his cartoons
at one Point he discuss everyone is an idiot
and I Think its good to dont take yourself too serious

I know C++ programmers also needs to learn "Polish notation" :P
Title: Re: Fun with Pixel Shaders and maths
Post by: Grincheux on February 15, 2018, 04:15:42 AM
Siekmanski, your work is wonderful.
I would stay many hours looking at the examples, beautifull...
Thanks :eusa_boohoo:
Title: Re: Fun with Pixel Shaders and maths
Post by: caballero on February 17, 2018, 06:36:55 AM
Hello, my first pixel shader plasma demo, full code for it. I've done a little experiment to compare the pixel shader with a simple gdi.
Title: Re: Fun with Pixel Shaders and maths
Post by: Siekmanski on February 17, 2018, 07:38:41 AM
Cool effect.  8)
We love pixel shaders   :biggrin:
Title: Re: Fun with Pixel Shaders and maths
Post by: caballero on February 18, 2018, 07:03:25 AM
> We love pixel shaders
Yes  :biggrin:

A sea scape made by Alexander Alekseev aka TDM here (https://www.shadertoy.com/view/Ms2SD1), where you can find its source code too. I only translated to Siekmanski's PSC. I have to improve the sun reflections, the original code is right.
Title: Re: Fun with Pixel Shaders and maths
Post by: Siekmanski on February 18, 2018, 08:27:20 AM
I also converted this shader some days ago.  :biggrin:
I can imagin you want to do the GLSL to HLSL conversion yourself but, if you want the HLSL code for this shader then let me know.
Title: Re: Fun with Pixel Shaders and maths
Post by: felipe on February 18, 2018, 02:25:58 PM
Nice!  :icon14:
Title: Re: Fun with Pixel Shaders and maths
Post by: caballero on February 18, 2018, 07:10:52 PM
> I can imagin you want to do the GLSL to HLSL conversion yourself
Yes, I'd like to try it by myself

> if you want the HLSL code for this shader then let me know
Ok, thank you, give me a bit of time first :biggrin:
Title: Re: Fun with Pixel Shaders and maths
Post by: caballero on February 18, 2018, 08:04:13 PM
I did it :eusa_dance: It was very easy, only needed a look :t
Title: Re: Fun with Pixel Shaders and maths
Post by: Siekmanski on February 18, 2018, 09:26:01 PM
I did it :eusa_dance: It was very easy, only needed a look :t

I knew it. Cool  8)
Title: Re: Fun with Pixel Shaders and maths
Post by: felipe on April 06, 2018, 01:04:26 PM
Nice!  :icon14:
:biggrin:
I have looked to this post a little more and looks just an amazing work siekmanski. Thank you very much to share it. I'm starting with directx 9 (mainly that version because i'm basing my learning in an old book of game development with that library). Is it true that d3d9.dll is standard, is in my machine too. I also realized that d3d8.lib is in the masm32 lib folder. Well, returning to my point, i guess that in this book there are no coverage to pixel shaders. I have downloaded the directx 9 sdk and i will check your .inc files with the ones of this sdk that i will need. If i need later (because i will start with the basics from this old book) something related with pixel shaders i will probably thank you again.

Btw, i have quoted myself with that smile comment, because with such stupid comment i didn't give the proper credit to this post. Probably still not now, but is a little better i think.
Title: Re: Fun with Pixel Shaders and maths
Post by: Siekmanski on April 06, 2018, 01:46:54 PM
Hi Felipe,

Thank you.
If the pixel shader examples run on your machine, d3d9.dll is on your machine too.
I don't use the DirectX9 wrapper lib anymore and wrote my own libs to keep the executables very small.

This could also be useful for you to try out the examples in the Directx 9 book:
The complete Direct3D9 9 SDK includes which I translated somewhere in 2004 to Masm assembly. http://members.home.nl/siekmanski/d3dx9_asm.zip
Title: Re: Fun with Pixel Shaders and maths
Post by: felipe on April 07, 2018, 10:53:27 AM
 :greenclp: Bravo siekmanski, amazing! Thank you very much.
Title: Re: Fun with Pixel Shaders and maths
Post by: felipe on April 08, 2018, 10:14:54 AM
Siekmanski from the example d3d9_alpha.asm i get this error:

Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: ...\d3dx9_asm\sources\d3d9xAlpha\d3d9_al
pha.asm

***********
ASCII build
***********

\masm32\d3dx9includes\limits.inc(33) : error A2005: symbol redefinition : MB_LEN
_MAX
_
Assembly Error
...

I have added the folders of the includes and libs in the masm32 folder (d3dx9includes and d3dx9libs). Thanks for any help, it will be great to use the includes for directx9.
Title: Re: Fun with Pixel Shaders and maths
Post by: felipe on April 08, 2018, 10:22:36 AM
I don't use the DirectX9 wrapper lib anymore and wrote my own libs to keep the executables very small.

Can you explain this a little bit please? If you want to do it here or in the game development forum is ok too.  :idea:
Title: Re: Fun with Pixel Shaders and maths
Post by: Siekmanski on April 08, 2018, 02:30:39 PM
Quote
I have added the folders of the includes and libs in the masm32 folder (d3dx9includes and d3dx9libs). Thanks for any help, it will be great to use the includes for directx9.
The reason I included all includes and libs is because they are not exactly the same as in the Masm32 SDK from 2004.
So, it's better to keep them apart from the original ones from the Masm32 SDK.
The sources are 14 years old and based on the DirectX 9 SDK from late 2003.

You can download the latest DirectX9 End-User Runtimes,
DirectX End-User Runtimes (June 2010)
https://www.microsoft.com/en-us/download/details.aspx?id=8109

I don't use the DirectX9 wrapper lib anymore and wrote my own libs to keep the executables very small.

Can you explain this a little bit please? If you want to do it here or in the game development forum is ok too.  :idea:

Because I was fed up with the fact that the user needed the exact same DirectX 9 wrapper-lib version (d3dx9.lib, more than 20 different libs......) installed to run.
To prefent this I wrote my own math lib and picture/texture loader lib for Direct3D9.
My latest sources only need d3d9.lib (d3d9.dll) to work.
Those libs use SIMD instructions so, you might want to update your Masm Assembler to a more resent one.

Title: Re: Fun with Pixel Shaders and maths
Post by: felipe on April 08, 2018, 02:48:12 PM
Siekmanski: first point: ok thanks one last question: any tips to create include files for libraries like those of the directx9 sdk? What was your main procedure to do such thing that time? To the second point: amazing!  :icon14:
Thanks a lot one more time.  :icon14:
Title: Re: Fun with Pixel Shaders and maths
Post by: Siekmanski on April 08, 2018, 03:03:11 PM
I translated it line by line myself, took a really long time.  :biggrin:
Title: Re: Fun with Pixel Shaders and maths
Post by: felipe on April 08, 2018, 03:27:50 PM
Code: [Select]
/*==========================================================================;
 *
 *  Copyright (C) Microsoft Corporation.  All Rights Reserved.
 *
 *  File:   d3d9.h
 *  Content:    Direct3D include file
 *
 ****************************************************************************/

#ifndef _D3D9_H_
#define _D3D9_H_

#ifndef DIRECT3D_VERSION
#define DIRECT3D_VERSION         0x0900
#endif  //DIRECT3D_VERSION

// include this file content only if compiling for DX9 interfaces
#if(DIRECT3D_VERSION >= 0x0900)


/* This identifier is passed to Direct3DCreate9 in order to ensure that an
 * application was built against the correct header files. This number is
 * incremented whenever a header (or other) change would require applications
 * to be rebuilt. If the version doesn't match, Direct3DCreate9 will fail.
 * (The number itself has no meaning.)*/

#ifdef D3D_DEBUG_INFO
#define D3D_SDK_VERSION   (32 | 0x80000000)
#define D3D9b_SDK_VERSION (31 | 0x80000000)

#else
#define D3D_SDK_VERSION   32
#define D3D9b_SDK_VERSION 31
#endif


#include <stdlib.h>

#define COM_NO_WINDOWS_H
#include <objbase.h>

#include <windows.h>

I see. I'm willing to do that, the problem is than i don't know how. Above is a little bit of the .h file of the d3d9. From the directx 9 sdk (which i believe is now freeware or something). I guess i will look to every include file in the masm package and compare with this .h files to undestand the syntax (actually i have never write a macro, maybe 1 very simple like an exercise or study and for 16 bits). Finally, if i really can't do this i guess i will give up with directx. I have searched in the fasm include from the main page and are very small and incompletes comparing with the ones of the masm package. So i really don't think there are other assemblers with includes for directx. Would be a good idea (just to do directx programming) to work with c to have this headers files working but using inline assembly all the time? Really don't want that, but can't think on more options by now. Ok good night and thanks again.  :icon14:
Title: Re: Fun with Pixel Shaders and maths
Post by: caballero on April 10, 2018, 04:38:13 AM
Why don't move this topic to the games thread that maybe should be named "graphics" and "games"? :t