Commit 3da335d4 authored by Laszlo Agocs's avatar Laszlo Agocs

gl: tex imp/exp

plus rework it a bit for metal
parent abcdc673
......@@ -69,8 +69,8 @@ struct QRhiResourceUpdateBatchPrivate;
// 1. new*() does not create underlying graphics resources. build() does.
// 2. Except: new*Descriptor() implicitly "builds". (no build() for QRhiRenderPassDescriptor f.ex.)
// 3. release() schedules graphics resources for destruction. The C++ object is reusable immediately via build(), or can be destroyed.
// 4. build() on an already built object calls release() implicitly. Except swapchains. (buildOrResize - special semantics)
// 5. Ownership of resources imported via QRhi*InitParams is not taken.
// 4. build() on an already built object calls release() implicitly. Except swapchains. (buildOrResize() - special semantics)
// 5. Ownership of resources imported (QRhi*InitParams or buildFrom()) or exported (nativeHandles()) is never taken or given away.
//
// Other:
// 1. QRhiResourceUpdateBatch manages no graphics resources underneath. beginPass() implicitly calls release() on the batch.
......
......@@ -1609,9 +1609,12 @@ void QGles2Texture::release()
texture = 0;
specified = false;
nativeHandlesStruct.texture = 0;
QRHI_RES_RHI(QRhiGles2);
rhiD->releaseQueue.append(e);
if (owns) {
QRHI_RES_RHI(QRhiGles2);
rhiD->releaseQueue.append(e);
}
}
static inline bool isPowerOfTwo(int x)
......@@ -1620,7 +1623,7 @@ static inline bool isPowerOfTwo(int x)
return x == (x & -x);
}
bool QGles2Texture::build()
bool QGles2Texture::prepareBuild(QSize *adjustedSize)
{
QRHI_RES_RHI(QRhiGles2);
......@@ -1639,7 +1642,6 @@ bool QGles2Texture::build()
const bool isCube = m_flags.testFlag(CubeMap);
const bool hasMipMaps = m_flags.testFlag(MipMapped);
const int mipLevelCount = hasMipMaps ? qCeil(log2(qMax(size.width(), size.height()))) + 1 : 1;
const bool isCompressed = rhiD->isCompressedFormat(m_format);
// ### more formats
......@@ -1647,6 +1649,7 @@ bool QGles2Texture::build()
glintformat = GL_RGBA;
glformat = GL_RGBA;
gltype = GL_UNSIGNED_BYTE;
mipLevelCount = hasMipMaps ? qCeil(log2(qMax(size.width(), size.height()))) + 1 : 1;
if (isCompressed) {
glintformat = toGlCompressedTextureFormat(m_format, m_flags);
......@@ -1656,8 +1659,25 @@ bool QGles2Texture::build()
}
}
if (adjustedSize)
*adjustedSize = size;
return true;
}
bool QGles2Texture::build()
{
QRHI_RES_RHI(QRhiGles2);
QSize size;
if (!prepareBuild(&size))
return false;
rhiD->f->glGenTextures(1, &texture);
const bool isCube = m_flags.testFlag(CubeMap);
const bool hasMipMaps = m_flags.testFlag(MipMapped);
const bool isCompressed = rhiD->isCompressedFormat(m_format);
if (!isCompressed) {
rhiD->f->glBindTexture(target, texture);
if (hasMipMaps || isCube) {
......@@ -1682,10 +1702,37 @@ bool QGles2Texture::build()
specified = false;
}
owns = true;
nativeHandlesStruct.texture = texture;
generation += 1;
return true;
}
bool QGles2Texture::buildFrom(QRhiNativeHandles *src)
{
QRhiGles2TextureNativeHandles *h = static_cast<QRhiGles2TextureNativeHandles *>(src);
if (!h || !h->texture)
return false;
if (!prepareBuild())
return false;
texture = h->texture;
specified = true;
owns = false;
nativeHandlesStruct.texture = texture;
generation += 1;
return true;
}
QRhiNativeHandles *QGles2Texture::nativeHandles()
{
return &nativeHandlesStruct;
}
QGles2Sampler::QGles2Sampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v, w)
......
......@@ -67,6 +67,11 @@ struct Q_RHI_EXPORT QRhiGles2NativeHandles : public QRhiNativeHandles
QOpenGLContext *context;
};
struct Q_RHI_EXPORT QRhiGles2TextureNativeHandles : public QRhiNativeHandles
{
uint texture = 0;
};
QT_END_NAMESPACE
#endif
......@@ -91,13 +91,20 @@ struct QGles2Texture : public QRhiTexture
int sampleCount, Flags flags);
void release() override;
bool build() override;
bool buildFrom(QRhiNativeHandles *src) override;
QRhiNativeHandles *nativeHandles() override;
bool prepareBuild(QSize *adjustedSize = nullptr);
GLuint texture = 0;
bool owns = true;
GLenum target;
GLenum glintformat;
GLenum glformat;
GLenum gltype;
bool specified = false;
int mipLevelCount = 0;
QRhiGles2TextureNativeHandles nativeHandlesStruct;
uint generation = 0;
friend class QRhiGles2;
......
......@@ -1470,8 +1470,10 @@ static inline MTLPixelFormat toMetalTextureFormat(QRhiTexture::Format format, QR
}
}
bool QMetalTexture::build()
bool QMetalTexture::prepareBuild(QSize *adjustedSize)
{
QRHI_RES_RHI(QRhiMetal);
if (d->tex)
release();
......@@ -1479,7 +1481,6 @@ bool QMetalTexture::build()
const bool isCube = m_flags.testFlag(CubeMap);
const bool hasMipMaps = m_flags.testFlag(MipMapped);
QRHI_RES_RHI(QRhiMetal);
d->format = toMetalTextureFormat(m_format, m_flags);
mipLevelCount = hasMipMaps ? qCeil(log2(qMax(size.width(), size.height()))) + 1 : 1;
samples = rhiD->effectiveSampleCount(m_sampleCount);
......@@ -1494,8 +1495,23 @@ bool QMetalTexture::build()
}
}
if (adjustedSize)
*adjustedSize = size;
return true;
}
bool QMetalTexture::build()
{
QRHI_RES_RHI(QRhiMetal);
QSize size;
if (!prepareBuild(&size))
return false;
MTLTextureDescriptor *desc = [[MTLTextureDescriptor alloc] init];
if (isCube)
if (m_flags.testFlag(CubeMap))
desc.textureType = MTLTextureTypeCube;
else
desc.textureType = samples > 1 ? MTLTextureType2DMultisample : MTLTextureType2D;
......@@ -1522,26 +1538,14 @@ bool QMetalTexture::build()
return true;
}
QRhiNativeHandles *QMetalTexture::nativeHandles()
{
return &nativeHandlesStruct;
}
bool QMetalTexture::buildFrom(QRhiNativeHandles *src)
{
QRhiMetalTextureNativeHandles *h = static_cast<QRhiMetalTextureNativeHandles *>(src);
if (!h || !h->texture)
return false;
if (d->tex)
release();
QRHI_RES_RHI(QRhiMetal);
d->format = toMetalTextureFormat(m_format, m_flags);
const QSize size = m_pixelSize.isEmpty() ? QSize(16, 16) : m_pixelSize;
const bool hasMipMaps = m_flags.testFlag(MipMapped);
mipLevelCount = hasMipMaps ? qCeil(log2(qMax(size.width(), size.height()))) + 1 : 1;
samples = rhiD->effectiveSampleCount(m_sampleCount);
if (!prepareBuild())
return false;
d->tex = (id<MTLTexture>) h->texture;
......@@ -1553,6 +1557,11 @@ bool QMetalTexture::buildFrom(QRhiNativeHandles *src)
return true;
}
QRhiNativeHandles *QMetalTexture::nativeHandles()
{
return &nativeHandlesStruct;
}
QMetalSampler::QMetalSampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
AddressMode u, AddressMode v, AddressMode w)
: QRhiSampler(rhi, magFilter, minFilter, mipmapMode, u, v, w),
......
......@@ -93,8 +93,10 @@ struct QMetalTexture : public QRhiTexture
~QMetalTexture();
void release() override;
bool build() override;
QRhiNativeHandles *nativeHandles() override;
bool buildFrom(QRhiNativeHandles *src) override;
QRhiNativeHandles *nativeHandles() override;
bool prepareBuild(QSize *adjustedSize = nullptr);
QMetalTextureData *d;
QRhiMetalTextureNativeHandles nativeHandlesStruct;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment