| 1 |
// Copyright (C) 2019 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "../shared/examplefw.h" |
| 5 |
#include <QImage> |
| 6 |
#include <QPainter> |
| 7 |
#include <functional> |
| 8 |
|
| 9 |
static float quadVertexData[] = { // Y up, CCW |
| 10 |
-0.5f, 0.5f, 0.0f, 0.0f, -0.5f, -0.5f, 0.0f, 1.0f, |
| 11 |
0.5f, -0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 1.0f, 0.0f |
| 12 |
}; |
| 13 |
|
| 14 |
static quint16 quadIndexData[] = { 0, 1, 2, 0, 2, 3 }; |
| 15 |
|
| 16 |
struct |
| 17 |
{ |
| 18 |
QList<QRhiResource *> releasePool; |
| 19 |
QRhiResourceUpdateBatch *initialUpdates = nullptr; |
| 20 |
|
| 21 |
QRhiBuffer *vertexBuffer = nullptr; |
| 22 |
QRhiBuffer *indexBuffer = nullptr; |
| 23 |
QRhiBuffer *uniformBuffer = nullptr; |
| 24 |
QRhiGraphicsPipeline *ps = nullptr; |
| 25 |
QRhiShaderResourceBindings *srb = nullptr; |
| 26 |
int index = 0; |
| 27 |
} d; |
| 28 |
|
| 29 |
void readBackCompleted(QRhiReadbackResult *result, const QByteArray &expected) |
| 30 |
{ |
| 31 |
if (result->data != expected) { |
| 32 |
qFatal("texture readback data did not match expected values"); |
| 33 |
} |
| 34 |
|
| 35 |
delete result; |
| 36 |
} |
| 37 |
|
| 38 |
void Window::customInit() |
| 39 |
{ |
| 40 |
if (!m_r->isFeatureSupported(QRhi::Feature::OneDimensionalTextures)) |
| 41 |
qFatal("1D textures are not supported"); |
| 42 |
|
| 43 |
const bool mipmaps = m_r->isFeatureSupported(QRhi::Feature::OneDimensionalTextureMipmaps); |
| 44 |
|
| 45 |
d.initialUpdates = m_r->nextResourceUpdateBatch(); |
| 46 |
|
| 47 |
QRhiTexture *texture = nullptr; |
| 48 |
QRhiReadbackResult *readbackResult = nullptr; |
| 49 |
QRhiReadbackDescription readbackDescription; |
| 50 |
QByteArray data; |
| 51 |
|
| 52 |
QList<QRhiShaderResourceBinding> shaderResouceBindings; |
| 53 |
|
| 54 |
// |
| 55 |
// Create vertex buffer |
| 56 |
// |
| 57 |
d.vertexBuffer = |
| 58 |
m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(quadVertexData)); |
| 59 |
d.vertexBuffer->create(); |
| 60 |
d.releasePool << d.vertexBuffer; |
| 61 |
|
| 62 |
d.initialUpdates->uploadStaticBuffer(d.vertexBuffer, 0, sizeof(quadVertexData), quadVertexData); |
| 63 |
|
| 64 |
// |
| 65 |
// Create index buffer |
| 66 |
// |
| 67 |
d.indexBuffer = |
| 68 |
m_r->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::IndexBuffer, sizeof(quadIndexData)); |
| 69 |
d.indexBuffer->create(); |
| 70 |
d.releasePool << d.indexBuffer; |
| 71 |
|
| 72 |
d.initialUpdates->uploadStaticBuffer(d.indexBuffer, quadIndexData); |
| 73 |
|
| 74 |
// |
| 75 |
// Create uniform buffer |
| 76 |
// |
| 77 |
d.uniformBuffer = |
| 78 |
m_r->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, m_r->ubufAligned(4)); |
| 79 |
d.uniformBuffer->create(); |
| 80 |
d.releasePool << d.uniformBuffer; |
| 81 |
|
| 82 |
shaderResouceBindings.append(QRhiShaderResourceBinding::uniformBuffer( |
| 83 |
0, QRhiShaderResourceBinding::StageFlag::FragmentStage, d.uniformBuffer, 0, 4)); |
| 84 |
|
| 85 |
// |
| 86 |
// Create samplers |
| 87 |
// |
| 88 |
QRhiSampler *samplerNearest = m_r->newSampler( |
| 89 |
QRhiSampler::Filter::Nearest, QRhiSampler::Filter::Nearest, |
| 90 |
QRhiSampler::Filter::Nearest, QRhiSampler::AddressMode::ClampToEdge, |
| 91 |
QRhiSampler::AddressMode::ClampToEdge, QRhiSampler::AddressMode::ClampToEdge); |
| 92 |
d.releasePool << samplerNearest; |
| 93 |
samplerNearest->create(); |
| 94 |
|
| 95 |
QRhiSampler *samplerNone = m_r->newSampler( |
| 96 |
QRhiSampler::Filter::Nearest, QRhiSampler::Filter::Nearest, QRhiSampler::Filter::None, |
| 97 |
QRhiSampler::AddressMode::ClampToEdge, QRhiSampler::AddressMode::ClampToEdge, |
| 98 |
QRhiSampler::AddressMode::ClampToEdge); |
| 99 |
d.releasePool << samplerNone; |
| 100 |
samplerNone->create(); |
| 101 |
|
| 102 |
// |
| 103 |
// 1D texture with generated mipmaps. Contains grey colormap |
| 104 |
// |
| 105 |
texture = m_r->newTexture(QRhiTexture::Format::RGBA8, 8, 0, 0, 1, |
| 106 |
QRhiTexture::Flag::OneDimensional |
| 107 |
| QRhiTexture::Flag::UsedAsTransferSource |
| 108 |
| (mipmaps ? (QRhiTexture::Flag::MipMapped |
| 109 |
| QRhiTexture::Flag::UsedWithGenerateMips) |
| 110 |
: QRhiTexture::Flag(0))); |
| 111 |
d.releasePool << texture; |
| 112 |
texture->create(); |
| 113 |
|
| 114 |
shaderResouceBindings.append(QRhiShaderResourceBinding::sampledTexture( |
| 115 |
1, QRhiShaderResourceBinding::FragmentStage, texture, |
| 116 |
texture->flags().testFlag(QRhiTexture::Flag::MipMapped) ? samplerNearest |
| 117 |
: samplerNone)); |
| 118 |
|
| 119 |
for (int i = 0; i < 8; ++i) { |
| 120 |
data.append(char(i) * 32); |
| 121 |
data.append(char(i) * 32); |
| 122 |
data.append(char(i) * 32); |
| 123 |
data.append(char(255)); |
| 124 |
} |
| 125 |
d.initialUpdates->uploadTexture( |
| 126 |
texture, QRhiTextureUploadEntry(0, 0, QRhiTextureSubresourceUploadDescription(data))); |
| 127 |
if (mipmaps) |
| 128 |
d.initialUpdates->generateMips(texture); |
| 129 |
|
| 130 |
readbackResult = new QRhiReadbackResult; |
| 131 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 132 |
readbackDescription.setTexture(texture); |
| 133 |
readbackDescription.setLayer(0); |
| 134 |
readbackDescription.setLevel(0); |
| 135 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 136 |
|
| 137 |
// |
| 138 |
// 1D texture array with generated mipmaps. layer 0 contains red colormap, layer 1 contains |
| 139 |
// green colormap |
| 140 |
// |
| 141 |
texture = |
| 142 |
m_r->newTextureArray(QRhiTexture::RGBA8, 2, QSize(8, 0), 1, |
| 143 |
QRhiTexture::Flag::TextureArray | QRhiTexture::Flag::OneDimensional |
| 144 |
| QRhiTexture::Flag::UsedAsTransferSource |
| 145 |
| (mipmaps ? (QRhiTexture::Flag::MipMapped |
| 146 |
| QRhiTexture::Flag::UsedWithGenerateMips) |
| 147 |
: QRhiTexture::Flag(0))); |
| 148 |
d.releasePool << texture; |
| 149 |
texture->create(); |
| 150 |
|
| 151 |
shaderResouceBindings.append(QRhiShaderResourceBinding::sampledTexture( |
| 152 |
2, QRhiShaderResourceBinding::FragmentStage, texture, |
| 153 |
texture->flags().testFlag(QRhiTexture::Flag::MipMapped) ? samplerNearest |
| 154 |
: samplerNone)); |
| 155 |
|
| 156 |
data.clear(); |
| 157 |
for (int i = 0; i < 8; ++i) { |
| 158 |
data.append(char(i * 32)); |
| 159 |
data.append(char(0)); |
| 160 |
data.append(char(0)); |
| 161 |
data.append(char(255)); |
| 162 |
} |
| 163 |
d.initialUpdates->uploadTexture( |
| 164 |
texture, QRhiTextureUploadEntry(0, 0, QRhiTextureSubresourceUploadDescription(data))); |
| 165 |
|
| 166 |
readbackResult = new QRhiReadbackResult; |
| 167 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 168 |
readbackDescription.setTexture(texture); |
| 169 |
readbackDescription.setLayer(0); |
| 170 |
readbackDescription.setLevel(0); |
| 171 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 172 |
|
| 173 |
data.clear(); |
| 174 |
for (int i = 0; i < 8; ++i) { |
| 175 |
data.append(char(0)); |
| 176 |
data.append(char(i * 32)); |
| 177 |
data.append(char(0)); |
| 178 |
data.append(char(255)); |
| 179 |
} |
| 180 |
d.initialUpdates->uploadTexture( |
| 181 |
texture, QRhiTextureUploadEntry(1, 0, QRhiTextureSubresourceUploadDescription(data))); |
| 182 |
if (mipmaps) |
| 183 |
d.initialUpdates->generateMips(texture); |
| 184 |
|
| 185 |
readbackResult = new QRhiReadbackResult; |
| 186 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 187 |
readbackDescription.setTexture(texture); |
| 188 |
readbackDescription.setLayer(1); |
| 189 |
readbackDescription.setLevel(0); |
| 190 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 191 |
|
| 192 |
// |
| 193 |
// 1D texture with uploaded mipmaps. Contains yellow colormap |
| 194 |
// |
| 195 |
texture = m_r->newTexture( |
| 196 |
QRhiTexture::Format::RGBA8, 8, 0, 0, 1, |
| 197 |
QRhiTexture::Flag::OneDimensional | QRhiTexture::Flag::UsedAsTransferSource |
| 198 |
| (mipmaps ? QRhiTexture::Flag::MipMapped : QRhiTexture::Flag(0))); |
| 199 |
d.releasePool << texture; |
| 200 |
texture->create(); |
| 201 |
|
| 202 |
shaderResouceBindings.append(QRhiShaderResourceBinding::sampledTexture( |
| 203 |
3, QRhiShaderResourceBinding::FragmentStage, texture, |
| 204 |
texture->flags().testFlag(QRhiTexture::Flag::MipMapped) ? samplerNearest |
| 205 |
: samplerNone)); |
| 206 |
|
| 207 |
data.clear(); |
| 208 |
for (int i = 0; i < 8; ++i) { |
| 209 |
data.append(char(i * 32)); |
| 210 |
data.append(char(i * 32)); |
| 211 |
data.append(char(0)); |
| 212 |
data.append(char(255)); |
| 213 |
} |
| 214 |
d.initialUpdates->uploadTexture( |
| 215 |
texture, QRhiTextureUploadEntry(0, 0, QRhiTextureSubresourceUploadDescription(data))); |
| 216 |
|
| 217 |
readbackResult = new QRhiReadbackResult; |
| 218 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 219 |
readbackDescription.setTexture(texture); |
| 220 |
readbackDescription.setLayer(0); |
| 221 |
readbackDescription.setLevel(0); |
| 222 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 223 |
|
| 224 |
QRhiTexture *textureSource = texture; |
| 225 |
QByteArray textureCopyData = data.mid(data.size() / 2).append(data.mid(0, data.size() / 2)); |
| 226 |
|
| 227 |
if (mipmaps) { |
| 228 |
|
| 229 |
data.clear(); |
| 230 |
for (int i = 0; i < 4; ++i) { |
| 231 |
data.append(char(i * 64)); |
| 232 |
data.append(char(i * 64)); |
| 233 |
data.append(char(0)); |
| 234 |
data.append(char(255)); |
| 235 |
} |
| 236 |
d.initialUpdates->uploadTexture( |
| 237 |
texture, |
| 238 |
QRhiTextureUploadEntry(0, 1, QRhiTextureSubresourceUploadDescription(data))); |
| 239 |
|
| 240 |
readbackResult = new QRhiReadbackResult; |
| 241 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 242 |
readbackDescription.setTexture(texture); |
| 243 |
readbackDescription.setLayer(0); |
| 244 |
readbackDescription.setLevel(1); |
| 245 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 246 |
|
| 247 |
data.clear(); |
| 248 |
for (int i = 0; i < 2; ++i) { |
| 249 |
data.append(char(i * 128)); |
| 250 |
data.append(char(i * 128)); |
| 251 |
data.append(char(0)); |
| 252 |
data.append(char(255)); |
| 253 |
} |
| 254 |
d.initialUpdates->uploadTexture( |
| 255 |
texture, |
| 256 |
QRhiTextureUploadEntry(0, 2, QRhiTextureSubresourceUploadDescription(data))); |
| 257 |
|
| 258 |
readbackResult = new QRhiReadbackResult; |
| 259 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 260 |
readbackDescription.setTexture(texture); |
| 261 |
readbackDescription.setLayer(0); |
| 262 |
readbackDescription.setLevel(2); |
| 263 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 264 |
|
| 265 |
data.clear(); |
| 266 |
data.append(char(128)); |
| 267 |
data.append(char(128)); |
| 268 |
data.append(char(0)); |
| 269 |
data.append(char(255)); |
| 270 |
d.initialUpdates->uploadTexture( |
| 271 |
texture, |
| 272 |
QRhiTextureUploadEntry(0, 3, QRhiTextureSubresourceUploadDescription(data))); |
| 273 |
|
| 274 |
readbackResult = new QRhiReadbackResult; |
| 275 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 276 |
readbackDescription.setTexture(texture); |
| 277 |
readbackDescription.setLayer(0); |
| 278 |
readbackDescription.setLevel(3); |
| 279 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 280 |
} |
| 281 |
|
| 282 |
// |
| 283 |
// 1D texture array with uploaded mipmaps. Layer 0 contains blue colormap, layer 1 contains |
| 284 |
// magenta colormap |
| 285 |
// |
| 286 |
texture = m_r->newTextureArray( |
| 287 |
QRhiTexture::Format::RGBA8, 2, QSize(8, 0), 1, |
| 288 |
QRhiTexture::Flag::TextureArray | QRhiTexture::Flag::OneDimensional |
| 289 |
| QRhiTexture::Flag::UsedAsTransferSource |
| 290 |
| (mipmaps ? QRhiTexture::Flag::MipMapped : QRhiTexture::Flag(0))); |
| 291 |
d.releasePool << texture; |
| 292 |
texture->create(); |
| 293 |
|
| 294 |
shaderResouceBindings.append(QRhiShaderResourceBinding::sampledTexture( |
| 295 |
4, QRhiShaderResourceBinding::FragmentStage, texture, |
| 296 |
texture->flags().testFlag(QRhiTexture::Flag::MipMapped) ? samplerNearest |
| 297 |
: samplerNone)); |
| 298 |
|
| 299 |
data.clear(); |
| 300 |
for (int i = 0; i < 8; ++i) { |
| 301 |
data.append(char(0)); |
| 302 |
data.append(char(0)); |
| 303 |
data.append(char(i * 32)); |
| 304 |
data.append(char(255)); |
| 305 |
} |
| 306 |
d.initialUpdates->uploadTexture( |
| 307 |
texture, QRhiTextureUploadEntry(0, 0, QRhiTextureSubresourceUploadDescription(data))); |
| 308 |
|
| 309 |
readbackResult = new QRhiReadbackResult; |
| 310 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 311 |
readbackDescription.setTexture(texture); |
| 312 |
readbackDescription.setLayer(0); |
| 313 |
readbackDescription.setLevel(0); |
| 314 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 315 |
|
| 316 |
QRhiTexture *textureArraySource = texture; |
| 317 |
QList<QByteArray> textureArrayCopyData; |
| 318 |
textureArrayCopyData.append(data.mid(data.size() / 2).append(data.mid(0, data.size() / 2))); |
| 319 |
|
| 320 |
if (mipmaps) { |
| 321 |
|
| 322 |
data.clear(); |
| 323 |
for (int i = 0; i < 4; ++i) { |
| 324 |
data.append(char(0)); |
| 325 |
data.append(char(0)); |
| 326 |
data.append(char(i * 64)); |
| 327 |
data.append(char(255)); |
| 328 |
} |
| 329 |
d.initialUpdates->uploadTexture( |
| 330 |
texture, |
| 331 |
QRhiTextureUploadEntry(0, 1, QRhiTextureSubresourceUploadDescription(data))); |
| 332 |
|
| 333 |
readbackResult = new QRhiReadbackResult; |
| 334 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 335 |
readbackDescription.setTexture(texture); |
| 336 |
readbackDescription.setLayer(0); |
| 337 |
readbackDescription.setLevel(1); |
| 338 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 339 |
|
| 340 |
data.clear(); |
| 341 |
for (int i = 0; i < 2; ++i) { |
| 342 |
data.append(char(0)); |
| 343 |
data.append(char(0)); |
| 344 |
data.append(char(i * 128)); |
| 345 |
data.append(char(255)); |
| 346 |
} |
| 347 |
d.initialUpdates->uploadTexture( |
| 348 |
texture, |
| 349 |
QRhiTextureUploadEntry(0, 2, QRhiTextureSubresourceUploadDescription(data))); |
| 350 |
|
| 351 |
readbackResult = new QRhiReadbackResult; |
| 352 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 353 |
readbackDescription.setTexture(texture); |
| 354 |
readbackDescription.setLayer(0); |
| 355 |
readbackDescription.setLevel(2); |
| 356 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 357 |
|
| 358 |
data.clear(); |
| 359 |
data.append(char(0)); |
| 360 |
data.append(char(0)); |
| 361 |
data.append(char(128)); |
| 362 |
data.append(char(255)); |
| 363 |
d.initialUpdates->uploadTexture( |
| 364 |
texture, |
| 365 |
QRhiTextureUploadEntry(0, 3, QRhiTextureSubresourceUploadDescription(data))); |
| 366 |
|
| 367 |
readbackResult = new QRhiReadbackResult; |
| 368 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 369 |
readbackDescription.setTexture(texture); |
| 370 |
readbackDescription.setLayer(0); |
| 371 |
readbackDescription.setLevel(3); |
| 372 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 373 |
} |
| 374 |
|
| 375 |
data.clear(); |
| 376 |
for (int i = 0; i < 8; ++i) { |
| 377 |
data.append(char(i * 32)); |
| 378 |
data.append(char(0)); |
| 379 |
data.append(char(i * 32)); |
| 380 |
data.append(char(255)); |
| 381 |
} |
| 382 |
d.initialUpdates->uploadTexture( |
| 383 |
texture, QRhiTextureUploadEntry(1, 0, QRhiTextureSubresourceUploadDescription(data))); |
| 384 |
|
| 385 |
readbackResult = new QRhiReadbackResult; |
| 386 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 387 |
readbackDescription.setTexture(texture); |
| 388 |
readbackDescription.setLayer(1); |
| 389 |
readbackDescription.setLevel(0); |
| 390 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 391 |
|
| 392 |
textureArrayCopyData.append(data.mid(data.size() / 2).append(data.mid(0, data.size() / 2))); |
| 393 |
|
| 394 |
if (mipmaps) { |
| 395 |
|
| 396 |
data.clear(); |
| 397 |
for (int i = 0; i < 4; ++i) { |
| 398 |
data.append(char(i * 64)); |
| 399 |
data.append(char(0)); |
| 400 |
data.append(char(i * 64)); |
| 401 |
data.append(char(255)); |
| 402 |
} |
| 403 |
d.initialUpdates->uploadTexture( |
| 404 |
texture, |
| 405 |
QRhiTextureUploadEntry(1, 1, QRhiTextureSubresourceUploadDescription(data))); |
| 406 |
|
| 407 |
readbackResult = new QRhiReadbackResult; |
| 408 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 409 |
readbackDescription.setTexture(texture); |
| 410 |
readbackDescription.setLayer(1); |
| 411 |
readbackDescription.setLevel(1); |
| 412 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 413 |
|
| 414 |
data.clear(); |
| 415 |
for (int i = 0; i < 2; ++i) { |
| 416 |
data.append(char(i * 128)); |
| 417 |
data.append(char(0)); |
| 418 |
data.append(char(i * 128)); |
| 419 |
data.append(char(255)); |
| 420 |
} |
| 421 |
d.initialUpdates->uploadTexture( |
| 422 |
texture, |
| 423 |
QRhiTextureUploadEntry(1, 2, QRhiTextureSubresourceUploadDescription(data))); |
| 424 |
|
| 425 |
readbackResult = new QRhiReadbackResult; |
| 426 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 427 |
readbackDescription.setTexture(texture); |
| 428 |
readbackDescription.setLayer(1); |
| 429 |
readbackDescription.setLevel(2); |
| 430 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 431 |
|
| 432 |
data.clear(); |
| 433 |
data.append(char(128)); |
| 434 |
data.append(char(0)); |
| 435 |
data.append(char(128)); |
| 436 |
data.append(char(255)); |
| 437 |
d.initialUpdates->uploadTexture( |
| 438 |
texture, |
| 439 |
QRhiTextureUploadEntry(1, 3, QRhiTextureSubresourceUploadDescription(data))); |
| 440 |
|
| 441 |
readbackResult = new QRhiReadbackResult; |
| 442 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, data); |
| 443 |
readbackDescription.setTexture(texture); |
| 444 |
readbackDescription.setLayer(1); |
| 445 |
readbackDescription.setLevel(3); |
| 446 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 447 |
} |
| 448 |
|
| 449 |
// |
| 450 |
// 1D Texture loaded from image - contains rainbow colormap |
| 451 |
// |
| 452 |
QImage image(256, 1, QImage::Format_RGBA8888); |
| 453 |
for (int i = 0; i < image.width(); ++i) { |
| 454 |
float x = float(i) / float(image.width() - 1); |
| 455 |
if (x < 2.0f / 5.0f) { |
| 456 |
image.setPixelColor(i, 0, QColor::fromRgbF(1.0f, 5.0f / 2.0f * x, 0.0f)); |
| 457 |
} else if (x < 3.0f / 5.0f) { |
| 458 |
image.setPixelColor(i, 0, QColor::fromRgbF(-5.0f * x + 3.0f, 1.0f, 0.0f)); |
| 459 |
} else if (x < 4.0f / 5.0f) { |
| 460 |
image.setPixelColor(i, 0, QColor::fromRgbF(0.0f, -5.0f * x + 4.0f, 5.0f * x - 3.0f)); |
| 461 |
} else { |
| 462 |
image.setPixelColor(i, 0, QColor::fromRgbF(10.0f / 3.0f * x - 8.0f / 3.0f, 0.0f, 1.0f)); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
texture = m_r->newTexture(QRhiTexture::Format::RGBA8, image.width(), 0, 0, 1, |
| 467 |
QRhiTexture::Flag::OneDimensional); |
| 468 |
d.releasePool << texture; |
| 469 |
texture->create(); |
| 470 |
|
| 471 |
shaderResouceBindings.append(QRhiShaderResourceBinding::sampledTexture( |
| 472 |
5, QRhiShaderResourceBinding::FragmentStage, texture, |
| 473 |
texture->flags().testFlag(QRhiTexture::Flag::MipMapped) ? samplerNearest |
| 474 |
: samplerNone)); |
| 475 |
|
| 476 |
d.initialUpdates->uploadTexture(texture, image); |
| 477 |
|
| 478 |
// |
| 479 |
// 1D Texture copied |
| 480 |
// |
| 481 |
texture = m_r->newTexture(QRhiTexture::Format::RGBA8, 8, 0, 0, 1, |
| 482 |
QRhiTexture::Flag::OneDimensional |
| 483 |
| QRhiTexture::Flag::UsedAsTransferSource); |
| 484 |
d.releasePool << texture; |
| 485 |
texture->create(); |
| 486 |
|
| 487 |
shaderResouceBindings.append(QRhiShaderResourceBinding::sampledTexture( |
| 488 |
6, QRhiShaderResourceBinding::FragmentStage, texture, |
| 489 |
texture->flags().testFlag(QRhiTexture::Flag::MipMapped) ? samplerNearest |
| 490 |
: samplerNone)); |
| 491 |
|
| 492 |
QRhiTextureCopyDescription copyDescription; |
| 493 |
copyDescription.setSourceLayer(0); |
| 494 |
copyDescription.setSourceLevel(0); |
| 495 |
copyDescription.setSourceTopLeft(QPoint(4, 0)); |
| 496 |
copyDescription.setDestinationLayer(0); |
| 497 |
copyDescription.setDestinationLevel(0); |
| 498 |
copyDescription.setDestinationTopLeft(QPoint(0, 0)); |
| 499 |
copyDescription.setPixelSize(QSize(4, 1)); |
| 500 |
|
| 501 |
d.initialUpdates->copyTexture(texture, textureSource, copyDescription); |
| 502 |
|
| 503 |
copyDescription.setSourceTopLeft(QPoint(0, 0)); |
| 504 |
copyDescription.setDestinationTopLeft(QPoint(4, 0)); |
| 505 |
|
| 506 |
d.initialUpdates->copyTexture(texture, textureSource, copyDescription); |
| 507 |
|
| 508 |
readbackResult = new QRhiReadbackResult; |
| 509 |
readbackResult->completed = std::bind(readBackCompleted, readbackResult, textureCopyData); |
| 510 |
readbackDescription.setTexture(texture); |
| 511 |
readbackDescription.setLayer(0); |
| 512 |
readbackDescription.setLevel(0); |
| 513 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 514 |
|
| 515 |
// |
| 516 |
// 1D Texture array copied |
| 517 |
// |
| 518 |
texture = m_r->newTextureArray(QRhiTexture::Format::RGBA8, 2, QSize(8, 0), 1, |
| 519 |
QRhiTexture::Flag::OneDimensional |
| 520 |
| QRhiTexture::Flag::UsedAsTransferSource); |
| 521 |
d.releasePool << texture; |
| 522 |
texture->create(); |
| 523 |
|
| 524 |
shaderResouceBindings.append(QRhiShaderResourceBinding::sampledTexture( |
| 525 |
7, QRhiShaderResourceBinding::FragmentStage, texture, |
| 526 |
texture->flags().testFlag(QRhiTexture::Flag::MipMapped) ? samplerNearest |
| 527 |
: samplerNone)); |
| 528 |
|
| 529 |
copyDescription.setSourceLayer(1); |
| 530 |
copyDescription.setSourceLevel(0); |
| 531 |
copyDescription.setSourceTopLeft(QPoint(4, 0)); |
| 532 |
copyDescription.setDestinationLayer(0); |
| 533 |
copyDescription.setDestinationLevel(0); |
| 534 |
copyDescription.setDestinationTopLeft(QPoint(0, 0)); |
| 535 |
copyDescription.setPixelSize(QSize(4, 1)); |
| 536 |
d.initialUpdates->copyTexture(texture, textureArraySource, copyDescription); |
| 537 |
|
| 538 |
copyDescription.setSourceTopLeft(QPoint(0, 0)); |
| 539 |
copyDescription.setDestinationTopLeft(QPoint(4, 0)); |
| 540 |
d.initialUpdates->copyTexture(texture, textureArraySource, copyDescription); |
| 541 |
|
| 542 |
copyDescription.setSourceLayer(0); |
| 543 |
copyDescription.setDestinationLayer(1); |
| 544 |
d.initialUpdates->copyTexture(texture, textureArraySource, copyDescription); |
| 545 |
|
| 546 |
copyDescription.setSourceTopLeft(QPoint(4, 0)); |
| 547 |
copyDescription.setDestinationTopLeft(QPoint(0, 0)); |
| 548 |
d.initialUpdates->copyTexture(texture, textureArraySource, copyDescription); |
| 549 |
|
| 550 |
readbackResult = new QRhiReadbackResult; |
| 551 |
readbackResult->completed = |
| 552 |
std::bind(readBackCompleted, readbackResult, textureArrayCopyData[0]); |
| 553 |
readbackDescription.setTexture(texture); |
| 554 |
readbackDescription.setLayer(1); |
| 555 |
readbackDescription.setLevel(0); |
| 556 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 557 |
|
| 558 |
readbackResult = new QRhiReadbackResult; |
| 559 |
readbackResult->completed = |
| 560 |
std::bind(readBackCompleted, readbackResult, textureArrayCopyData[1]); |
| 561 |
readbackDescription.setTexture(texture); |
| 562 |
readbackDescription.setLayer(0); |
| 563 |
readbackDescription.setLevel(0); |
| 564 |
d.initialUpdates->readBackTexture(readbackDescription, readbackResult); |
| 565 |
|
| 566 |
// |
| 567 |
// Shader resource bindings |
| 568 |
// |
| 569 |
d.srb = m_r->newShaderResourceBindings(); |
| 570 |
d.releasePool << d.srb; |
| 571 |
d.srb->setBindings(shaderResouceBindings.cbegin(), shaderResouceBindings.cend()); |
| 572 |
d.srb->create(); |
| 573 |
|
| 574 |
// |
| 575 |
// Pipeline |
| 576 |
// |
| 577 |
d.ps = m_r->newGraphicsPipeline(); |
| 578 |
d.releasePool << d.ps; |
| 579 |
d.ps->setShaderStages( |
| 580 |
{ { QRhiShaderStage::Vertex, getShader(QLatin1String(":/texture1d.vert.qsb")) }, |
| 581 |
{ QRhiShaderStage::Fragment, getShader(QLatin1String(":/texture1d.frag.qsb")) } }); |
| 582 |
|
| 583 |
QRhiVertexInputLayout inputLayout; |
| 584 |
inputLayout.setBindings({ { 4 * sizeof(float) } }); |
| 585 |
inputLayout.setAttributes( |
| 586 |
{ { 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 587 |
{ 0, 1, QRhiVertexInputAttribute::Float2, quint32(2 * sizeof(float)) } }); |
| 588 |
|
| 589 |
d.ps->setVertexInputLayout(inputLayout); |
| 590 |
d.ps->setShaderResourceBindings(d.srb); |
| 591 |
d.ps->setRenderPassDescriptor(m_rp); |
| 592 |
d.ps->create(); |
| 593 |
} |
| 594 |
|
| 595 |
void Window::customRelease() |
| 596 |
{ |
| 597 |
qDeleteAll(d.releasePool); |
| 598 |
d.releasePool.clear(); |
| 599 |
} |
| 600 |
|
| 601 |
void Window::customRender() |
| 602 |
{ |
| 603 |
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer(); |
| 604 |
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch(); |
| 605 |
|
| 606 |
if (d.initialUpdates) { |
| 607 |
u->merge(d.initialUpdates); |
| 608 |
d.initialUpdates->release(); |
| 609 |
d.initialUpdates = nullptr; |
| 610 |
} |
| 611 |
|
| 612 |
u->updateDynamicBuffer(d.uniformBuffer, 0, 4, &d.index); |
| 613 |
d.index++; |
| 614 |
|
| 615 |
cb->beginPass(m_sc->currentFrameRenderTarget(), m_clearColor, { 1.0f, 0 }, u); |
| 616 |
cb->setGraphicsPipeline(d.ps); |
| 617 |
cb->setShaderResources(d.srb); |
| 618 |
const QSize outputSizeInPixels = m_sc->currentPixelSize(); |
| 619 |
cb->setViewport( |
| 620 |
{ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) }); |
| 621 |
QRhiCommandBuffer::VertexInput vbufBinding(d.vertexBuffer, 0); |
| 622 |
cb->setVertexInput(0, 1, &vbufBinding, d.indexBuffer, 0, QRhiCommandBuffer::IndexUInt16); |
| 623 |
cb->drawIndexed(6); |
| 624 |
cb->endPass(); |
| 625 |
} |