Skip to content

Commit cb0fb23

Browse files
committed
compatability_cast and OpenGL Device Object casts
1 parent b3a3892 commit cb0fb23

12 files changed

+43
-43
lines changed

include/nbl/video/decl/IBackendObject.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,27 @@ class IBackendObject
2323
bool isCompatibleDevicewise(const IBackendObject* other) const;
2424

2525
bool wasCreatedBy(const ILogicalDevice* device) const;
26-
26+
27+
// returns nullptr if `base` is not compatible with device
2728
template<typename derived_t_ptr, typename base_t_ptr>
28-
static inline derived_t_ptr cast(base_t_ptr base, const ILogicalDevice* device)
29+
static inline derived_t_ptr device_compatibility_cast(base_t_ptr base, const ILogicalDevice* device)
2930
{
3031
using base_t = std::remove_pointer_t<base_t_ptr>;
3132
using derived_t = std::remove_pointer_t<derived_t_ptr>;
3233
static_assert(std::is_base_of_v<IBackendObject, base_t>,"base_t should be derived from IBackendObject");
3334
static_assert(std::is_base_of_v<base_t,derived_t>,"derived_t should be derived from base_t");
34-
if (!base->wasCreatedBy(device))
35+
if (base && !base->wasCreatedBy(device))
3536
return nullptr;
3637
return static_cast<derived_t_ptr>(base);
3738
}
3839

40+
// returns nullptr if `base` is not compatible with `other`
3941
template<typename derived_t_ptr, typename base_t_ptr, typename other_t_ptr>
40-
static inline derived_t_ptr cast(base_t_ptr base, const other_t_ptr& compatibleWith)
42+
static inline derived_t_ptr compatibility_cast(base_t_ptr base, const other_t_ptr& compatibleWith)
4143
{
42-
return cast<derived_t_ptr,base_t_ptr>(base,compatibleWith->getOriginDevice());
44+
using other_t = std::remove_pointer_t<other_t_ptr>;
45+
static_assert(std::is_base_of_v<IBackendObject, other_t>,"other_t_ptr should be derived from IBackendObject");
46+
return device_compatibility_cast<derived_t_ptr,base_t_ptr>(base,compatibleWith->getOriginDevice());
4347
}
4448

4549
const ILogicalDevice* getOriginDevice() const;

src/nbl/video/COpenGLBufferView.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class COpenGLBufferView : public IGPUBufferView
2727
m_GLformat = getSizedOpenGLFormatFromOurFormat(gl, m_format);
2828

2929
if (m_offset==0u && m_size==m_buffer->getSize())
30-
gl->extGlTextureBuffer(m_textureName, m_GLformat, static_cast<COpenGLBuffer*>(m_buffer.get())->getOpenGLName());
30+
gl->extGlTextureBuffer(m_textureName, m_GLformat, IBackendObject::compatibility_cast<COpenGLBuffer*>(m_buffer.get(), this)->getOpenGLName());
3131
else
32-
gl->extGlTextureBufferRange(m_textureName, m_GLformat, static_cast<COpenGLBuffer*>(m_buffer.get())->getOpenGLName(), m_offset, m_size);
32+
gl->extGlTextureBufferRange(m_textureName, m_GLformat, IBackendObject::compatibility_cast<COpenGLBuffer*>(m_buffer.get(), this)->getOpenGLName(), m_offset, m_size);
3333

3434
m_textureSize = m_size / asset::getTexelOrBlockBytesize(m_format);
3535
}

src/nbl/video/COpenGLCommandBuffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ class COpenGLCommandBuffer final : public IGPUCommandBuffer
11991199
if (pDescriptorSets[i])
12001200
{
12011201
cmd.descriptorSets[cmd.dsCount++] = core::smart_refctd_ptr<const IGPUDescriptorSet>(pDescriptorSets[i]);
1202-
const auto count = static_cast<const COpenGLDescriptorSet*>(pDescriptorSets[i])->getDynamicOffsetCount();
1202+
const auto count = IBackendObject::compatibility_cast<const COpenGLDescriptorSet*>(pDescriptorSets[i], this)->getDynamicOffsetCount();
12031203
std::copy_n(dynamicOffsetsIt,count,cmd.dynamicOffsets+cmd.dynamicOffsetCount);
12041204
cmd.dynamicOffsetCount += count;
12051205
dynamicOffsetsIt += count;

src/nbl/video/COpenGLComputePipeline.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class COpenGLComputePipeline : public IGPUComputePipeline, public IOpenGLPipelin
5959
uint32_t stampValue = _pcState.getStamp(IGPUShader::ESS_COMPUTE);
6060
if (stampValue>m_lastUpdateStamp)
6161
{
62-
auto uniforms = static_cast<COpenGLSpecializedShader*>(m_shader.get())->getUniforms();
63-
auto locations = static_cast<COpenGLSpecializedShader*>(m_shader.get())->getLocations();
62+
auto uniforms = IBackendObject::compatibility_cast<COpenGLSpecializedShader*>(m_shader.get(), this)->getUniforms();
63+
auto locations = IBackendObject::compatibility_cast<COpenGLSpecializedShader*>(m_shader.get(), this)->getLocations();
6464
if (uniforms.size())
6565
IOpenGLPipeline<1>::setUniformsImitatingPushConstants(gl, 0u, _ctxID, _pcState.data, uniforms, locations);
6666
m_lastUpdateStamp = stampValue;

src/nbl/video/COpenGLFramebuffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class COpenGLFramebuffer final : public IGPUFramebuffer
165165
const auto& att = attachments[a];
166166
const auto& d = descriptions[a];
167167

168-
auto* glatt = static_cast<COpenGLImageView*>(att.get());
168+
auto* glatt = IBackendObject::compatibility_cast<COpenGLImageView*>(att.get(), this);
169169
const GLuint glname = glatt->getOpenGLName();
170170
//gl->glTexture.pglBindTexture(GL_TEXTURE_2D, glname); // what was it for???
171171
const GLenum textarget = COpenGLImageView::ViewTypeToGLenumTarget[glatt->getCreationParameters().viewType];
@@ -179,7 +179,7 @@ class COpenGLFramebuffer final : public IGPUFramebuffer
179179
if (sub.depthStencilAttachment)
180180
{
181181
const auto& att = attachments[sub.depthStencilAttachment->attachment];
182-
auto* glatt = static_cast<COpenGLImageView*>(att.get());
182+
auto* glatt = IBackendObject::compatibility_cast<COpenGLImageView*>(att.get(), this);
183183
const GLuint glname = glatt->getOpenGLName();
184184
const GLenum textarget = COpenGLImageView::ViewTypeToGLenumTarget[glatt->getCreationParameters().viewType];
185185

src/nbl/video/COpenGLImageView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class COpenGLImageView final : public IGPUImageView
5858
//GL_INVALID_OPERATION is generated if texture has already been bound or otherwise given a target.
5959
//thus we cannot create a name for view with glCreateTextures
6060
gl->glTexture.pglGenTextures(1, &name);
61-
gl->extGlTextureView( name, target, static_cast<COpenGLImage*>(params.image.get())->getOpenGLName(), internalFormat,
61+
gl->extGlTextureView( name, target, IBackendObject::compatibility_cast<COpenGLImage*>(params.image.get(), this)->getOpenGLName(), internalFormat,
6262
params.subresourceRange.baseMipLevel, params.subresourceRange.levelCount,
6363
params.subresourceRange.baseArrayLayer, params.subresourceRange.layerCount);
6464

src/nbl/video/COpenGLPipelineCache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class COpenGLPipelineCache final : public IGPUPipelineCache
4141
for (uint32_t i = 0u; i < _count; ++i)
4242
{
4343
{
44-
const auto& src = static_cast<const COpenGLPipelineCache*>(_srcCaches[i])->m_cache;
44+
const auto& src = IBackendObject::compatibility_cast<const COpenGLPipelineCache*>(_srcCaches[i], this)->m_cache;
4545
m_cache.insert(src.begin(), src.end());
4646
}
4747
{
48-
const auto& src = static_cast<const COpenGLPipelineCache*>(_srcCaches[i])->m_parsedSpirvs;
48+
const auto& src = IBackendObject::compatibility_cast<const COpenGLPipelineCache*>(_srcCaches[i], this)->m_parsedSpirvs;
4949
m_parsedSpirvs.insert(src.begin(), src.end());
5050
}
5151
}

src/nbl/video/COpenGLRenderpassIndependentPipeline.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ class COpenGLRenderpassIndependentPipeline final : public IGPURenderpassIndepend
133133
uint32_t stampValue = _pcState.getStamp(stage);
134134
if (stampValue>m_lastUpdateStamp[i])
135135
{
136-
auto uniforms = static_cast<COpenGLSpecializedShader*>(m_shaders[i].get())->getUniforms();
137-
auto locations = static_cast<COpenGLSpecializedShader*>(m_shaders[i].get())->getLocations();
136+
auto uniforms = IBackendObject::compatibility_cast<COpenGLSpecializedShader*>(m_shaders[i].get(), this)->getUniforms();
137+
auto locations = IBackendObject::compatibility_cast<COpenGLSpecializedShader*>(m_shaders[i].get(), this)->getLocations();
138138
if (uniforms.size())
139139
IOpenGLPipeline<SHADER_STAGE_COUNT>::setUniformsImitatingPushConstants(gl, i, _ctxID, _pcState.data, uniforms, locations);
140140
m_lastUpdateStamp[i] = stampValue;

src/nbl/video/COpenGL_LogicalDevice.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class COpenGL_LogicalDevice : public IOpenGL_LogicalDevice
302302
bool resetFences(uint32_t _count, IGPUFence*const * _fences) override final
303303
{
304304
for (uint32_t i = 0u; i < _count; ++i)
305-
static_cast<COpenGLFence*>(_fences[i])->reset();
305+
IBackendObject::device_compatibility_cast<COpenGLFence*>(_fences[i], this)->reset();
306306
return true;
307307
}
308308

@@ -570,7 +570,7 @@ class COpenGL_LogicalDevice : public IOpenGL_LogicalDevice
570570
}
571571
core::smart_refctd_ptr<IGPUSpecializedShader> createGPUSpecializedShader_impl(const IGPUShader* _unspecialized, const asset::ISpecializedShader::SInfo& _specInfo, const asset::ISPIRVOptimizer* _spvopt = nullptr) override final
572572
{
573-
const COpenGLShader* glUnspec = static_cast<const COpenGLShader*>(_unspecialized);
573+
const COpenGLShader* glUnspec = IBackendObject::device_compatibility_cast<const COpenGLShader*>(_unspecialized, this);
574574

575575
const std::string& EP = _specInfo.entryPoint;
576576
const asset::IShader::E_SHADER_STAGE stage = _unspecialized->getStage();

src/nbl/video/COpenGL_Queue.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class COpenGL_Queue final : public IGPUQueue
210210
for (uint32_t i = 0; i < submit.waitSemaphoreCount; ++i)
211211
{
212212
IGPUSemaphore* sem = submit.pWaitSemaphores[i].get();
213-
COpenGLSemaphore* glsem = static_cast<COpenGLSemaphore*>(sem);
213+
COpenGLSemaphore* glsem = IBackendObject::device_compatibility_cast<COpenGLSemaphore*>(sem, m_device);
214214
glsem->wait(&gl);
215215
}
216216

@@ -231,7 +231,7 @@ class COpenGL_Queue final : public IGPUQueue
231231
// also: we can limit flushing to bindings (especially buffers and textures): vertex, index, SSBO, UBO, indirect, ...
232232
ctxlocal.flushStateGraphics(&gl, SOpenGLContextLocalCache::GSB_ALL, m_ctxid);
233233
ctxlocal.flushStateCompute(&gl, SOpenGLContextLocalCache::GSB_ALL, m_ctxid);
234-
auto* cmdbuf = static_cast<COpenGLCommandBuffer*>(submit.commandBuffers[i].get());
234+
auto* cmdbuf = IBackendObject::device_compatibility_cast<COpenGLCommandBuffer*>(submit.commandBuffers[i].get(), m_device);
235235
cmdbuf->executeAll(&gl, &_state.ctxlocal, m_ctxid);
236236
}
237237

@@ -341,7 +341,7 @@ class COpenGL_Queue final : public IGPUQueue
341341
params.commandBufferCount = submit.commandBufferCount;
342342
for (uint32_t i = 0u; i < submit.signalSemaphoreCount; ++i)
343343
{
344-
COpenGLSemaphore* sem = static_cast<COpenGLSemaphore*>(submit.pSignalSemaphores[i]);
344+
COpenGLSemaphore* sem = IBackendObject::device_compatibility_cast<COpenGLSemaphore*>(submit.pSignalSemaphores[i], m_originDevice);
345345
sem->associateGLSync(core::smart_refctd_ptr(sync));
346346
}
347347
core::smart_refctd_ptr<IGPUSemaphore>* waitSems = nullptr;
@@ -382,7 +382,7 @@ class COpenGL_Queue final : public IGPUQueue
382382

383383
if (_fence)
384384
{
385-
COpenGLFence* glfence = static_cast<COpenGLFence*>(_fence);
385+
COpenGLFence* glfence = IBackendObject::device_compatibility_cast<COpenGLFence*>(_fence, m_originDevice);
386386
glfence->associateGLSync(std::move(sync)); // associate sync used for signal semaphores in last submit
387387
}
388388

0 commit comments

Comments
 (0)