Skip to content

Commit 10b313e

Browse files
committed
OpenGL copyImageToBuffer changes (added logger and correct layer memory calc)
+ Added Screenshoting to example06
1 parent bb9bcbc commit 10b313e

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

examples_tests/06.MeshLoaders/main.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,20 @@ class MeshLoadersApp : public ApplicationBase
313313
}
314314
void onAppTerminated_impl() override
315315
{
316+
const auto& fboCreationParams = fbo[acquiredNextFBO]->getCreationParameters();
317+
auto gpuSourceImageView = fboCreationParams.attachments[0];
318+
319+
bool status = ext::ScreenShot::createScreenShot(
320+
logicalDevice.get(),
321+
queues[CommonAPI::InitOutput::EQT_TRANSFER_DOWN],
322+
renderFinished[resourceIx].get(),
323+
gpuSourceImageView.get(),
324+
assetManager.get(),
325+
"ScreenShot.png",
326+
asset::EIL_PRESENT_SRC,
327+
static_cast<asset::E_ACCESS_FLAGS>(0u));
328+
329+
assert(status);
316330
logicalDevice->waitIdle();
317331
}
318332
void workLoopBody() override

src/nbl/video/COpenGLCommandBuffer.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ COpenGLCommandBuffer::~COpenGLCommandBuffer()
120120
return IGPUCommandBuffer::reset(_flags);
121121
}
122122

123-
void COpenGLCommandBuffer::copyBufferToImage(const SCmd<impl::ECT_COPY_BUFFER_TO_IMAGE>& c, IOpenGL_FunctionTable* gl, SOpenGLContextLocalCache* ctxlocal, uint32_t ctxid)
123+
void COpenGLCommandBuffer::copyBufferToImage(const SCmd<impl::ECT_COPY_BUFFER_TO_IMAGE>& c, IOpenGL_FunctionTable* gl, SOpenGLContextLocalCache* ctxlocal, uint32_t ctxid, const system::logger_opt_ptr logger)
124124
{
125125
IGPUImage* dstImage = c.dstImage.get();
126126
const IGPUBuffer* srcBuffer = c.srcBuffer.get();
@@ -219,7 +219,7 @@ COpenGLCommandBuffer::~COpenGLCommandBuffer()
219219
}
220220
}
221221

222-
void COpenGLCommandBuffer::copyImageToBuffer(const SCmd<impl::ECT_COPY_IMAGE_TO_BUFFER>& c, IOpenGL_FunctionTable* gl, SOpenGLContextLocalCache* ctxlocal, uint32_t ctxid)
222+
void COpenGLCommandBuffer::copyImageToBuffer(const SCmd<impl::ECT_COPY_IMAGE_TO_BUFFER>& c, IOpenGL_FunctionTable* gl, SOpenGLContextLocalCache* ctxlocal, uint32_t ctxid, const system::logger_opt_ptr logger)
223223
{
224224
const auto* srcImage = c.srcImage.get();
225225
auto* dstBuffer = c.dstBuffer.get();
@@ -234,10 +234,11 @@ COpenGLCommandBuffer::~COpenGLCommandBuffer()
234234
GLuint src = static_cast<const COpenGLImage*>(srcImage)->getOpenGLName();
235235
GLenum glfmt, gltype;
236236
getOpenGLFormatAndParametersFromColorFormat(gl, format, glfmt, gltype);
237-
237+
238+
const auto texelBlockInfo = asset::TexelBlockInfo(format);
238239
const auto bpp = asset::getBytesPerPixel(format);
239-
const auto blockDims = asset::getBlockDimensions(format);
240-
const auto blockByteSize = asset::getTexelOrBlockBytesize(format);
240+
const auto blockDims = texelBlockInfo.getDimension();
241+
const auto blockByteSize = texelBlockInfo.getBlockByteSize();
241242

242243
const bool usingGetTexSubImage = (gl->features->Version >= 450 || gl->features->FeatureAvailable[gl->features->EOpenGLFeatures::NBL_ARB_get_texture_sub_image]);
243244

@@ -250,9 +251,15 @@ COpenGLCommandBuffer::~COpenGLCommandBuffer()
250251
{
251252
if(it->bufferOffset != core::alignUp(it->bufferOffset, blockByteSize))
252253
{
253-
assert(false && "bufferOffset should be aligned to block/texel byte size.");
254+
logger.log("bufferOffset should be aligned to block/texel byte size.", system::ILogger::ELL_ERROR);
255+
assert(false);
254256
continue;
255257
}
258+
259+
const auto imageExtent = core::vector3du32_SIMD(it->imageExtent.width, it->imageExtent.height, it->imageExtent.depth);
260+
const auto imageExtentInBlocks = texelBlockInfo.convertTexelsToBlocks(imageExtent);
261+
const auto imageExtentBlockStridesInBytes = texelBlockInfo.convert3DBlockStridesTo1DByteStrides(imageExtentInBlocks);
262+
const uint32_t eachLayerNeededMemory = imageExtentBlockStridesInBytes[3]; // = blockByteSize * imageExtentInBlocks.x * imageExtentInBlocks.y * imageExtentInBlocks.z
256263

257264
uint32_t pitch = ((it->bufferRowLength ? it->bufferRowLength : it->imageExtent.width) * bpp).getIntegerApprox();
258265
int32_t alignment = 0x1 << core::min(core::max(core::findLSB(it->bufferOffset), core::findLSB(pitch)), 3u);
@@ -291,14 +298,13 @@ COpenGLCommandBuffer::~COpenGLCommandBuffer()
291298
{
292299
ctxlocal->flushStateGraphics(gl, SOpenGLContextLocalCache::GSB_PIXEL_PACK_UNPACK, ctxid);
293300

294-
// TODO this isnt totally valid right?
295-
const size_t bytesPerLayer = pitch * yRange * (compressed ? nbl::asset::getFormatChannelCount(format) : nbl::asset::getTexelOrBlockBytesize(format)); // all block compressed formats are decoded into 1 byte per channel format
301+
const size_t bytesPerLayer = eachLayerNeededMemory;
296302
for (uint32_t z = 0u; z < zRange; ++z)
297303
{
298304
GLuint fbo = ctxlocal->getSingleColorAttachmentFBO(gl, srcImage, it->imageSubresource.mipLevel, it->imageSubresource.baseArrayLayer + z);
299305
if (!fbo)
300306
{
301-
// TODO log something
307+
logger.log("Couldn't retrieve attachment to download image to.", system::ILogger::ELL_ERROR);
302308
continue;
303309
}
304310

@@ -692,14 +698,14 @@ COpenGLCommandBuffer::~COpenGLCommandBuffer()
692698
{
693699
auto& c = cmd.get<impl::ECT_COPY_BUFFER_TO_IMAGE>();
694700

695-
copyBufferToImage(c, gl, ctxlocal, ctxid);
701+
copyBufferToImage(c, gl, ctxlocal, ctxid, m_logger.getOptRawPtr());
696702
}
697703
break;
698704
case impl::ECT_COPY_IMAGE_TO_BUFFER:
699705
{
700706
auto& c = cmd.get<impl::ECT_COPY_IMAGE_TO_BUFFER>();
701707

702-
copyImageToBuffer(c, gl, ctxlocal, ctxid);
708+
copyImageToBuffer(c, gl, ctxlocal, ctxid, m_logger.getOptRawPtr());
703709
}
704710
break;
705711
case impl::ECT_BLIT_IMAGE:

src/nbl/video/COpenGLCommandBuffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,9 @@ class COpenGLCommandBuffer final : public IGPUCommandBuffer
467467
#undef _NBL_SCMD_TYPE_FOR_ECT
468468
#undef _NBL_COMMAND_TYPES_LIST
469469

470-
static void copyBufferToImage(const SCmd<impl::ECT_COPY_BUFFER_TO_IMAGE>& c, IOpenGL_FunctionTable* gl, SOpenGLContextLocalCache* ctxlocal, uint32_t ctxid);
470+
static void copyBufferToImage(const SCmd<impl::ECT_COPY_BUFFER_TO_IMAGE>& c, IOpenGL_FunctionTable* gl, SOpenGLContextLocalCache* ctxlocal, uint32_t ctxid, const system::logger_opt_ptr logger);
471471

472-
static void copyImageToBuffer(const SCmd<impl::ECT_COPY_IMAGE_TO_BUFFER>& c, IOpenGL_FunctionTable* gl, SOpenGLContextLocalCache* ctxlocal, uint32_t ctxid);
472+
static void copyImageToBuffer(const SCmd<impl::ECT_COPY_IMAGE_TO_BUFFER>& c, IOpenGL_FunctionTable* gl, SOpenGLContextLocalCache* ctxlocal, uint32_t ctxid, const system::logger_opt_ptr logger);
473473

474474
static void beginRenderpass_clearAttachments(IOpenGL_FunctionTable* gl, SOpenGLContextLocalCache* ctxlocal, uint32_t ctxid, const SRenderpassBeginInfo& info, GLuint fbo, const system::logger_opt_ptr logger);
475475

0 commit comments

Comments
 (0)