flat assembler
Message board for the users of flat assembler.

Index > Examples and Tutorials > DX11 Compute Shader, Directly to Backbuffer. Win64

Author
Thread Post new topic Reply to topic
bitRAKE



Joined: 21 Jul 2003
Posts: 2800
Location: dank orb
bitRAKE
Uses d3dcompiler_43.dll, which is probably on your machine. (d3dcompiler_46.dll will work as well if you have the Win8 SDK.)

Purpose is to show minimum needed to use GPU and see result on screen.

One thing that is confusing to me: the shader defines the output texture composed of float4, but the backbuffer is of type RGBA. I assume the conversion happens somewhere, but I could not find it documented. Found two other errors in the documentation along the way.

From here usually we would want to pass some incremental data each iteration. This requires creating a constant buffer, and binding it to the compute shader. Next, probably might add more UAVs, or other types of buffers. To return data to the CPU.

Compute shaders can be quite flexible. For example, http://pouet.net/prod.php?which=59091 and http://pouet.net/prod.php?which=59466 and http://pouet.net/prod.php?which=57302 is almost entirely a cs5 shader. Jan Vlietinck has made available 3D fluid simulation and other examples: http://users.skynet.be/fquake/


Hope it is useful to someone.
No video on this one. Very Happy

Thanks baldr for the list macros.


Description: Compute Shader 101.
Download
Filename: CS00.zip
Filesize: 16.72 KB
Downloaded: 206 Time(s)


_________________
¯\(°_o)/¯ unlicense.org
Post 20 May 2013, 14:50
View user's profile Send private message Visit poster's website Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6029
Location: Poland
MHajduk
bitRAKE wrote:
Uses d3dcompiler_43.dll, which is probably on your machine. (d3dcompiler_46.dll will work as well if you have the Win8 SDK.)
I have Windows 7 Home Premium and this DLL is absent in my system. Why this particular "exotic" library is so necessary? Could it be replaced with other DLLs that are present in "regular" Windows 7?
Post 20 May 2013, 17:55
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 20:24; edited 1 time in total
Post 20 May 2013, 17:58
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6029
Location: Poland
MHajduk
HaHaAnonymous wrote:
Quote:
I have Windows 7 Home premium and this DLL is absent in my system.

Try updating your DirectX.

Quote:
Why this particular "exotic" library is so necessary?

The question is: Why don't just include this stupid "dll" with the program? I think it is possible in a way or another.
The library could be placed along with other files in the project folder but I suppose it may violate (?) a copyright. Another way would be to include entire library or some extracted functions as a binary resource inside the program but it would be a bit tricky IMO and again may violate someone's copyright.
Post 20 May 2013, 18:05
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16903
Location: In your JS exploiting you and your system
revolution
My Win7 Pro here also doesn't have any of the DLL files.

The tyranny of the default means that using this requires the user to upgrade/update/download an external file.
Post 20 May 2013, 18:38
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 20:24; edited 1 time in total
Post 20 May 2013, 18:53
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2800
Location: dank orb
bitRAKE
https://docs.google.com/file/d/0B0Vow7Lxd7nuZ1R4a1VydHh0c2s/edit?usp=sharing

Above is the link to D3DCompiler_46.dll, from the Win8 SDK. It does not have any dependencies which would require any other part of the SDK, or DX update for that matter. The compiled bytecode might require an updated DX, though.

Source will obviously need a slight change to use this latest version.
Code:
    dll [d3dcompiler_46 D3DCompile]    
MS Does not support compiling at runtime except for development. They suggest and urge distributing compiled bytecode. Although, d3dx11_43.dll also has function to compile shaders.

_________________
¯\(°_o)/¯ unlicense.org
Post 20 May 2013, 20:08
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2800
Location: dank orb
bitRAKE
Here we have small data updates being sent to the GPU each iteration of the main loop. For a very lazy timer we just send RDTSC EAX. Enough to produce a strobing effect and cause seizures. Very Happy

Next up is more internal state space - a texture buffer on the GPU to persist between updates.


Description: Compute Shader 102
Download
Filename: CS02.zip
Filesize: 5.56 KB
Downloaded: 196 Time(s)


_________________
¯\(°_o)/¯ unlicense.org
Post 21 May 2013, 02:40
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2800
Location: dank orb
bitRAKE
Once we have a a basic Raymarching framework:
(Largely, Rrrola's main loop - I think it has errors.)
Code:
[numthreads(32, 32, 1)]
void cs0_Main( uint3 gId  : SV_GroupID,
               uint3 dtId : SV_DispatchThreadID,
               uint3 gtId : SV_GroupThreadID,
               uint  gI   : SV_GroupIndex ) {

// First convert 2D space into into 3D ray direction:

  // screen coords 16:10 ratio, square pixels, centered
  float sx = -1.60 + 3.2 * (float)dtId.x / (float)XRES;
  float sy = -1.00 + 2.0 * (float)dtId.y / (float)YRES;

  // bend rays (fish eye)
  float r2 = sx*sx*0.32f + sy*sy;
  float tt = (7.0f-sqrt(37.5f-11.5f*r2))/(r2+1.0f);
  float dx = sx*tt;
  float dy = sy*tt;

  float3 v = float3( dx * 0.955336f + 0.29552f, dy, 0.955336f - dx * 0.29552f );

// choose camera position
//  float3 p = float3( 0.195, 0.5, 0.0 );
  float3 p = pos;


// Intersect the view ray using raymarching.
#define MAX_DIST 4.0
#define MIN_DIST 0.0001

  float totalD = 0.0, D = 3.4e38, extraD = 0.0, lastD;
  int steps;
  int max_steps = 128;
  for (steps=0; steps<max_steps; steps++) {
    lastD = D;
    D = DE(p + totalD * normalize(v));

    // Overstepping: have we jumped too far? Cancel last step.
    if (extraD > 0.0 && D < extraD) {
      totalD -= extraD;
      extraD = 0.0;
      D = 3.4e38;
      steps--;
      continue;
    }
    if (D < MIN_DIST || D > MAX_DIST) break;
    totalD += D;

    // Overstepping is based on the optimal length of the last step.
    totalD += extraD = 0.096 * D*(D+extraD)/lastD;
  }
  p += totalD * normalize(v);

// diagnostic coloring of surface

  float3 color = float3(0,0,0);
  float dC = float(steps)/float(max_steps);

  // We've got a hit or we're not sure.
  if (D < MAX_DIST) {
    color = lerp(float3(0,1,0),float3(1,0,1),dC);

    // We've gone through all steps, but we haven't hit anything.
    if (D > MIN_DIST) {
      color = float3(1.0,1.0,1.0);
    }
  }

  output[dtId.xy] = float4(color,1);
}    
...it can be used with any distance estimation function (DE() needed above).
http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm

For example:
Code:
float cyl(float3 p, float r, float c) {
    return max(length(p.xz)-r, abs(p.y)-c);
}

float DE(float3 p) { return cly(p,1.0,1.3); }    
...and we have a cylinder standing on end.

It's very fun to combine and tweak distance estimation function to get different effect / shape.

_________________
¯\(°_o)/¯ unlicense.org
Post 23 May 2013, 16:54
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2800
Location: dank orb
bitRAKE
I'm still learning the DX11 changes - haven't programmed any DX since DX8, and a lot has changed. One of the mistakes made in my code above, is that once objects are bound to the context they remain bound. So, the main loop can be reduced to just: update of constant buffer(Map/Unmap), Dispatch, Present.

The DX11 documentation is a mess - saying something isn't possible, and then describing how to do it in other places, lol. It's still leaps and bounds better than DX8 though. Hard to believe how cheap and power efficient this 560Ti card is.
Post 26 May 2013, 09:09
View user's profile Send private message Visit poster's website Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Just had a look at this to see how it's done - a lot different from DX6/7 when I played with that a few times!

I tried running it using Win7/ATI but it starts with a coloured box and then, according to my debugger, deadlocked - I see 4 worker threads all stuck in syscalls with the main thread at "$11512 call qword ptr [rax+10h]".
Post 26 May 2013, 14:24
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2800
Location: dank orb
bitRAKE
It's erroring out, and then can't Release due to the pointers being invalid. I've attached the current iteration. There isn't much in the way of error checking. In fact, I don't even verify DX11 swapchain is being returned.

cmp byte [nFL+1],$0B ; would do it.

...I'd be glad to help diagnose this further, but would need more info. Would be nice to have it working on ATI, too. It only appears deadlocked because it doesn't process messages - that is by design and not a real problem. All the threads are created by the driver. If you don't see the pretty colored circles in this one then that would be a problem. Assuming you have DX11 hardware, etc... Oh, might want to change the resolution [in the source code] to match what you are using.


Description: Connett Circles
Download
Filename: cs1.zip
Filesize: 6.4 KB
Downloaded: 242 Time(s)


_________________
¯\(°_o)/¯ unlicense.org
Post 26 May 2013, 15:04
View user's profile Send private message Visit poster's website Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777
DirectX is nothing but Hell. Whatever you learn now will be deprecated soon.

Z77 does not assume there is a GPU so the graphics are truly portable, universal and helpful to OS creators and embedded systems programmers. For example, CodeVu draws everything - every single pixel, image, font, control, etc - directly to memory using only the CPU. Same with my ARM graphics.

Windows has always had a monopoly on video cards. Manufacturers conceal information and make it impossible for individuals like us to ever come up.
Post 29 May 2013, 14:43
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2800
Location: dank orb
bitRAKE
I completely agree. I enjoy learning so much - it will be fun the next time.

_________________
¯\(°_o)/¯ unlicense.org
Post 29 May 2013, 23:44
View user's profile Send private message Visit poster's website Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 721
tthsqe
bitrake,
are you going to make a fasm implementation of that shader code at http://boxplorer2.googlecode.com/svn/trunk/fragment.glsl of which you quoted a small portion for me?
P.S: Thanks - I did try Rrrola's shading, but found that I didn't like as much as what I settled on finally for my renderer.
Post 30 May 2013, 03:03
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2800
Location: dank orb
bitRAKE
I didn't have any plans to, but I have used pieces of it above in this thread. The verbosity of his shader has been most helpful at understanding.

_________________
¯\(°_o)/¯ unlicense.org
Post 30 May 2013, 04:08
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.