-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #98 Implete compile compute shader
- Loading branch information
1 parent
a80f0e5
commit b076479
Showing
12 changed files
with
410 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (C) 2020 Pham Hong Duc | ||
// This file is part of the "Skylicht Engine". | ||
// For conditions of distribution and use, see copyright notice in irrlicht.h | ||
// Add irrlicht compute shader feature | ||
|
||
#ifndef __I_GPU_COMPUTE_H_INCLUDED__ | ||
#define __I_GPU_COMPUTE_H_INCLUDED__ | ||
|
||
#include "IrrCompileConfig.h" | ||
|
||
#include "IReferenceCounted.h" | ||
#include "EDriverTypes.h" | ||
|
||
namespace irr | ||
{ | ||
namespace video | ||
{ | ||
class IRWBuffer; | ||
class ITexture; | ||
|
||
class IGPUCompute : public virtual IReferenceCounted | ||
{ | ||
public: | ||
IGPUCompute() : | ||
DriverType(EDT_NULL) | ||
{ | ||
|
||
} | ||
|
||
E_DRIVER_TYPE getDriverType() const { return DriverType; }; | ||
|
||
virtual void setTexture(int slot, ITexture *texture) = 0; | ||
|
||
virtual void setBuffer(int slot, IRWBuffer *buffer) = 0; | ||
|
||
virtual void dispatch(int threadGroupX, int threadGroupY, int threadGroupZ) = 0; | ||
|
||
protected: | ||
|
||
E_DRIVER_TYPE DriverType; | ||
}; | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
// Copyright (C) 2020 Pham Hong Duc | ||
// This file is part of the "Skylicht Engine" | ||
// Upgrade GPU Compute Shader feature | ||
|
||
#include "pch.h" | ||
#include "IrrCompileConfig.h" | ||
#include "CD3D11Driver.h" | ||
#include "CD3D11Texture.h" | ||
#include "CD3D11RWBuffer.h" | ||
#include "CD3D11GPUCompute.h" | ||
|
||
#ifdef _IRR_COMPILE_WITH_DIRECT3D_11_ | ||
|
||
#include "irrOS.h" | ||
|
||
#include "d3dcompiler.h" | ||
|
||
namespace irr | ||
{ | ||
namespace video | ||
{ | ||
CD3D11GPUCompute::CD3D11GPUCompute(CD3D11Driver *driver) : | ||
ComputeShader(NULL), | ||
ShaderBuffer(NULL) | ||
{ | ||
DriverType = EDT_DIRECT3D11; | ||
|
||
Device = driver->getExposedVideoData().D3D11.D3DDev11; | ||
if (Device) | ||
{ | ||
Device->AddRef(); | ||
Device->GetImmediateContext(&Context); | ||
} | ||
|
||
for (int i = 0; i < NUM_PARAMS_SUPPORT; i++) | ||
{ | ||
TextureSlot[i] = NULL; | ||
BufferSlot[i] = NULL; | ||
} | ||
} | ||
|
||
CD3D11GPUCompute::~CD3D11GPUCompute() | ||
{ | ||
Device->Release(); | ||
Context->Release(); | ||
|
||
if (ComputeShader != NULL) | ||
ComputeShader->Release(); | ||
|
||
if (ShaderBuffer != NULL) | ||
ShaderBuffer->Release(); | ||
} | ||
|
||
bool CD3D11GPUCompute::compile(const c8* computeShaderProgram, | ||
const c8* computeShaderEntryPointName, | ||
E_COMPUTE_SHADER_TYPE csCompileTarget) | ||
{ | ||
ID3D10Blob* errorMsgs = 0; | ||
|
||
ID3DInclude* includer = NULL; | ||
|
||
UINT flags = 0; | ||
|
||
#if !defined(WINDOWS_STORE) | ||
if (csCompileTarget >= ECST_CS_5_0) | ||
flags |= D3D10_SHADER_ENABLE_STRICTNESS; | ||
else | ||
{ | ||
flags |= D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY; | ||
csCompileTarget = ECST_CS_4_0; | ||
} | ||
|
||
#ifdef _DEBUG | ||
// These values allow use of PIX and shader debuggers | ||
flags |= D3D10_SHADER_DEBUG; | ||
flags |= D3D10_SHADER_SKIP_OPTIMIZATION; | ||
#else | ||
// These flags allow maximum performance | ||
flags |= D3D10_SHADER_OPTIMIZATION_LEVEL3; | ||
#endif | ||
|
||
flags |= D3D10_SHADER_OPTIMIZATION_LEVEL3; | ||
#endif | ||
|
||
// last macro has to be NULL | ||
core::array<D3D_SHADER_MACRO> macroArray; | ||
|
||
D3D_SHADER_MACRO macro; | ||
macro.Definition = NULL; | ||
macro.Name = NULL; | ||
|
||
macroArray.push_back(macro); | ||
|
||
HRESULT hr = D3DCompile( | ||
computeShaderProgram, | ||
strlen(computeShaderProgram), | ||
"", | ||
¯oArray[0], | ||
includer, | ||
computeShaderEntryPointName, | ||
COMPUTE_SHADER_TYPE_NAMES[csCompileTarget], | ||
flags, 0, | ||
&ShaderBuffer, | ||
&errorMsgs); | ||
|
||
if (FAILED(hr)) | ||
{ | ||
core::stringc errorMsg = "Could not compile shader"; | ||
|
||
if (errorMsgs) | ||
{ | ||
errorMsg += ": "; | ||
errorMsg += static_cast<const char*>(errorMsgs->GetBufferPointer()); | ||
|
||
errorMsgs->Release(); | ||
} | ||
|
||
logFormatError(hr, errorMsg); | ||
|
||
return false; | ||
} | ||
#ifdef _DEBUG | ||
else if (errorMsgs) | ||
{ | ||
core::stringc errorMsg = "Shader compilation warning: "; | ||
errorMsg += static_cast<const char*>(errorMsgs->GetBufferPointer()); | ||
|
||
errorMsgs->Release(); | ||
errorMsgs = NULL; | ||
|
||
os::Printer::log(errorMsg.c_str(), ELL_WARNING); | ||
} | ||
#endif | ||
|
||
if (errorMsgs) | ||
errorMsgs->Release(); | ||
|
||
if (!ShaderBuffer) | ||
return false; | ||
|
||
hr = Device->CreateComputeShader( | ||
ShaderBuffer->GetBufferPointer(), | ||
ShaderBuffer->GetBufferSize(), | ||
NULL, | ||
&ComputeShader); | ||
|
||
if (FAILED(hr)) | ||
{ | ||
core::stringc errorMsg = "Could not create computeshader"; | ||
logFormatError(hr, errorMsg); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void CD3D11GPUCompute::setTexture(int slot, ITexture *texture) | ||
{ | ||
TextureSlot[slot] = texture; | ||
} | ||
|
||
void CD3D11GPUCompute::setBuffer(int slot, IRWBuffer *buffer) | ||
{ | ||
BufferSlot[slot] = buffer; | ||
} | ||
|
||
void CD3D11GPUCompute::dispatch(int threadGroupX, int threadGroupY, int threadGroupZ) | ||
{ | ||
Context->CSSetShader(ComputeShader, NULL, 0); | ||
|
||
for (int i = 0; i < NUM_PARAMS_SUPPORT; i++) | ||
{ | ||
// Texture resource view | ||
ID3D11ShaderResourceView* views = NULL; | ||
if (TextureSlot[i]) | ||
views = ((CD3D11Texture*)TextureSlot[i])->getShaderResourceView(); | ||
Context->CSSetShaderResources(i, 1, &views); | ||
|
||
// Buffer unorderred access view | ||
ID3D11UnorderedAccessView* unorderedAccessView; | ||
if (BufferSlot[i]) | ||
unorderedAccessView = ((CD3D11RWBuffer*)BufferSlot[i])->getUnorderedAccessView(); | ||
Context->CSSetUnorderedAccessViews(i, 1, &unorderedAccessView, NULL); | ||
} | ||
|
||
// do gpu compute | ||
Context->Dispatch(threadGroupX, threadGroupY, threadGroupZ); | ||
} | ||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.