Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // FramebufferVk.cpp: |
| 7 | // Implements the class methods for FramebufferVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
| 11 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 12 | #include <array> |
| 13 | #include <vulkan/vulkan.h> |
| 14 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 15 | #include "common/debug.h" |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 16 | #include "image_util/imageformats.h" |
| 17 | #include "libANGLE/formatutils.h" |
| 18 | #include "libANGLE/renderer/renderer_utils.h" |
| 19 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 20 | #include "libANGLE/renderer/vulkan/DisplayVk.h" |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 21 | #include "libANGLE/renderer/vulkan/RenderTargetVk.h" |
| 22 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
| 23 | #include "libANGLE/renderer/vulkan/SurfaceVk.h" |
| 24 | #include "libANGLE/renderer/vulkan/formatutilsvk.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 25 | |
| 26 | namespace rx |
| 27 | { |
| 28 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 29 | namespace |
| 30 | { |
| 31 | |
| 32 | gl::ErrorOrResult<const gl::InternalFormat *> GetReadAttachmentInfo( |
| 33 | const gl::FramebufferAttachment *readAttachment) |
| 34 | { |
| 35 | RenderTargetVk *renderTarget = nullptr; |
| 36 | ANGLE_TRY(readAttachment->getRenderTarget(&renderTarget)); |
| 37 | |
| 38 | GLenum implFormat = renderTarget->format->format().fboImplementationInternalFormat; |
| 39 | return &gl::GetInternalFormatInfo(implFormat); |
| 40 | } |
| 41 | |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 42 | VkSampleCountFlagBits ConvertSamples(GLint sampleCount) |
| 43 | { |
| 44 | switch (sampleCount) |
| 45 | { |
| 46 | case 0: |
| 47 | case 1: |
| 48 | return VK_SAMPLE_COUNT_1_BIT; |
| 49 | case 2: |
| 50 | return VK_SAMPLE_COUNT_2_BIT; |
| 51 | case 4: |
| 52 | return VK_SAMPLE_COUNT_4_BIT; |
| 53 | case 8: |
| 54 | return VK_SAMPLE_COUNT_8_BIT; |
| 55 | case 16: |
| 56 | return VK_SAMPLE_COUNT_16_BIT; |
| 57 | case 32: |
| 58 | return VK_SAMPLE_COUNT_32_BIT; |
| 59 | default: |
| 60 | UNREACHABLE(); |
| 61 | return VK_SAMPLE_COUNT_FLAG_BITS_MAX_ENUM; |
| 62 | } |
| 63 | } |
| 64 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 65 | } // anonymous namespace |
| 66 | |
| 67 | // static |
| 68 | FramebufferVk *FramebufferVk::CreateUserFBO(const gl::FramebufferState &state) |
| 69 | { |
| 70 | return new FramebufferVk(state); |
| 71 | } |
| 72 | |
| 73 | // static |
| 74 | FramebufferVk *FramebufferVk::CreateDefaultFBO(const gl::FramebufferState &state, |
| 75 | WindowSurfaceVk *backbuffer) |
| 76 | { |
| 77 | return new FramebufferVk(state, backbuffer); |
| 78 | } |
| 79 | |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 80 | FramebufferVk::FramebufferVk(const gl::FramebufferState &state) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 81 | : FramebufferImpl(state), mBackbuffer(nullptr), mRenderPass(), mFramebuffer() |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 82 | { |
| 83 | } |
| 84 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 85 | FramebufferVk::FramebufferVk(const gl::FramebufferState &state, WindowSurfaceVk *backbuffer) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 86 | : FramebufferImpl(state), mBackbuffer(backbuffer), mRenderPass(), mFramebuffer() |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 87 | { |
| 88 | } |
| 89 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 90 | FramebufferVk::~FramebufferVk() |
| 91 | { |
| 92 | } |
| 93 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 94 | void FramebufferVk::destroy(ContextImpl *contextImpl) |
| 95 | { |
| 96 | VkDevice device = GetAs<ContextVk>(contextImpl)->getDevice(); |
| 97 | |
| 98 | mRenderPass.destroy(device); |
| 99 | mFramebuffer.destroy(device); |
| 100 | } |
| 101 | |
| 102 | void FramebufferVk::destroyDefault(DisplayImpl *displayImpl) |
| 103 | { |
| 104 | VkDevice device = GetAs<DisplayVk>(displayImpl)->getRenderer()->getDevice(); |
| 105 | |
| 106 | mRenderPass.destroy(device); |
| 107 | mFramebuffer.destroy(device); |
| 108 | } |
| 109 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 110 | gl::Error FramebufferVk::discard(size_t count, const GLenum *attachments) |
| 111 | { |
| 112 | UNIMPLEMENTED(); |
| 113 | return gl::Error(GL_INVALID_OPERATION); |
| 114 | } |
| 115 | |
| 116 | gl::Error FramebufferVk::invalidate(size_t count, const GLenum *attachments) |
| 117 | { |
| 118 | UNIMPLEMENTED(); |
| 119 | return gl::Error(GL_INVALID_OPERATION); |
| 120 | } |
| 121 | |
| 122 | gl::Error FramebufferVk::invalidateSub(size_t count, |
| 123 | const GLenum *attachments, |
| 124 | const gl::Rectangle &area) |
| 125 | { |
| 126 | UNIMPLEMENTED(); |
| 127 | return gl::Error(GL_INVALID_OPERATION); |
| 128 | } |
| 129 | |
| 130 | gl::Error FramebufferVk::clear(ContextImpl *context, GLbitfield mask) |
| 131 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 132 | ContextVk *contextVk = GetAs<ContextVk>(context); |
| 133 | |
| 134 | if (mState.getDepthAttachment() && (mask & GL_DEPTH_BUFFER_BIT) != 0) |
| 135 | { |
| 136 | // TODO(jmadill): Depth clear |
| 137 | UNIMPLEMENTED(); |
| 138 | } |
| 139 | |
| 140 | if (mState.getStencilAttachment() && (mask & GL_STENCIL_BUFFER_BIT) != 0) |
| 141 | { |
| 142 | // TODO(jmadill): Stencil clear |
| 143 | UNIMPLEMENTED(); |
| 144 | } |
| 145 | |
| 146 | if ((mask & GL_COLOR_BUFFER_BIT) == 0) |
| 147 | { |
| 148 | return gl::NoError(); |
| 149 | } |
| 150 | |
| 151 | const auto &glState = context->getGLState(); |
| 152 | const auto &clearColor = glState.getColorClearValue(); |
| 153 | VkClearColorValue clearColorValue; |
| 154 | clearColorValue.float32[0] = clearColor.red; |
| 155 | clearColorValue.float32[1] = clearColor.green; |
| 156 | clearColorValue.float32[2] = clearColor.blue; |
| 157 | clearColorValue.float32[3] = clearColor.alpha; |
| 158 | |
| 159 | // TODO(jmadill): Scissored clears. |
| 160 | const auto *attachment = mState.getFirstNonNullAttachment(); |
| 161 | ASSERT(attachment && attachment->isAttached()); |
| 162 | const auto &size = attachment->getSize(); |
| 163 | const gl::Rectangle renderArea(0, 0, size.width, size.height); |
| 164 | |
| 165 | vk::CommandBuffer *commandBuffer = contextVk->getCommandBuffer(); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 166 | ANGLE_TRY(commandBuffer->begin(contextVk->getDevice())); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 167 | |
| 168 | for (const auto &colorAttachment : mState.getColorAttachments()) |
| 169 | { |
| 170 | if (colorAttachment.isAttached()) |
| 171 | { |
| 172 | RenderTargetVk *renderTarget = nullptr; |
| 173 | ANGLE_TRY(colorAttachment.getRenderTarget(&renderTarget)); |
| 174 | renderTarget->image->changeLayoutTop( |
| 175 | VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, commandBuffer); |
| 176 | commandBuffer->clearSingleColorImage(*renderTarget->image, clearColorValue); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | commandBuffer->end(); |
| 181 | |
| 182 | ANGLE_TRY(contextVk->submitCommands(*commandBuffer)); |
| 183 | |
| 184 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | gl::Error FramebufferVk::clearBufferfv(ContextImpl *context, |
| 188 | GLenum buffer, |
| 189 | GLint drawbuffer, |
| 190 | const GLfloat *values) |
| 191 | { |
| 192 | UNIMPLEMENTED(); |
| 193 | return gl::Error(GL_INVALID_OPERATION); |
| 194 | } |
| 195 | |
| 196 | gl::Error FramebufferVk::clearBufferuiv(ContextImpl *context, |
| 197 | GLenum buffer, |
| 198 | GLint drawbuffer, |
| 199 | const GLuint *values) |
| 200 | { |
| 201 | UNIMPLEMENTED(); |
| 202 | return gl::Error(GL_INVALID_OPERATION); |
| 203 | } |
| 204 | |
| 205 | gl::Error FramebufferVk::clearBufferiv(ContextImpl *context, |
| 206 | GLenum buffer, |
| 207 | GLint drawbuffer, |
| 208 | const GLint *values) |
| 209 | { |
| 210 | UNIMPLEMENTED(); |
| 211 | return gl::Error(GL_INVALID_OPERATION); |
| 212 | } |
| 213 | |
| 214 | gl::Error FramebufferVk::clearBufferfi(ContextImpl *context, |
| 215 | GLenum buffer, |
| 216 | GLint drawbuffer, |
| 217 | GLfloat depth, |
| 218 | GLint stencil) |
| 219 | { |
| 220 | UNIMPLEMENTED(); |
| 221 | return gl::Error(GL_INVALID_OPERATION); |
| 222 | } |
| 223 | |
| 224 | GLenum FramebufferVk::getImplementationColorReadFormat() const |
| 225 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 226 | auto errOrResult = GetReadAttachmentInfo(mState.getReadAttachment()); |
| 227 | |
| 228 | // TODO(jmadill): Handle getRenderTarget error. |
| 229 | if (errOrResult.isError()) |
| 230 | { |
Yuly Novikov | d73f852 | 2017-01-13 17:48:57 -0500 | [diff] [blame] | 231 | ERR() << "Internal error in FramebufferVk::getImplementationColorReadFormat."; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 232 | return GL_NONE; |
| 233 | } |
| 234 | |
| 235 | return errOrResult.getResult()->format; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | GLenum FramebufferVk::getImplementationColorReadType() const |
| 239 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 240 | auto errOrResult = GetReadAttachmentInfo(mState.getReadAttachment()); |
| 241 | |
| 242 | // TODO(jmadill): Handle getRenderTarget error. |
| 243 | if (errOrResult.isError()) |
| 244 | { |
Yuly Novikov | d73f852 | 2017-01-13 17:48:57 -0500 | [diff] [blame] | 245 | ERR() << "Internal error in FramebufferVk::getImplementationColorReadFormat."; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 246 | return GL_NONE; |
| 247 | } |
| 248 | |
| 249 | return errOrResult.getResult()->type; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | gl::Error FramebufferVk::readPixels(ContextImpl *context, |
| 253 | const gl::Rectangle &area, |
| 254 | GLenum format, |
| 255 | GLenum type, |
| 256 | GLvoid *pixels) const |
| 257 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 258 | const auto &glState = context->getGLState(); |
| 259 | const auto *readFramebuffer = glState.getReadFramebuffer(); |
| 260 | const auto *readAttachment = readFramebuffer->getReadColorbuffer(); |
| 261 | |
| 262 | RenderTargetVk *renderTarget = nullptr; |
| 263 | ANGLE_TRY(readAttachment->getRenderTarget(&renderTarget)); |
| 264 | |
| 265 | ContextVk *contextVk = GetAs<ContextVk>(context); |
| 266 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 267 | VkDevice device = renderer->getDevice(); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 268 | |
| 269 | vk::Image *readImage = renderTarget->image; |
| 270 | vk::StagingImage stagingImage; |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 271 | ANGLE_TRY(renderer->createStagingImage(TextureDimension::TEX_2D, *renderTarget->format, |
| 272 | renderTarget->extents, &stagingImage)); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 273 | |
| 274 | vk::CommandBuffer *commandBuffer = contextVk->getCommandBuffer(); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 275 | commandBuffer->begin(device); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 276 | stagingImage.getImage().changeLayoutTop(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_GENERAL, |
| 277 | commandBuffer); |
| 278 | |
| 279 | gl::Box copyRegion; |
| 280 | copyRegion.x = area.x; |
| 281 | copyRegion.y = area.y; |
| 282 | copyRegion.z = 0; |
| 283 | copyRegion.width = area.width; |
| 284 | copyRegion.height = area.height; |
| 285 | copyRegion.depth = 1; |
| 286 | |
| 287 | readImage->changeLayoutTop(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 288 | commandBuffer); |
| 289 | commandBuffer->copySingleImage(*readImage, stagingImage.getImage(), copyRegion, |
| 290 | VK_IMAGE_ASPECT_COLOR_BIT); |
| 291 | commandBuffer->end(); |
| 292 | |
| 293 | ANGLE_TRY(renderer->submitAndFinishCommandBuffer(*commandBuffer)); |
| 294 | |
| 295 | // TODO(jmadill): parameters |
| 296 | uint8_t *mapPointer = nullptr; |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 297 | ANGLE_TRY( |
| 298 | stagingImage.getDeviceMemory().map(device, 0, stagingImage.getSize(), 0, &mapPointer)); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 299 | |
| 300 | const auto &angleFormat = renderTarget->format->format(); |
| 301 | |
| 302 | // TODO(jmadill): Use pixel bytes from the ANGLE format directly. |
| 303 | const auto &glFormat = gl::GetInternalFormatInfo(angleFormat.glInternalFormat); |
| 304 | int inputPitch = glFormat.pixelBytes * area.width; |
| 305 | |
| 306 | PackPixelsParams params; |
| 307 | params.area = area; |
| 308 | params.format = format; |
| 309 | params.type = type; |
| 310 | params.outputPitch = inputPitch; |
| 311 | params.pack = glState.getPackState(); |
| 312 | |
| 313 | PackPixels(params, angleFormat, inputPitch, mapPointer, reinterpret_cast<uint8_t *>(pixels)); |
| 314 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 315 | stagingImage.getDeviceMemory().unmap(device); |
Jamie Madill | f651c77 | 2017-02-21 15:03:51 -0500 | [diff] [blame] | 316 | renderer->enqueueGarbage(renderer->getCurrentQueueSerial(), std::move(stagingImage)); |
| 317 | |
| 318 | stagingImage.getImage().destroy(renderer->getDevice()); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 319 | |
| 320 | stagingImage.destroy(device); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 321 | |
| 322 | return vk::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | gl::Error FramebufferVk::blit(ContextImpl *context, |
| 326 | const gl::Rectangle &sourceArea, |
| 327 | const gl::Rectangle &destArea, |
| 328 | GLbitfield mask, |
| 329 | GLenum filter) |
| 330 | { |
| 331 | UNIMPLEMENTED(); |
| 332 | return gl::Error(GL_INVALID_OPERATION); |
| 333 | } |
| 334 | |
| 335 | bool FramebufferVk::checkStatus() const |
| 336 | { |
| 337 | UNIMPLEMENTED(); |
| 338 | return bool(); |
| 339 | } |
| 340 | |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 341 | void FramebufferVk::syncState(ContextImpl *contextImpl, const gl::Framebuffer::DirtyBits &dirtyBits) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 342 | { |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 343 | auto contextVk = GetAs<ContextVk>(contextImpl); |
| 344 | |
| 345 | ASSERT(dirtyBits.any()); |
| 346 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 347 | // TODO(jmadill): Smarter update. |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 348 | mRenderPass.destroy(contextVk->getDevice()); |
| 349 | mFramebuffer.destroy(contextVk->getDevice()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | gl::ErrorOrResult<vk::RenderPass *> FramebufferVk::getRenderPass(VkDevice device) |
| 353 | { |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 354 | if (mRenderPass.valid()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 355 | { |
| 356 | return &mRenderPass; |
| 357 | } |
| 358 | |
| 359 | // TODO(jmadill): Can we use stack-only memory? |
| 360 | std::vector<VkAttachmentDescription> attachmentDescs; |
| 361 | std::vector<VkAttachmentReference> colorAttachmentRefs; |
| 362 | |
| 363 | const auto &colorAttachments = mState.getColorAttachments(); |
| 364 | for (size_t attachmentIndex = 0; attachmentIndex < colorAttachments.size(); ++attachmentIndex) |
| 365 | { |
| 366 | const auto &colorAttachment = colorAttachments[attachmentIndex]; |
| 367 | if (colorAttachment.isAttached()) |
| 368 | { |
| 369 | VkAttachmentDescription colorDesc; |
| 370 | VkAttachmentReference colorRef; |
| 371 | |
| 372 | RenderTargetVk *renderTarget = nullptr; |
| 373 | ANGLE_TRY(colorAttachment.getRenderTarget(&renderTarget)); |
| 374 | |
| 375 | // TODO(jmadill): We would only need this flag for duplicated attachments. |
| 376 | colorDesc.flags = VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT; |
| 377 | colorDesc.format = renderTarget->format->native; |
| 378 | colorDesc.samples = ConvertSamples(colorAttachment.getSamples()); |
| 379 | |
| 380 | // The load op controls the prior existing depth/color attachment data. |
| 381 | // TODO(jmadill): Proper load ops. Should not be hard coded to clear. |
| 382 | colorDesc.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 383 | colorDesc.storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
| 384 | colorDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 385 | colorDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 386 | colorDesc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 387 | |
| 388 | // We might want to transition directly to PRESENT_SRC for Surface attachments. |
| 389 | colorDesc.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 390 | |
| 391 | colorRef.attachment = static_cast<uint32_t>(colorAttachments.size()) - 1u; |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 392 | colorRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 393 | |
| 394 | attachmentDescs.push_back(colorDesc); |
| 395 | colorAttachmentRefs.push_back(colorRef); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | const auto *depthStencilAttachment = mState.getDepthStencilAttachment(); |
| 400 | VkAttachmentReference depthStencilAttachmentRef; |
| 401 | bool useDepth = depthStencilAttachment && depthStencilAttachment->isAttached(); |
| 402 | |
| 403 | if (useDepth) |
| 404 | { |
| 405 | VkAttachmentDescription depthStencilDesc; |
| 406 | |
| 407 | RenderTargetVk *renderTarget = nullptr; |
| 408 | ANGLE_TRY(depthStencilAttachment->getRenderTarget(&renderTarget)); |
| 409 | |
| 410 | depthStencilDesc.flags = 0; |
| 411 | depthStencilDesc.format = renderTarget->format->native; |
| 412 | depthStencilDesc.samples = ConvertSamples(depthStencilAttachment->getSamples()); |
| 413 | depthStencilDesc.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
| 414 | depthStencilDesc.storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
| 415 | depthStencilDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 416 | depthStencilDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 417 | depthStencilDesc.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 418 | depthStencilDesc.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 419 | |
| 420 | depthStencilAttachmentRef.attachment = static_cast<uint32_t>(attachmentDescs.size()); |
| 421 | depthStencilAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
| 422 | |
| 423 | attachmentDescs.push_back(depthStencilDesc); |
| 424 | } |
| 425 | |
| 426 | ASSERT(!attachmentDescs.empty()); |
| 427 | |
| 428 | VkSubpassDescription subpassDesc; |
| 429 | |
| 430 | subpassDesc.flags = 0; |
| 431 | subpassDesc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; |
| 432 | subpassDesc.inputAttachmentCount = 0; |
| 433 | subpassDesc.pInputAttachments = nullptr; |
| 434 | subpassDesc.colorAttachmentCount = static_cast<uint32_t>(colorAttachmentRefs.size()); |
| 435 | subpassDesc.pColorAttachments = colorAttachmentRefs.data(); |
| 436 | subpassDesc.pResolveAttachments = nullptr; |
| 437 | subpassDesc.pDepthStencilAttachment = (useDepth ? &depthStencilAttachmentRef : nullptr); |
| 438 | subpassDesc.preserveAttachmentCount = 0; |
| 439 | subpassDesc.pPreserveAttachments = nullptr; |
| 440 | |
| 441 | VkRenderPassCreateInfo renderPassInfo; |
| 442 | |
| 443 | renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; |
| 444 | renderPassInfo.pNext = nullptr; |
| 445 | renderPassInfo.flags = 0; |
| 446 | renderPassInfo.attachmentCount = static_cast<uint32_t>(attachmentDescs.size()); |
| 447 | renderPassInfo.pAttachments = attachmentDescs.data(); |
| 448 | renderPassInfo.subpassCount = 1; |
| 449 | renderPassInfo.pSubpasses = &subpassDesc; |
| 450 | renderPassInfo.dependencyCount = 0; |
| 451 | renderPassInfo.pDependencies = nullptr; |
| 452 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 453 | vk::RenderPass renderPass; |
| 454 | ANGLE_TRY(renderPass.init(device, renderPassInfo)); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 455 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 456 | mRenderPass.retain(device, std::move(renderPass)); |
| 457 | |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 458 | return &mRenderPass; |
| 459 | } |
| 460 | |
| 461 | gl::ErrorOrResult<vk::Framebuffer *> FramebufferVk::getFramebuffer(VkDevice device) |
| 462 | { |
| 463 | // If we've already created our cached Framebuffer, return it. |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 464 | if (mFramebuffer.valid()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 465 | { |
| 466 | return &mFramebuffer; |
| 467 | } |
| 468 | |
| 469 | vk::RenderPass *renderPass = nullptr; |
| 470 | ANGLE_TRY_RESULT(getRenderPass(device), renderPass); |
| 471 | |
| 472 | // If we've a Framebuffer provided by a Surface (default FBO/backbuffer), query it. |
| 473 | if (mBackbuffer) |
| 474 | { |
| 475 | return mBackbuffer->getCurrentFramebuffer(device, *renderPass); |
| 476 | } |
| 477 | |
| 478 | // Gather VkImageViews over all FBO attachments, also size of attached region. |
| 479 | std::vector<VkImageView> attachments; |
| 480 | gl::Extents attachmentsSize; |
| 481 | |
| 482 | const auto &colorAttachments = mState.getColorAttachments(); |
| 483 | for (size_t attachmentIndex = 0; attachmentIndex < colorAttachments.size(); ++attachmentIndex) |
| 484 | { |
| 485 | const auto &colorAttachment = colorAttachments[attachmentIndex]; |
| 486 | if (colorAttachment.isAttached()) |
| 487 | { |
| 488 | RenderTargetVk *renderTarget = nullptr; |
| 489 | ANGLE_TRY(colorAttachment.getRenderTarget<RenderTargetVk>(&renderTarget)); |
| 490 | attachments.push_back(renderTarget->imageView->getHandle()); |
| 491 | |
| 492 | ASSERT(attachmentsSize.empty() || attachmentsSize == colorAttachment.getSize()); |
| 493 | attachmentsSize = colorAttachment.getSize(); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | const auto *depthStencilAttachment = mState.getDepthStencilAttachment(); |
| 498 | if (depthStencilAttachment && depthStencilAttachment->isAttached()) |
| 499 | { |
| 500 | RenderTargetVk *renderTarget = nullptr; |
| 501 | ANGLE_TRY(depthStencilAttachment->getRenderTarget<RenderTargetVk>(&renderTarget)); |
| 502 | attachments.push_back(renderTarget->imageView->getHandle()); |
| 503 | |
| 504 | ASSERT(attachmentsSize.empty() || attachmentsSize == depthStencilAttachment->getSize()); |
| 505 | attachmentsSize = depthStencilAttachment->getSize(); |
| 506 | } |
| 507 | |
| 508 | ASSERT(!attachments.empty()); |
| 509 | |
| 510 | VkFramebufferCreateInfo framebufferInfo; |
| 511 | |
| 512 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
| 513 | framebufferInfo.pNext = nullptr; |
| 514 | framebufferInfo.flags = 0; |
| 515 | framebufferInfo.renderPass = mRenderPass.getHandle(); |
| 516 | framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); |
| 517 | framebufferInfo.pAttachments = attachments.data(); |
| 518 | framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width); |
| 519 | framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height); |
| 520 | framebufferInfo.layers = 1; |
| 521 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 522 | vk::Framebuffer framebuffer; |
| 523 | ANGLE_TRY(static_cast<gl::Error>(framebuffer.init(device, framebufferInfo))); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 524 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 525 | mFramebuffer.retain(device, std::move(framebuffer)); |
| 526 | |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 527 | return &mFramebuffer; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 528 | } |
| 529 | |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 530 | gl::Error FramebufferVk::getSamplePosition(size_t index, GLfloat *xy) const |
| 531 | { |
| 532 | UNIMPLEMENTED(); |
| 533 | return gl::InternalError() << "getSamplePosition is unimplemented."; |
| 534 | } |
| 535 | |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 536 | gl::Error FramebufferVk::beginRenderPass(VkDevice device, |
| 537 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 538 | Serial queueSerial, |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 539 | const gl::State &glState) |
| 540 | { |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 541 | // TODO(jmadill): Cache render targets. |
| 542 | for (const auto &colorAttachment : mState.getColorAttachments()) |
| 543 | { |
| 544 | if (colorAttachment.isAttached()) |
| 545 | { |
| 546 | RenderTargetVk *renderTarget = nullptr; |
| 547 | ANGLE_TRY(colorAttachment.getRenderTarget<RenderTargetVk>(&renderTarget)); |
| 548 | renderTarget->resource->setQueueSerial(queueSerial); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | const auto *depthStencilAttachment = mState.getDepthStencilAttachment(); |
| 553 | if (depthStencilAttachment && depthStencilAttachment->isAttached()) |
| 554 | { |
| 555 | RenderTargetVk *renderTarget = nullptr; |
| 556 | ANGLE_TRY(depthStencilAttachment->getRenderTarget<RenderTargetVk>(&renderTarget)); |
| 557 | renderTarget->resource->setQueueSerial(queueSerial); |
| 558 | } |
| 559 | |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 560 | vk::Framebuffer *framebuffer = nullptr; |
| 561 | ANGLE_TRY_RESULT(getFramebuffer(device), framebuffer); |
| 562 | ASSERT(framebuffer && framebuffer->valid()); |
| 563 | |
| 564 | vk::RenderPass *renderPass = nullptr; |
| 565 | ANGLE_TRY_RESULT(getRenderPass(device), renderPass); |
| 566 | ASSERT(renderPass && renderPass->valid()); |
| 567 | |
| 568 | // TODO(jmadill): Proper clear value implementation. |
| 569 | VkClearColorValue colorClear; |
| 570 | memset(&colorClear, 0, sizeof(VkClearColorValue)); |
| 571 | colorClear.float32[0] = glState.getColorClearValue().red; |
| 572 | colorClear.float32[1] = glState.getColorClearValue().green; |
| 573 | colorClear.float32[2] = glState.getColorClearValue().blue; |
| 574 | colorClear.float32[3] = glState.getColorClearValue().alpha; |
| 575 | |
| 576 | std::vector<VkClearValue> attachmentClearValues; |
| 577 | attachmentClearValues.push_back({colorClear}); |
| 578 | |
| 579 | // Updated the cached image layout of the attachments in this FBO. |
| 580 | // For a default FBO, we need to call through to the WindowSurfaceVk |
| 581 | // TODO(jmadill): Iterate over all attachments. |
| 582 | ASSERT(mBackbuffer); |
| 583 | RenderTargetVk *renderTarget = nullptr; |
| 584 | ANGLE_TRY(mState.getFirstColorAttachment()->getRenderTarget(&renderTarget)); |
| 585 | renderTarget->image->updateLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
| 586 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 587 | ANGLE_TRY(commandBuffer->begin(device)); |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 588 | commandBuffer->beginRenderPass(*renderPass, *framebuffer, glState.getViewport(), |
| 589 | attachmentClearValues); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 590 | |
| 591 | setQueueSerial(queueSerial); |
| 592 | if (mBackbuffer) |
| 593 | { |
| 594 | mBackbuffer->setQueueSerial(queueSerial); |
| 595 | } |
| 596 | |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 597 | return gl::NoError(); |
| 598 | } |
| 599 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 600 | } // namespace rx |