Skip to content

Commit

Permalink
Added compute shader samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
reattiva committed Jun 3, 2016
1 parent 161d6fe commit 7b97029
Show file tree
Hide file tree
Showing 12 changed files with 985 additions and 3 deletions.
11 changes: 11 additions & 0 deletions Source/Work/WorkTests/Data/CS/RenderPaths/Compute.xml
@@ -0,0 +1,11 @@
<renderpath>
<rendertarget name="light" sizedivisor="1 1" format="rgba" />
<rendertarget name="normal" sizedivisor="1 1" format="rgba" />
<rendertarget name="depth" sizedivisor="1 1" format="lineardepth" />
<command type="clear" color="fog" depth="1.0" stencil="0" />
<command type="quad" vs="CopyFramebuffer" ps="CopyFramebuffer" >
<texture unit="diffuse" name="Textures/HeightMap.png" />
</command>
<command type="compute" tag="basic" groups="1024 1 1" cs="BasicCompute" />
<command type="event" tag="event" />
</renderpath>
75 changes: 75 additions & 0 deletions Source/Work/WorkTests/Data/CS/Shaders/HLSL/BasicCompute.hlsl
@@ -0,0 +1,75 @@

// Urho3D adaptation on the Microsoft basic compute example (MIT licensed):
// https://github.com/walbourn/directx-sdk-samples/tree/master/BasicCompute11
// https://code.msdn.microsoft.com/windowsdesktop/DirectCompute-Basic-Win32-7d5a7408

//--------------------------------------------------------------------------------------
// File: BasicCompute.hlsl
//
// This file contains the Compute Shader to perform array A + array B
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------

#ifdef USE_STRUCTURED_BUFFERS

struct BufType
{
int i;
float f;
#ifdef TEST_DOUBLE
double d;
#endif
};

StructuredBuffer<BufType> Buffer0 : register(t0);
StructuredBuffer<BufType> Buffer1 : register(t1);
RWStructuredBuffer<BufType> BufferOut : register(u0);

[numthreads(1, 1, 1)]
void CS( uint3 DTid : SV_DispatchThreadID )
{
BufferOut[DTid.x].i = Buffer0[DTid.x].i + Buffer1[DTid.x].i;
BufferOut[DTid.x].f = Buffer0[DTid.x].f + Buffer1[DTid.x].f;
#ifdef TEST_DOUBLE
BufferOut[DTid.x].d = Buffer0[DTid.x].d + Buffer1[DTid.x].d;
#endif
}

#else // The following code is for raw buffers

ByteAddressBuffer Buffer0 : register(t0);
ByteAddressBuffer Buffer1 : register(t1);
RWByteAddressBuffer BufferOut : register(u0);

[numthreads(1, 1, 1)]
void CS( uint3 DTid : SV_DispatchThreadID )
{
#ifdef TEST_DOUBLE
int i0 = asint( Buffer0.Load( DTid.x*16 ) );
float f0 = asfloat( Buffer0.Load( DTid.x*16+4 ) );
double d0 = asdouble( Buffer0.Load( DTid.x*16+8 ), Buffer0.Load( DTid.x*16+12 ) );
int i1 = asint( Buffer1.Load( DTid.x*16 ) );
float f1 = asfloat( Buffer1.Load( DTid.x*16+4 ) );
double d1 = asdouble( Buffer1.Load( DTid.x*16+8 ), Buffer1.Load( DTid.x*16+12 ) );

BufferOut.Store( DTid.x*16, asuint(i0 + i1) );
BufferOut.Store( DTid.x*16+4, asuint(f0 + f1) );

uint dl, dh;
asuint( d0 + d1, dl, dh );

BufferOut.Store( DTid.x*16+8, dl );
BufferOut.Store( DTid.x*16+12, dh );
#else
int i0 = asint( Buffer0.Load( DTid.x*8 ) );
float f0 = asfloat( Buffer0.Load( DTid.x*8+4 ) );
int i1 = asint( Buffer1.Load( DTid.x*8 ) );
float f1 = asfloat( Buffer1.Load( DTid.x*8+4 ) );

BufferOut.Store( DTid.x*8, asuint(i0 + i1) );
BufferOut.Store( DTid.x*8+4, asuint(f0 + f1) );
#endif // TEST_DOUBLE
}

#endif // USE_STRUCTURED_BUFFERS
21 changes: 21 additions & 0 deletions Source/Work/WorkTests/Data/CS/Shaders/HLSL/MIT.txt
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Microsoft Corp

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

8 changes: 8 additions & 0 deletions Source/Work/WorkTests/Data/CS2/PostProcess/Compute2.xml
@@ -0,0 +1,8 @@
<renderpath>
<command type="event" cs="Circles" output="">
<texture unit="diffuse" name="viewport" />
</command>
<command type="quad" vs="CopyFramebuffer" ps="CopyFramebuffer" output="viewport">
<texture unit="diffuse" name="viewport" />
</command>
</renderpath>
81 changes: 81 additions & 0 deletions Source/Work/WorkTests/Data/CS2/Shaders/HLSL/Circles.hlsl
@@ -0,0 +1,81 @@

//
// Adaptation for Urho3D of "Compute Shader Filters" by Marco Alamia:
// http://www.codinglabs.net/tutorial_compute_shaders_filters.aspx
//

struct Pixel
{
int colour;
};

StructuredBuffer<Pixel> Buffer0 : register(t0);
RWStructuredBuffer<Pixel> BufferOut : register(u0);

cbuffer CustomUB : register(b0)
{
float4 cCircleColor;
}

float4 readPixel(int x, int y)
{
float4 output;
uint index = (x + y * 1024);

output.x = (float)(((Buffer0[index].colour ) & 0x000000ff) ) / 255.0f;
output.y = (float)(((Buffer0[index].colour ) & 0x0000ff00) >> 8 ) / 255.0f;
output.z = (float)(((Buffer0[index].colour ) & 0x00ff0000) >> 16) / 255.0f;
output.w = (float)(((Buffer0[index].colour ) & 0xff000000) >> 24) / 255.0f;

return output;
}

float4 readOutputPixel(int x, int y)
{
float4 output;
uint index = (x + y * 1024);

output.x = (float)(((BufferOut[index].colour ) & 0x000000ff) ) / 255.0f;
output.y = (float)(((BufferOut[index].colour ) & 0x0000ff00) >> 8 ) / 255.0f;
output.z = (float)(((BufferOut[index].colour ) & 0x00ff0000) >> 16) / 255.0f;
output.w = (float)(((BufferOut[index].colour ) & 0xff000000) >> 24) / 255.0f;

return output;
}

void writeToPixel(int x, int y, float4 colour)
{
uint index = (x + y * 1024);

int ired = (int)(clamp(colour.r,0,1) * 255);
int igreen = (int)(clamp(colour.g,0,1) * 255) << 8;
int iblue = (int)(clamp(colour.b,0,1) * 255) << 16;
int ialpha = (int)(clamp(colour.a,0,1) * 255) << 24;

BufferOut[index].colour = ired + igreen + iblue + ialpha;
}

[numthreads(32, 32, 1)]
void CS( uint3 dispatchThreadID : SV_DispatchThreadID )
{
int x = dispatchThreadID.x;
int y = dispatchThreadID.y;

float4 pixel = readPixel(dispatchThreadID.x, dispatchThreadID.y);

//if( readOutputPixel(x,y).a < 0.5 )
writeToPixel(dispatchThreadID.x, dispatchThreadID.y, pixel);

GroupMemoryBarrierWithGroupSync();

if( dot(pixel.rgb,float3(1,1,1)) > 2.79 )
{
for( float alpha = 0; alpha<360; alpha+=1 )
{
int clampedX = clamp(x + cos(alpha)*15, 0, 1024);
int clampedY = clamp(y + sin(alpha)*15, 0, 768);//336);
//writeToPixel( clampedX, clampedY, float4(0.2,0,0.2,1.0) );
writeToPixel( clampedX, clampedY, cCircleColor );
}
}
}

0 comments on commit 7b97029

Please sign in to comment.