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