I want to comiple a compute shader from text, but it failed with 0x8e16d728 error.
// #pragma kernel CSMain //line 1,//uncomment this line will generate warning "unknown pragma kernel"
RWTexture2D<float4> Result : register (u0);
Texture2D<float4> ImageInput : register (t0);
cbuffer ImageSize : register(b0) {
int width;
int height;
}
[numthreads(16, 16, 1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
uint2 size = uint2(width, height);
// uint2 size = uint2(ImageSizes[0], ImageSizes[1]);
uint2 flip = uint2(id.x, size.y - id.y);
float4 flipped = ImageInput[flip];
uint2 xy = id.xy;
Result[xy] = flipped;
}
Here is the compile function :
static HRESULT compileCS(ID3D11Device* pDevice, int csByteLength, const char* csByteString, ComPtr<ID3D11ComputeShader>& shader) {
HRESULT hr = E_FAIL;
if (shader != nullptr)
{
return S_OK;
}
UINT flags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined(DEBUG) || defined(_DEBUG)
flags |= D3DCOMPILE_DEBUG;
#endif
// Prefer higher CS shader profile when possible as CS 5.0 provides better performance on 11-class hardware.
LPCSTR profile = (pDevice->GetFeatureLevel() >= D3D_FEATURE_LEVEL_11_0) ? "cs_5_0" : "cs_4_0";
const D3D_SHADER_MACRO defines[] =
{
"EXAMPLE_DEFINE", "1",
NULL, NULL };
ComPtr<ID3DBlob> shaderBlob = nullptr;
ComPtr<ID3DBlob> errorBlob = nullptr;
hr == D3DCompile(csByteString,
csByteLength,
nullptr,
defines,
D3D_COMPILE_STANDARD_FILE_INCLUDE,
"CSMain",
profile,
flags,
0,
&shaderBlob,
&errorBlob);
if (FAILED(hr))
{
if (errorBlob)
{
const char* error = (const char*)errorBlob->GetBufferPointer();
auto bufferSize = errorBlob->GetBufferSize();
Log(error);
}
else
{
LogFormat("compile shader failed hr = %#08x", hr);
}
return hr;
}
Log("compile cs success");
//编译成功
hr = pDevice->CreateComputeShader(shaderBlob->GetBufferPointer(), shaderBlob->GetBufferSize(), nullptr, shader.ReleaseAndGetAddressOf());
if (FAILED(hr))
{
Log("Create shader from blob failed!");
return hr;
}
return hr;
}
When I uncomment line 1 in the hlsl,the error changes to another strange error code 0x8e16d8a8.
Note that errorBlob only shows warning when line 1 is uncommented, and shows nothing when commented, just the error code from hr.
I also tried compile from fxc, it can compile and generate bytecode (although when line 1 is uncommented while compiling, it generates the same "unknown pragma kernel" warning), but another error occured when calling CreateComputeShader..
Now I'm lost, can you help me?