Ogre Version: :14.2:
Operating System: :Windows:
Render System: :DX11:
I'm trying to get run a compute HLSL shader. I want to pass a 2D texture and some parameters to the shader and obtain a structure with few floats as the result. I want to run this shader once when my application starts. This will prepare some data what I need to read back. Unfortunately, I did not find such example.
The samples folder contains one compute sample which utilizes the Compositor manager. However this is designed for compute shaders during render operation. What is not my case. I wonder how should I approach this. Here is my testing code what I did not try to run yet. Not sure how to bind the vertex buffer to my shader.
Code: Select all
auto spComputeProg = Ogre::HighLevelGpuProgramManager::getSingleton().createProgram(
"InstGen", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "hlsl", Ogre::GpuProgramType::GPT_COMPUTE_PROGRAM);
spComputeProg->setSourceFile("instgen.hlsl");
spComputeProg->setParameter("entry_point", "main");
spComputeProg->setParameter("target", "cs_5_0");
spComputeProg->load();
// Create and initialize the vertex buffer
size_t nCount = 32;
auto spOutBuffer = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(
sizeof(float), nCount, Ogre::HBU_GPU_TO_CPU, true); // shadow since we will read the data from GPU
{
Ogre::HardwareBufferLockGuard lock(spOutBuffer, Ogre::HardwareBuffer::HBL_WRITE_ONLY);
auto pData = static_cast<float*>(lock.pData);
for (size_t ndx = 0; ndx < nCount; ++ndx)
pData[ndx] = (float)ndx;
}
auto spParams = spComputeProg->getDefaultParameters();
spParams->setNamedConstant("outBuff", 0);
auto pRS = Ogre::Root::getSingleton().getRenderSystem();
pRS->bindGpuProgram(spComputeProg->_getBindingDelegate());
// !!! How to bind my vertex buffer to the GPU program? !!!
pRS->_dispatchCompute(Ogre::Vector3i((nCount - 1) / 8 + 1, (nCount - 1) / 8 + 1, 1));
// Verify the result
{
Ogre::HardwareBufferLockGuard lock(spOutBuffer, Ogre::HardwareBuffer::HBL_READ_ONLY);
auto pData = static_cast<float*>(lock.pData);
for (size_t ndx = 0; ndx < nCount; ++ndx) {
float fVal = (float)ndx;
bool bOK = pData[ndx] != fVal * fVal;
}
}
// Cleanup
Ogre::GpuProgramManager::getSingleton().remove(spComputeProg);
Code: Select all
RWStructuredBuffer<float> outBuff: register(u0);
[numthreads(8, 1, 1)]
void main(uint3 id : SV_DispatchThreadID)
{
uint index = id.x;
outBuff[index] = 2.0 * outBuff[index];"
};
Thanks for any help
Abyss