diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2823d7a --- /dev/null +++ b/.gitignore @@ -0,0 +1,96 @@ +# compiled shader files +*.sh +*.cso +*.asm + +# Compiled Object files +*.slo +*.lo +*.o + +# Compiled Dynamic libraries +*.so +*.dylib + +# Compiled Static libraries +*.lai +*.la +*.a + + +#OS junk files +[Tt]humbs.db +*.DS_Store + +#Visual Studio files +*.[Oo]bj +*.user +*.aps +*.pch +*.vspscc +*.vssscc +*_i.c +*_p.c +*.ncb +*.suo +*.tlb +*.tlh +*.bak +*.[Cc]ache +*.ilk +*.log +*.lib +*.sbr +*.sdf +*.opensdf +*.unsuccessfulbuild +ipch/ +[Oo]bj/ +[Bb]in +[Dd]ebug*/ +[Rr]elease*/ +Ankh.NoLoad + +#MonoDevelop +*.pidb +*.userprefs + +#Tooling +_ReSharper*/ +*.resharper +[Tt]est[Rr]esult* +*.sass-cache + +#Project files +[Bb]uild/ + +#Subversion files +.svn + +# Office Temp Files +~$* + +# vim Temp Files +*~ + +#NuGet +packages/ +*.nupkg + +#ncrunch +*ncrunch* +*crunch*.local.xml + +# visual studio database projects +*.dbmdl + +#Test files +*.testsettings + +#blender backups +*.blend1 +*.blend2 + +#obj files in resources +/resources/models/objects/*.mtl +!/resources/models/objects/*.obj diff --git a/DXShader.cpp b/DXShader.cpp new file mode 100644 index 0000000..13eaf15 --- /dev/null +++ b/DXShader.cpp @@ -0,0 +1,16 @@ +#include "DXShader.h" + + +DXShader::DXShader(ID3D11Device* device, const BYTE* bytecode) +{ + device->CreateComputeShader(bytecode, sizeof(bytecode), NULL, &this->shader); +} + + +DXShader::~DXShader() +{ +} + +ID3D11ComputeShader* DXShader::getShader() { + return this->shader; +} diff --git a/DXShader.h b/DXShader.h new file mode 100644 index 0000000..68ef08d --- /dev/null +++ b/DXShader.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +class DXShader +{ +private: + ID3D11ComputeShader* shader; +public: + DXShader(ID3D11Device*, const BYTE*); + ~DXShader(); + + ID3D11ComputeShader* getShader(); +}; + diff --git a/DXStructuredBuffer.cpp b/DXStructuredBuffer.cpp new file mode 100644 index 0000000..45ae485 --- /dev/null +++ b/DXStructuredBuffer.cpp @@ -0,0 +1,27 @@ +#include "DXStructuredBuffer.h" + + +DXStructuredBuffer::DXStructuredBuffer(ID3D11Device* d, unsigned int s, unsigned int n) +{ + D3D11_BUFFER_DESC sbDesc; + D3D11_UNORDERED_ACCESS_VIEW_DESC sbUAVDesc; + + sbDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE; + sbDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; + sbDesc.StructureByteStride = s; + sbDesc.ByteWidth = sbDesc.StructureByteStride * n; + sbDesc.Usage = D3D11_USAGE_DEFAULT; + d->CreateBuffer(&sbDesc, NULL, &this->buffer); + + // UAV + sbUAVDesc.Buffer.NumElements = sbDesc.ByteWidth / sbDesc.StructureByteStride; + sbUAVDesc.Format = DXGI_FORMAT_UNKNOWN; //DXGI_FORMAT_R8G8B8A8_UNORM + sbUAVDesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; + + d->CreateUnorderedAccessView((ID3D11Resource *) this->buffer, &sbUAVDesc, &this->uav); +} + + +DXStructuredBuffer::~DXStructuredBuffer() +{ +} diff --git a/DXStructuredBuffer.h b/DXStructuredBuffer.h new file mode 100644 index 0000000..d574fb5 --- /dev/null +++ b/DXStructuredBuffer.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +class DXStructuredBuffer +{ +private: + ID3D11Buffer* buffer; + ID3D11UnorderedAccessView* uav; +public: + DXStructuredBuffer(ID3D11Device*, unsigned int, unsigned int); + ~DXStructuredBuffer(); +}; + diff --git a/DXTexture.cpp b/DXTexture.cpp new file mode 100644 index 0000000..a0735b5 --- /dev/null +++ b/DXTexture.cpp @@ -0,0 +1,15 @@ +#include "DXTexture.h" + + +DXTexture::DXTexture(IDXGISwapChain* sc, ID3D11Device* dev, int id) +{ + sc->GetBuffer(id, __uuidof(ID3D11Texture2D), (void**) &this->texture); + dev->CreateUnorderedAccessView((ID3D11Resource*) this->texture, NULL, &this->uav); + +} + + +DXTexture::~DXTexture() +{ + this->texture->Release(); +} diff --git a/DXTexture.h b/DXTexture.h new file mode 100644 index 0000000..5903aa6 --- /dev/null +++ b/DXTexture.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +class DXTexture +{ +private: + ID3D11Texture2D* texture; + ID3D11UnorderedAccessView* uav; +public: + DXTexture(IDXGISwapChain*, ID3D11Device*, int); + ~DXTexture(); +}; + diff --git a/DXWrapper.cpp b/DXWrapper.cpp new file mode 100644 index 0000000..b09ff57 --- /dev/null +++ b/DXWrapper.cpp @@ -0,0 +1,52 @@ +#include "DXWrapper.h" + +DXWrapper::DXWrapper(HWND window, int width, int height) +{ + const DXGI_SWAP_CHAIN_DESC sd = { { width, height, { 60, 1 }, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED, DXGI_MODE_SCALING_UNSPECIFIED }, { 1, 0 }, DXGI_USAGE_RENDER_TARGET_OUTPUT, 1, NULL, TRUE, DXGI_SWAP_EFFECT_SEQUENTIAL, 0 }; + + DXGI_SWAP_CHAIN_DESC temp; + temp = sd; + temp.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_UNORDERED_ACCESS | DXGI_USAGE_SHADER_INPUT; + temp.OutputWindow = window; + + + D3D11CreateDeviceAndSwapChain( + NULL, + D3D_DRIVER_TYPE_HARDWARE, + NULL, + D3D11_CREATE_DEVICE_DEBUG, + NULL, + 0, + D3D11_SDK_VERSION, + &temp, + &(this->swapChain), + &(this->device), + NULL, + &(this->context) + ); + +} + + +DXWrapper::~DXWrapper() +{ + this->context->ClearState(); + this->device->Release(); + this->swapChain->Release(); +} + +void DXWrapper::present() { + this->swapChain->Present(0, 0); +} + +DXTexture DXWrapper::getTexture(int id) { + return DXTexture(this->swapChain, this->device, id); +} + +DXStructuredBuffer DXWrapper::getStructuredBuffer(unsigned int stride, unsigned int num) { + return DXStructuredBuffer(this->device, stride, num); +} + +DXShader DXWrapper::getComputeShader(const BYTE* bytecode) { + return DXShader(device, bytecode); +} diff --git a/DXWrapper.h b/DXWrapper.h new file mode 100644 index 0000000..5b02bd5 --- /dev/null +++ b/DXWrapper.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include +#include + +#include "DXTexture.h" +#include "DXStructuredBuffer.h" +#include "DXShader.h" + +class DXWrapper +{ +private: + ID3D11Device* device; + IDXGISwapChain* swapChain; + ID3D11DeviceContext* context; + +public: + DXWrapper(HWND, int, int); + ~DXWrapper(); + + void present(); + DXTexture getTexture(int id); + DXStructuredBuffer getStructuredBuffer(unsigned int, unsigned int); + DXShader getComputeShader(const BYTE*); +}; + diff --git a/Main.cpp b/Main.cpp new file mode 100644 index 0000000..e5896ba --- /dev/null +++ b/Main.cpp @@ -0,0 +1,59 @@ +//////////////////////////////////////// +// +// Radix Sort in DirectCompute +// by Jacob Maskiewicz +// +// CSE 190: GPU Programming +// +//////////////////////////////////////// + +#include + +#include "DXWrapper.h" + +#include "RadixSort.sh" + +// define the size of the window +#define THREADSX 16 // number of threads in the thread group used in the compute shader +#define THREADSY 16 // number of threads in the thread group used in the compute shader +#define WINDOWWIDTH 1280 +#define WINDOWHEIGHT 720 + +#define WINWIDTH ((((WINDOWWIDTH + THREADSX - 1) / THREADSX) * THREADSX)) // multiply of ThreadsX +#define WINHEIGHT ((((WINDOWHEIGHT + THREADSY - 1) / THREADSY) * THREADSY)) // multiply of ThreadsY + +#define WINPOSX 50 +#define WINPOSY 50 + +int WINAPI WinMain( + HINSTANCE hInstance, // HANDLE TO AN INSTANCE. This is the "handle" to YOUR PROGRAM ITSELF. + HINSTANCE hPrevInstance,// USELESS on modern windows (totally ignore hPrevInstance) + LPSTR szCmdLine, // Command line arguments. similar to argv in standard C programs + int iCmdShow) // Start window maximized, minimized, etc. +{ + HWND windowHandle = CreateWindow(L"edit", 0, WS_POPUP | WS_VISIBLE, WINPOSX, WINPOSY, WINWIDTH, WINHEIGHT, 0, 0, 0, 0); + + ShowCursor(false); + + DXWrapper dx(windowHandle, WINWIDTH, WINHEIGHT); + + DXTexture& backBuffer = dx.getTexture(0); + DXStructuredBuffer& sortBuffer = dx.getStructuredBuffer(sizeof(float), WINWIDTH * WINHEIGHT); + + DXShader& shader = dx.getComputeShader(g_main); + + bool running = true; + while (running) { + MSG msg; + PeekMessage(&msg, windowHandle, 0, 0, PM_REMOVE); + + //TODO - set shader + //TODO - set views and buffers + //TODO - dispatch shader + //TODO - reset shader and resource view + + dx.present(); + } + + // release buffers and shaders +} \ No newline at end of file diff --git a/RadixSort.hlsl b/RadixSort.hlsl new file mode 100644 index 0000000..bb977c3 --- /dev/null +++ b/RadixSort.hlsl @@ -0,0 +1,4 @@ +[numthreads(1, 1, 1)] +void main( uint3 DTid : SV_DispatchThreadID ) +{ +} \ No newline at end of file diff --git a/radix_sort.sln b/radix_sort.sln new file mode 100644 index 0000000..bc4e31f --- /dev/null +++ b/radix_sort.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "radix_sort", "radix_sort.vcxproj", "{F3C5DCE0-E45D-45C9-8D13-38BC935C312E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F3C5DCE0-E45D-45C9-8D13-38BC935C312E}.Debug|Win32.ActiveCfg = Debug|Win32 + {F3C5DCE0-E45D-45C9-8D13-38BC935C312E}.Debug|Win32.Build.0 = Debug|Win32 + {F3C5DCE0-E45D-45C9-8D13-38BC935C312E}.Release|Win32.ActiveCfg = Release|Win32 + {F3C5DCE0-E45D-45C9-8D13-38BC935C312E}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/radix_sort.vcxproj b/radix_sort.vcxproj new file mode 100644 index 0000000..53c4490 --- /dev/null +++ b/radix_sort.vcxproj @@ -0,0 +1,103 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {F3C5DCE0-E45D-45C9-8D13-38BC935C312E} + Win32Proj + radix_sort + + + + Application + true + v120 + Unicode + + + Application + false + v120 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + Compute + 5.0 + %(Filename).cso + %(Filename).sh + %(Filename).asm + + + + + + \ No newline at end of file diff --git a/radix_sort.vcxproj.filters b/radix_sort.vcxproj.filters new file mode 100644 index 0000000..c362d4b --- /dev/null +++ b/radix_sort.vcxproj.filters @@ -0,0 +1,53 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Source Files + + + \ No newline at end of file