blob: e9d5173c4e718742d48c2e6ecf113fbafe54b5ee [file] [log] [blame]
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001//
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// TextureVk.cpp:
7// Implements the class methods for TextureVk.
8//
9
10#include "libANGLE/renderer/vulkan/TextureVk.h"
11
12#include "common/debug.h"
Luc Ferronc5181702018-05-17 09:44:42 -040013#include "image_util/generatemip.inl"
Jamie Madill035fd6b2017-10-03 15:43:22 -040014#include "libANGLE/Context.h"
15#include "libANGLE/renderer/vulkan/ContextVk.h"
Luc Ferron018709f2018-05-10 13:53:11 -040016#include "libANGLE/renderer/vulkan/FramebufferVk.h"
Jamie Madill035fd6b2017-10-03 15:43:22 -040017#include "libANGLE/renderer/vulkan/RendererVk.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050018#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040019
20namespace rx
21{
Luc Ferron5164b792018-03-06 09:10:12 -050022namespace
23{
Jamie Madill93edca12018-03-30 10:43:18 -040024void MapSwizzleState(GLenum internalFormat,
25 const gl::SwizzleState &swizzleState,
26 gl::SwizzleState *swizzleStateOut)
Luc Ferron5164b792018-03-06 09:10:12 -050027{
28 switch (internalFormat)
29 {
Jamie Madill26084d02018-04-09 13:44:04 -040030 case GL_LUMINANCE8_OES:
Jamie Madill93edca12018-03-30 10:43:18 -040031 swizzleStateOut->swizzleRed = swizzleState.swizzleRed;
32 swizzleStateOut->swizzleGreen = swizzleState.swizzleRed;
33 swizzleStateOut->swizzleBlue = swizzleState.swizzleRed;
34 swizzleStateOut->swizzleAlpha = GL_ONE;
Luc Ferron5164b792018-03-06 09:10:12 -050035 break;
Jamie Madill26084d02018-04-09 13:44:04 -040036 case GL_LUMINANCE8_ALPHA8_OES:
Jamie Madill93edca12018-03-30 10:43:18 -040037 swizzleStateOut->swizzleRed = swizzleState.swizzleRed;
38 swizzleStateOut->swizzleGreen = swizzleState.swizzleRed;
39 swizzleStateOut->swizzleBlue = swizzleState.swizzleRed;
40 swizzleStateOut->swizzleAlpha = swizzleState.swizzleGreen;
Luc Ferron5164b792018-03-06 09:10:12 -050041 break;
Jamie Madill26084d02018-04-09 13:44:04 -040042 case GL_ALPHA8_OES:
Jamie Madill93edca12018-03-30 10:43:18 -040043 swizzleStateOut->swizzleRed = GL_ZERO;
44 swizzleStateOut->swizzleGreen = GL_ZERO;
45 swizzleStateOut->swizzleBlue = GL_ZERO;
46 swizzleStateOut->swizzleAlpha = swizzleState.swizzleRed;
Luc Ferron49cef9a2018-03-21 17:28:53 -040047 break;
Luc Ferron5164b792018-03-06 09:10:12 -050048 default:
Jamie Madill93edca12018-03-30 10:43:18 -040049 *swizzleStateOut = swizzleState;
Luc Ferron5164b792018-03-06 09:10:12 -050050 break;
51 }
52}
Jamie Madill26084d02018-04-09 13:44:04 -040053
54constexpr VkBufferUsageFlags kStagingBufferFlags =
55 (VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
56constexpr size_t kStagingBufferSize = 1024 * 16;
Luc Ferron05cd6df2018-05-24 15:51:29 -040057
58constexpr VkFormatFeatureFlags kBlitFeatureFlags =
59 VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
Luc Ferron5164b792018-03-06 09:10:12 -050060} // anonymous namespace
Jamie Madill9e54b5a2016-05-25 12:57:39 -040061
Jamie Madill26084d02018-04-09 13:44:04 -040062// StagingStorage implementation.
Luc Ferrona9ab0f32018-05-17 17:03:55 -040063PixelBuffer::PixelBuffer(RendererVk *renderer)
64 : mStagingBuffer(kStagingBufferFlags, kStagingBufferSize)
Jamie Madill26084d02018-04-09 13:44:04 -040065{
Jamie Madill20fa8d52018-04-15 10:09:32 -040066 // vkCmdCopyBufferToImage must have an offset that is a multiple of 4.
67 // https://www.khronos.org/registry/vulkan/specs/1.0/man/html/VkBufferImageCopy.html
Luc Ferrona9ab0f32018-05-17 17:03:55 -040068 mStagingBuffer.init(4, renderer);
Jamie Madill26084d02018-04-09 13:44:04 -040069}
70
Jamie Madilla7be1f72018-04-13 15:16:26 -040071PixelBuffer::~PixelBuffer()
Jamie Madill26084d02018-04-09 13:44:04 -040072{
73}
74
Jamie Madilla7be1f72018-04-13 15:16:26 -040075void PixelBuffer::release(RendererVk *renderer)
Jamie Madill26084d02018-04-09 13:44:04 -040076{
77 mStagingBuffer.release(renderer);
78}
79
Luc Ferron2f3f4142018-05-30 08:27:19 -040080void PixelBuffer::removeStagedUpdates(const gl::ImageIndex &index)
81{
82 // Find any staged updates for this index and removes them from the pending list.
83 uint32_t levelIndex = static_cast<uint32_t>(index.getLevelIndex());
84 uint32_t layerIndex = static_cast<uint32_t>(index.getLayerIndex());
85 auto removeIfStatement = [levelIndex, layerIndex](SubresourceUpdate &update) {
86 return update.copyRegion.imageSubresource.mipLevel == levelIndex &&
87 update.copyRegion.imageSubresource.baseArrayLayer == layerIndex;
88 };
89 mSubresourceUpdates.erase(
90 std::remove_if(mSubresourceUpdates.begin(), mSubresourceUpdates.end(), removeIfStatement),
91 mSubresourceUpdates.end());
92}
93
Jamie Madill21061022018-07-12 23:56:30 -040094angle::Result PixelBuffer::stageSubresourceUpdate(ContextVk *contextVk,
95 const gl::ImageIndex &index,
96 const gl::Extents &extents,
97 const gl::Offset &offset,
98 const gl::InternalFormat &formatInfo,
99 const gl::PixelUnpackState &unpack,
100 GLenum type,
101 const uint8_t *pixels)
Jamie Madill26084d02018-04-09 13:44:04 -0400102{
103 GLuint inputRowPitch = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400104 ANGLE_VK_CHECK_MATH(contextVk, formatInfo.computeRowPitch(type, extents.width, unpack.alignment,
105 unpack.rowLength, &inputRowPitch));
Jamie Madill26084d02018-04-09 13:44:04 -0400106
107 GLuint inputDepthPitch = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400108 ANGLE_VK_CHECK_MATH(contextVk, formatInfo.computeDepthPitch(extents.height, unpack.imageHeight,
109 inputRowPitch, &inputDepthPitch));
Jamie Madill26084d02018-04-09 13:44:04 -0400110
111 // TODO(jmadill): skip images for 3D Textures.
112 bool applySkipImages = false;
113
114 GLuint inputSkipBytes = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400115 ANGLE_VK_CHECK_MATH(contextVk,
116 formatInfo.computeSkipBytes(type, inputRowPitch, inputDepthPitch, unpack,
117 applySkipImages, &inputSkipBytes));
Jamie Madill26084d02018-04-09 13:44:04 -0400118
119 RendererVk *renderer = contextVk->getRenderer();
120
121 const vk::Format &vkFormat = renderer->getFormat(formatInfo.sizedInternalFormat);
122 const angle::Format &storageFormat = vkFormat.textureFormat();
123
124 size_t outputRowPitch = storageFormat.pixelBytes * extents.width;
125 size_t outputDepthPitch = outputRowPitch * extents.height;
126
Jamie Madill20fa8d52018-04-15 10:09:32 -0400127 VkBuffer bufferHandle = VK_NULL_HANDLE;
128
Jamie Madill26084d02018-04-09 13:44:04 -0400129 uint8_t *stagingPointer = nullptr;
130 bool newBufferAllocated = false;
131 uint32_t stagingOffset = 0;
132 size_t allocationSize = outputDepthPitch * extents.depth;
Jamie Madill21061022018-07-12 23:56:30 -0400133 ANGLE_TRY(mStagingBuffer.allocate(contextVk, allocationSize, &stagingPointer, &bufferHandle,
Jamie Madilleebe2192018-07-11 09:01:18 -0400134 &stagingOffset, &newBufferAllocated));
Jamie Madill26084d02018-04-09 13:44:04 -0400135
136 const uint8_t *source = pixels + inputSkipBytes;
137
Frank Henigmand9618bf2018-06-24 19:57:31 -0400138 LoadImageFunctionInfo loadFunction = vkFormat.textureLoadFunctions(type);
Jamie Madill26084d02018-04-09 13:44:04 -0400139
140 loadFunction.loadFunction(extents.width, extents.height, extents.depth, source, inputRowPitch,
141 inputDepthPitch, stagingPointer, outputRowPitch, outputDepthPitch);
142
Jamie Madill20fa8d52018-04-15 10:09:32 -0400143 VkBufferImageCopy copy;
Jamie Madill26084d02018-04-09 13:44:04 -0400144
Jamie Madill20fa8d52018-04-15 10:09:32 -0400145 copy.bufferOffset = static_cast<VkDeviceSize>(stagingOffset);
146 copy.bufferRowLength = extents.width;
147 copy.bufferImageHeight = extents.height;
148 copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
149 copy.imageSubresource.mipLevel = index.getLevelIndex();
150 copy.imageSubresource.baseArrayLayer = index.hasLayer() ? index.getLayerIndex() : 0;
151 copy.imageSubresource.layerCount = index.getLayerCount();
152
Luc Ferron33e05ba2018-04-23 15:12:34 -0400153 gl_vk::GetOffset(offset, &copy.imageOffset);
Jamie Madill20fa8d52018-04-15 10:09:32 -0400154 gl_vk::GetExtent(extents, &copy.imageExtent);
155
156 mSubresourceUpdates.emplace_back(bufferHandle, copy);
Jamie Madill26084d02018-04-09 13:44:04 -0400157
Jamie Madill21061022018-07-12 23:56:30 -0400158 return angle::Result::Continue();
Jamie Madill26084d02018-04-09 13:44:04 -0400159}
160
Jamie Madill21061022018-07-12 23:56:30 -0400161angle::Result PixelBuffer::stageSubresourceUpdateFromFramebuffer(
162 const gl::Context *context,
163 const gl::ImageIndex &index,
164 const gl::Rectangle &sourceArea,
165 const gl::Offset &dstOffset,
166 const gl::Extents &dstExtent,
167 const gl::InternalFormat &formatInfo,
168 FramebufferVk *framebufferVk)
Luc Ferron2a849bf2018-05-10 13:19:11 -0400169{
Luc Ferronaa2126c2018-07-09 15:36:36 -0400170 ContextVk *contextVk = vk::GetImpl(context);
171
Luc Ferron2a849bf2018-05-10 13:19:11 -0400172 // If the extents and offset is outside the source image, we need to clip.
173 gl::Rectangle clippedRectangle;
Jamie Madill58675012018-05-22 14:54:07 -0400174 const gl::Extents readExtents = framebufferVk->getReadImageExtents();
175 if (!ClipRectangle(sourceArea, gl::Rectangle(0, 0, readExtents.width, readExtents.height),
Luc Ferron2a849bf2018-05-10 13:19:11 -0400176 &clippedRectangle))
177 {
178 // Empty source area, nothing to do.
Jamie Madill21061022018-07-12 23:56:30 -0400179 return angle::Result::Continue();
Luc Ferron2a849bf2018-05-10 13:19:11 -0400180 }
181
Luc Ferronaa2126c2018-07-09 15:36:36 -0400182 bool isViewportFlipEnabled = contextVk->isViewportFlipEnabledForDrawFBO();
183 if (isViewportFlipEnabled)
184 {
185 clippedRectangle.y = readExtents.height - clippedRectangle.y - clippedRectangle.height;
186 }
187
Luc Ferron2a849bf2018-05-10 13:19:11 -0400188 // 1- obtain a buffer handle to copy to
Jamie Madill21061022018-07-12 23:56:30 -0400189 RendererVk *renderer = contextVk->getRenderer();
Luc Ferron2a849bf2018-05-10 13:19:11 -0400190
191 const vk::Format &vkFormat = renderer->getFormat(formatInfo.sizedInternalFormat);
192 const angle::Format &storageFormat = vkFormat.textureFormat();
Frank Henigmand9618bf2018-06-24 19:57:31 -0400193 LoadImageFunctionInfo loadFunction = vkFormat.textureLoadFunctions(formatInfo.type);
Luc Ferron2a849bf2018-05-10 13:19:11 -0400194
195 size_t outputRowPitch = storageFormat.pixelBytes * clippedRectangle.width;
196 size_t outputDepthPitch = outputRowPitch * clippedRectangle.height;
197
198 VkBuffer bufferHandle = VK_NULL_HANDLE;
199
200 uint8_t *stagingPointer = nullptr;
201 bool newBufferAllocated = false;
202 uint32_t stagingOffset = 0;
Luc Ferron018709f2018-05-10 13:53:11 -0400203
204 // The destination is only one layer deep.
205 size_t allocationSize = outputDepthPitch;
Jamie Madill21061022018-07-12 23:56:30 -0400206 ANGLE_TRY(mStagingBuffer.allocate(contextVk, allocationSize, &stagingPointer, &bufferHandle,
Jamie Madilleebe2192018-07-11 09:01:18 -0400207 &stagingOffset, &newBufferAllocated));
Luc Ferron2a849bf2018-05-10 13:19:11 -0400208
Luc Ferronaa2126c2018-07-09 15:36:36 -0400209 gl::PixelPackState pixelPackState = gl::PixelPackState();
210 // TODO(lucferron): The pixel pack state alignment should probably be 1 instead of 4.
211 // http://anglebug.com/2718
212
213 if (isViewportFlipEnabled)
214 {
215 pixelPackState.reverseRowOrder = !pixelPackState.reverseRowOrder;
216 }
217
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400218 PackPixelsParams params;
Luc Ferronaa2126c2018-07-09 15:36:36 -0400219 params.area = clippedRectangle;
Luc Ferronc94ba1d2018-06-18 11:26:28 -0400220 params.format = formatInfo.format;
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400221 params.type = formatInfo.type;
222 params.outputPitch = static_cast<GLuint>(outputRowPitch);
223 params.packBuffer = nullptr;
Luc Ferronaa2126c2018-07-09 15:36:36 -0400224 params.pack = pixelPackState;
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400225
Luc Ferron018709f2018-05-10 13:53:11 -0400226 // 2- copy the source image region to the pixel buffer using a cpu readback
227 if (loadFunction.requiresConversion)
228 {
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400229 // When a conversion is required, we need to use the loadFunction to read from a temporary
230 // buffer instead so its an even slower path.
Luc Ferronaa2126c2018-07-09 15:36:36 -0400231 size_t bufferSize =
232 storageFormat.pixelBytes * clippedRectangle.width * clippedRectangle.height;
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400233 angle::MemoryBuffer *memoryBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400234 ANGLE_VK_CHECK_ALLOC(contextVk, context->getScratchBuffer(bufferSize, &memoryBuffer));
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400235
236 // Read into the scratch buffer
Luc Ferron1617e692018-07-11 11:08:19 -0400237 ANGLE_TRY(framebufferVk->readPixelsImpl(
Jamie Madill21061022018-07-12 23:56:30 -0400238 contextVk, clippedRectangle, params, VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferron1617e692018-07-11 11:08:19 -0400239 framebufferVk->getColorReadRenderTarget(), memoryBuffer->data()));
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400240
241 // Load from scratch buffer to our pixel buffer
Luc Ferronaa2126c2018-07-09 15:36:36 -0400242 loadFunction.loadFunction(clippedRectangle.width, clippedRectangle.height, 1,
243 memoryBuffer->data(), outputRowPitch, 0, stagingPointer,
244 outputRowPitch, 0);
Luc Ferron018709f2018-05-10 13:53:11 -0400245 }
246 else
247 {
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400248 // We read directly from the framebuffer into our pixel buffer.
Luc Ferron1617e692018-07-11 11:08:19 -0400249 ANGLE_TRY(framebufferVk->readPixelsImpl(
Jamie Madill21061022018-07-12 23:56:30 -0400250 contextVk, clippedRectangle, params, VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferron1617e692018-07-11 11:08:19 -0400251 framebufferVk->getColorReadRenderTarget(), stagingPointer));
Luc Ferron018709f2018-05-10 13:53:11 -0400252 }
Luc Ferron2a849bf2018-05-10 13:19:11 -0400253
Luc Ferron018709f2018-05-10 13:53:11 -0400254 // 3- enqueue the destination image subresource update
Luc Ferron2a849bf2018-05-10 13:19:11 -0400255 VkBufferImageCopy copyToImage;
256 copyToImage.bufferOffset = static_cast<VkDeviceSize>(stagingOffset);
Luc Ferron018709f2018-05-10 13:53:11 -0400257 copyToImage.bufferRowLength = 0; // Tightly packed data can be specified as 0.
Luc Ferron2a849bf2018-05-10 13:19:11 -0400258 copyToImage.bufferImageHeight = clippedRectangle.height;
259 copyToImage.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
260 copyToImage.imageSubresource.mipLevel = index.getLevelIndex();
261 copyToImage.imageSubresource.baseArrayLayer = index.hasLayer() ? index.getLayerIndex() : 0;
262 copyToImage.imageSubresource.layerCount = index.getLayerCount();
263 gl_vk::GetOffset(dstOffset, &copyToImage.imageOffset);
264 gl_vk::GetExtent(dstExtent, &copyToImage.imageExtent);
265
266 // 3- enqueue the destination image subresource update
267 mSubresourceUpdates.emplace_back(bufferHandle, copyToImage);
Jamie Madill21061022018-07-12 23:56:30 -0400268 return angle::Result::Continue();
Luc Ferron2a849bf2018-05-10 13:19:11 -0400269}
270
Jamie Madill21061022018-07-12 23:56:30 -0400271angle::Result PixelBuffer::allocate(ContextVk *contextVk,
272 size_t sizeInBytes,
273 uint8_t **ptrOut,
274 VkBuffer *handleOut,
275 uint32_t *offsetOut,
276 bool *newBufferAllocatedOut)
Luc Ferronc5181702018-05-17 09:44:42 -0400277{
Jamie Madill21061022018-07-12 23:56:30 -0400278 return mStagingBuffer.allocate(contextVk, sizeInBytes, ptrOut, handleOut, offsetOut,
Luc Ferronc5181702018-05-17 09:44:42 -0400279 newBufferAllocatedOut);
280}
281
Jamie Madill21061022018-07-12 23:56:30 -0400282angle::Result PixelBuffer::flushUpdatesToImage(ContextVk *contextVk,
283 uint32_t levelCount,
284 vk::ImageHelper *image,
285 vk::CommandBuffer *commandBuffer)
Jamie Madill26084d02018-04-09 13:44:04 -0400286{
Jamie Madill20fa8d52018-04-15 10:09:32 -0400287 if (mSubresourceUpdates.empty())
Jamie Madill26084d02018-04-09 13:44:04 -0400288 {
Jamie Madill21061022018-07-12 23:56:30 -0400289 return angle::Result::Continue();
Jamie Madill26084d02018-04-09 13:44:04 -0400290 }
291
Jamie Madill21061022018-07-12 23:56:30 -0400292 ANGLE_TRY(mStagingBuffer.flush(contextVk));
Jamie Madill20fa8d52018-04-15 10:09:32 -0400293
Luc Ferron2f3f4142018-05-30 08:27:19 -0400294 std::vector<SubresourceUpdate> updatesToKeep;
295
Jamie Madill20fa8d52018-04-15 10:09:32 -0400296 for (const SubresourceUpdate &update : mSubresourceUpdates)
297 {
298 ASSERT(update.bufferHandle != VK_NULL_HANDLE);
Luc Ferron1a186b12018-04-24 15:25:35 -0400299
Luc Ferron2f3f4142018-05-30 08:27:19 -0400300 const uint32_t updateMipLevel = update.copyRegion.imageSubresource.mipLevel;
301 // It's possible we've accumulated updates that are no longer applicable if the image has
302 // never been flushed but the image description has changed. Check if this level exist for
303 // this image.
304 if (updateMipLevel >= levelCount)
305 {
306 updatesToKeep.emplace_back(update);
307 continue;
308 }
309
Luc Ferron1a186b12018-04-24 15:25:35 -0400310 // Conservatively flush all writes to the image. We could use a more restricted barrier.
311 // Do not move this above the for loop, otherwise multiple updates can have race conditions
312 // and not be applied correctly as seen i:
313 // dEQP-gles2.functional_texture_specification_texsubimage2d_align_2d* tests on Windows AMD
314 image->changeLayoutWithStages(
315 VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
316 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, commandBuffer);
317
Jamie Madill20fa8d52018-04-15 10:09:32 -0400318 commandBuffer->copyBufferToImage(update.bufferHandle, image->getImage(),
319 image->getCurrentLayout(), 1, &update.copyRegion);
320 }
321
Luc Ferron2f3f4142018-05-30 08:27:19 -0400322 // Only remove the updates that were actually applied to the image.
323 mSubresourceUpdates = std::move(updatesToKeep);
324
325 if (mSubresourceUpdates.empty())
326 {
Jamie Madill21061022018-07-12 23:56:30 -0400327 mStagingBuffer.releaseRetainedBuffers(contextVk->getRenderer());
Luc Ferron2f3f4142018-05-30 08:27:19 -0400328 }
329 else
330 {
331 WARN() << "Internal Vulkan bufffer could not be released. This is likely due to having "
332 "extra images defined in the Texture.";
333 }
Jamie Madill20fa8d52018-04-15 10:09:32 -0400334
Jamie Madill21061022018-07-12 23:56:30 -0400335 return angle::Result::Continue();
Jamie Madill26084d02018-04-09 13:44:04 -0400336}
337
Luc Ferron10434f62018-04-24 10:06:37 -0400338bool PixelBuffer::empty() const
339{
340 return mSubresourceUpdates.empty();
341}
342
Jamie Madill21061022018-07-12 23:56:30 -0400343angle::Result PixelBuffer::stageSubresourceUpdateAndGetData(ContextVk *contextVk,
344 size_t allocationSize,
345 const gl::ImageIndex &imageIndex,
346 const gl::Extents &extents,
347 const gl::Offset &offset,
348 uint8_t **destData)
Luc Ferronc5181702018-05-17 09:44:42 -0400349{
350 VkBuffer bufferHandle;
351 uint32_t stagingOffset = 0;
352 bool newBufferAllocated = false;
Jamie Madill21061022018-07-12 23:56:30 -0400353 ANGLE_TRY(mStagingBuffer.allocate(contextVk, allocationSize, destData, &bufferHandle,
Luc Ferronc5181702018-05-17 09:44:42 -0400354 &stagingOffset, &newBufferAllocated));
355
356 VkBufferImageCopy copy;
357 copy.bufferOffset = static_cast<VkDeviceSize>(stagingOffset);
358 copy.bufferRowLength = extents.width;
359 copy.bufferImageHeight = extents.height;
360 copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
361 copy.imageSubresource.mipLevel = imageIndex.getLevelIndex();
362 copy.imageSubresource.baseArrayLayer = imageIndex.hasLayer() ? imageIndex.getLayerIndex() : 0;
363 copy.imageSubresource.layerCount = imageIndex.getLayerCount();
364
365 gl_vk::GetOffset(offset, &copy.imageOffset);
366 gl_vk::GetExtent(extents, &copy.imageExtent);
367
368 mSubresourceUpdates.emplace_back(bufferHandle, copy);
369
Jamie Madill21061022018-07-12 23:56:30 -0400370 return angle::Result::Continue();
Luc Ferronc5181702018-05-17 09:44:42 -0400371}
372
Jamie Madill21061022018-07-12 23:56:30 -0400373angle::Result TextureVk::generateMipmapLevelsWithCPU(ContextVk *contextVk,
374 const angle::Format &sourceFormat,
375 GLuint layer,
376 GLuint firstMipLevel,
377 GLuint maxMipLevel,
378 const size_t sourceWidth,
379 const size_t sourceHeight,
380 const size_t sourceRowPitch,
381 uint8_t *sourceData)
Luc Ferronc5181702018-05-17 09:44:42 -0400382{
Luc Ferronc5181702018-05-17 09:44:42 -0400383 size_t previousLevelWidth = sourceWidth;
384 size_t previousLevelHeight = sourceHeight;
385 uint8_t *previousLevelData = sourceData;
386 size_t previousLevelRowPitch = sourceRowPitch;
387
388 for (GLuint currentMipLevel = firstMipLevel; currentMipLevel <= maxMipLevel; currentMipLevel++)
389 {
390 // Compute next level width and height.
391 size_t mipWidth = std::max<size_t>(1, previousLevelWidth >> 1);
392 size_t mipHeight = std::max<size_t>(1, previousLevelHeight >> 1);
393
394 // With the width and height of the next mip, we can allocate the next buffer we need.
395 uint8_t *destData = nullptr;
396 size_t destRowPitch = mipWidth * sourceFormat.pixelBytes;
397
398 size_t mipAllocationSize = destRowPitch * mipHeight;
399 gl::Extents mipLevelExtents(static_cast<int>(mipWidth), static_cast<int>(mipHeight), 1);
400
401 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateAndGetData(
Jamie Madill21061022018-07-12 23:56:30 -0400402 contextVk, mipAllocationSize,
Luc Ferron22695bf2018-05-22 15:52:08 -0400403 gl::ImageIndex::MakeFromType(mState.getType(), currentMipLevel, layer), mipLevelExtents,
Luc Ferronc5181702018-05-17 09:44:42 -0400404 gl::Offset(), &destData));
405
406 // Generate the mipmap into that new buffer
407 sourceFormat.mipGenerationFunction(previousLevelWidth, previousLevelHeight, 1,
408 previousLevelData, previousLevelRowPitch, 0, destData,
409 destRowPitch, 0);
410
411 // Swap for the next iteration
412 previousLevelWidth = mipWidth;
413 previousLevelHeight = mipHeight;
414 previousLevelData = destData;
415 previousLevelRowPitch = destRowPitch;
416 }
417
Jamie Madill21061022018-07-12 23:56:30 -0400418 return angle::Result::Continue();
Luc Ferronc5181702018-05-17 09:44:42 -0400419}
420
Jamie Madilla7be1f72018-04-13 15:16:26 -0400421PixelBuffer::SubresourceUpdate::SubresourceUpdate() : bufferHandle(VK_NULL_HANDLE)
Jamie Madill20fa8d52018-04-15 10:09:32 -0400422{
423}
424
Jamie Madilla7be1f72018-04-13 15:16:26 -0400425PixelBuffer::SubresourceUpdate::SubresourceUpdate(VkBuffer bufferHandleIn,
426 const VkBufferImageCopy &copyRegionIn)
Jamie Madill20fa8d52018-04-15 10:09:32 -0400427 : bufferHandle(bufferHandleIn), copyRegion(copyRegionIn)
428{
429}
430
Jamie Madilla7be1f72018-04-13 15:16:26 -0400431PixelBuffer::SubresourceUpdate::SubresourceUpdate(const SubresourceUpdate &other) = default;
Jamie Madill20fa8d52018-04-15 10:09:32 -0400432
Jamie Madill26084d02018-04-09 13:44:04 -0400433// TextureVk implementation.
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400434TextureVk::TextureVk(const gl::TextureState &state, RendererVk *renderer)
Jamie Madillbcf467f2018-05-23 09:46:00 -0400435 : TextureImpl(state), mRenderTarget(&mImage, &mBaseLevelImageView, this), mPixelBuffer(renderer)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400436{
437}
438
439TextureVk::~TextureVk()
440{
441}
442
Jamie Madill035fd6b2017-10-03 15:43:22 -0400443gl::Error TextureVk::onDestroy(const gl::Context *context)
444{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400445 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill035fd6b2017-10-03 15:43:22 -0400446 RendererVk *renderer = contextVk->getRenderer();
447
Jamie Madillc4f27e42018-03-31 14:19:18 -0400448 releaseImage(context, renderer);
Jamie Madillc57ee252018-05-30 19:53:48 -0400449 renderer->releaseObject(getStoredQueueSerial(), &mSampler);
Jamie Madill035fd6b2017-10-03 15:43:22 -0400450
Jamie Madilla7be1f72018-04-13 15:16:26 -0400451 mPixelBuffer.release(renderer);
Jamie Madill035fd6b2017-10-03 15:43:22 -0400452 return gl::NoError();
453}
454
Jamie Madillc564c072017-06-01 12:45:42 -0400455gl::Error TextureVk::setImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400456 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400457 GLenum internalFormat,
458 const gl::Extents &size,
459 GLenum format,
460 GLenum type,
461 const gl::PixelUnpackState &unpack,
462 const uint8_t *pixels)
463{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400464 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill1b038242017-11-01 15:14:36 -0400465 RendererVk *renderer = contextVk->getRenderer();
466
Jamie Madillc4f27e42018-03-31 14:19:18 -0400467 // Convert internalFormat to sized internal format.
468 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat, type);
Jamie Madill035fd6b2017-10-03 15:43:22 -0400469
Geoff Langd691aee2018-07-11 16:32:06 -0400470 ANGLE_TRY(redefineImage(context, index, formatInfo, size));
Jamie Madill035fd6b2017-10-03 15:43:22 -0400471
Geoff Langbd6ae4a2018-01-29 15:51:18 -0500472 // Early-out on empty textures, don't create a zero-sized storage.
Jamie Madill26084d02018-04-09 13:44:04 -0400473 if (size.empty())
Geoff Langbd6ae4a2018-01-29 15:51:18 -0500474 {
475 return gl::NoError();
476 }
477
Jamie Madill26084d02018-04-09 13:44:04 -0400478 // Create a new graph node to store image initialization commands.
Jamie Madill316c6062018-05-29 10:49:45 -0400479 onResourceChanged(renderer);
Jamie Madill26084d02018-04-09 13:44:04 -0400480
Jamie Madill035fd6b2017-10-03 15:43:22 -0400481 // Handle initial data.
Jamie Madill035fd6b2017-10-03 15:43:22 -0400482 if (pixels)
483 {
Luc Ferron33e05ba2018-04-23 15:12:34 -0400484 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdate(contextVk, index, size, gl::Offset(),
485 formatInfo, unpack, type, pixels));
Jamie Madill035fd6b2017-10-03 15:43:22 -0400486 }
487
488 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400489}
490
Jamie Madillc564c072017-06-01 12:45:42 -0400491gl::Error TextureVk::setSubImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400492 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400493 const gl::Box &area,
494 GLenum format,
495 GLenum type,
496 const gl::PixelUnpackState &unpack,
497 const uint8_t *pixels)
498{
Jamie Madill5b18f482017-11-30 17:24:22 -0500499 ContextVk *contextVk = vk::GetImpl(context);
500 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, type);
Luc Ferron33e05ba2018-04-23 15:12:34 -0400501 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdate(
502 contextVk, index, gl::Extents(area.width, area.height, area.depth),
503 gl::Offset(area.x, area.y, area.z), formatInfo, unpack, type, pixels));
Jamie Madillb2214862018-04-26 07:25:48 -0400504
505 // Create a new graph node to store image initialization commands.
Jamie Madill316c6062018-05-29 10:49:45 -0400506 onResourceChanged(contextVk->getRenderer());
Jamie Madillb2214862018-04-26 07:25:48 -0400507
Jamie Madill5b18f482017-11-30 17:24:22 -0500508 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400509}
510
Jamie Madillc564c072017-06-01 12:45:42 -0400511gl::Error TextureVk::setCompressedImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400512 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400513 GLenum internalFormat,
514 const gl::Extents &size,
515 const gl::PixelUnpackState &unpack,
516 size_t imageSize,
517 const uint8_t *pixels)
518{
519 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500520 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400521}
522
Jamie Madillc564c072017-06-01 12:45:42 -0400523gl::Error TextureVk::setCompressedSubImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400524 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400525 const gl::Box &area,
526 GLenum format,
527 const gl::PixelUnpackState &unpack,
528 size_t imageSize,
529 const uint8_t *pixels)
530{
531 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500532 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400533}
534
Jamie Madillc564c072017-06-01 12:45:42 -0400535gl::Error TextureVk::copyImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400536 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400537 const gl::Rectangle &sourceArea,
538 GLenum internalFormat,
Jamie Madill690c8eb2018-03-12 15:20:03 -0400539 gl::Framebuffer *source)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400540{
Luc Ferronf299a372018-05-14 14:44:54 -0400541 gl::Extents newImageSize(sourceArea.width, sourceArea.height, 1);
542 const gl::InternalFormat &internalFormatInfo =
543 gl::GetInternalFormatInfo(internalFormat, GL_UNSIGNED_BYTE);
Geoff Langd691aee2018-07-11 16:32:06 -0400544 ANGLE_TRY(redefineImage(context, index, internalFormatInfo, newImageSize));
Luc Ferronf299a372018-05-14 14:44:54 -0400545 return copySubImageImpl(context, index, gl::Offset(0, 0, 0), sourceArea, internalFormatInfo,
546 source);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400547}
548
Jamie Madillc564c072017-06-01 12:45:42 -0400549gl::Error TextureVk::copySubImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400550 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400551 const gl::Offset &destOffset,
552 const gl::Rectangle &sourceArea,
Jamie Madill690c8eb2018-03-12 15:20:03 -0400553 gl::Framebuffer *source)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400554{
Luc Ferronf299a372018-05-14 14:44:54 -0400555 const gl::InternalFormat &currentFormat = *mState.getBaseLevelDesc().format.info;
556 return copySubImageImpl(context, index, destOffset, sourceArea, currentFormat, source);
557}
558
Geoff Langd691aee2018-07-11 16:32:06 -0400559gl::Error TextureVk::copyTexture(const gl::Context *context,
560 const gl::ImageIndex &index,
561 GLenum internalFormat,
562 GLenum type,
563 size_t sourceLevel,
564 bool unpackFlipY,
565 bool unpackPremultiplyAlpha,
566 bool unpackUnmultiplyAlpha,
567 const gl::Texture *source)
568{
569 TextureVk *sourceVk = vk::GetImpl(source);
570 const gl::ImageDesc &sourceImageDesc =
571 sourceVk->mState.getImageDesc(NonCubeTextureTypeToTarget(source->getType()), sourceLevel);
572 gl::Rectangle sourceArea(0, 0, sourceImageDesc.size.width, sourceImageDesc.size.height);
573
574 const gl::InternalFormat &destFormatInfo = gl::GetInternalFormatInfo(internalFormat, type);
575
576 ANGLE_TRY(redefineImage(context, index, destFormatInfo, sourceImageDesc.size));
577
578 return copySubTextureImpl(vk::GetImpl(context), index, gl::kOffsetZero, destFormatInfo,
579 sourceLevel, sourceArea, unpackFlipY, unpackPremultiplyAlpha,
580 unpackUnmultiplyAlpha, sourceVk);
581}
582
583gl::Error TextureVk::copySubTexture(const gl::Context *context,
584 const gl::ImageIndex &index,
585 const gl::Offset &destOffset,
586 size_t sourceLevel,
587 const gl::Rectangle &sourceArea,
588 bool unpackFlipY,
589 bool unpackPremultiplyAlpha,
590 bool unpackUnmultiplyAlpha,
591 const gl::Texture *source)
592{
593 gl::TextureTarget target = index.getTarget();
594 size_t level = static_cast<size_t>(index.getLevelIndex());
595 const gl::InternalFormat &destFormatInfo = *mState.getImageDesc(target, level).format.info;
596 return copySubTextureImpl(vk::GetImpl(context), index, destOffset, destFormatInfo, sourceLevel,
597 sourceArea, unpackFlipY, unpackPremultiplyAlpha,
598 unpackUnmultiplyAlpha, vk::GetImpl(source));
599}
600
Jamie Madill21061022018-07-12 23:56:30 -0400601angle::Result TextureVk::copySubImageImpl(const gl::Context *context,
602 const gl::ImageIndex &index,
603 const gl::Offset &destOffset,
604 const gl::Rectangle &sourceArea,
605 const gl::InternalFormat &internalFormat,
606 gl::Framebuffer *source)
Luc Ferronf299a372018-05-14 14:44:54 -0400607{
Luc Ferron018709f2018-05-10 13:53:11 -0400608 gl::Extents fbSize = source->getReadColorbuffer()->getSize();
609 gl::Rectangle clippedSourceArea;
610 if (!ClipRectangle(sourceArea, gl::Rectangle(0, 0, fbSize.width, fbSize.height),
611 &clippedSourceArea))
612 {
Jamie Madill21061022018-07-12 23:56:30 -0400613 return angle::Result::Continue();
Luc Ferron018709f2018-05-10 13:53:11 -0400614 }
615
616 const gl::Offset modifiedDestOffset(destOffset.x + sourceArea.x - sourceArea.x,
617 destOffset.y + sourceArea.y - sourceArea.y, 0);
618
Frank Henigmand9618bf2018-06-24 19:57:31 -0400619 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill316c6062018-05-29 10:49:45 -0400620 RendererVk *renderer = contextVk->getRenderer();
Luc Ferronf299a372018-05-14 14:44:54 -0400621 FramebufferVk *framebufferVk = vk::GetImpl(source);
Luc Ferron018709f2018-05-10 13:53:11 -0400622
623 // For now, favor conformance. We do a CPU readback that does the conversion, and then stage the
624 // change to the pixel buffer.
625 // Eventually we can improve this easily by implementing vkCmdBlitImage to do the conversion
626 // when its supported.
Jamie Madill58675012018-05-22 14:54:07 -0400627 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateFromFramebuffer(
Luc Ferron018709f2018-05-10 13:53:11 -0400628 context, index, clippedSourceArea, modifiedDestOffset,
Luc Ferronf299a372018-05-14 14:44:54 -0400629 gl::Extents(clippedSourceArea.width, clippedSourceArea.height, 1), internalFormat,
Jamie Madill58675012018-05-22 14:54:07 -0400630 framebufferVk));
Luc Ferron018709f2018-05-10 13:53:11 -0400631
Jamie Madill316c6062018-05-29 10:49:45 -0400632 onResourceChanged(renderer);
633 framebufferVk->addReadDependency(this);
Jamie Madill21061022018-07-12 23:56:30 -0400634 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400635}
636
Geoff Langd691aee2018-07-11 16:32:06 -0400637gl::Error TextureVk::copySubTextureImpl(ContextVk *contextVk,
638 const gl::ImageIndex &index,
639 const gl::Offset &destOffset,
640 const gl::InternalFormat &destFormat,
641 size_t sourceLevel,
642 const gl::Rectangle &sourceArea,
643 bool unpackFlipY,
644 bool unpackPremultiplyAlpha,
645 bool unpackUnmultiplyAlpha,
646 TextureVk *source)
647{
648 RendererVk *renderer = contextVk->getRenderer();
649
650 // Read back the requested region of the source texture
651 uint8_t *sourceData = nullptr;
652 ANGLE_TRY(source->copyImageDataToBuffer(contextVk, sourceLevel, sourceArea, &sourceData));
653
654 ANGLE_TRY(renderer->finish(contextVk));
655
656 // Using the front-end ANGLE format for the colorRead and colorWrite functions. Otherwise
657 // emulated formats like luminance-alpha would not know how to interpret the data.
658 const angle::Format &sourceAngleFormat = source->getImage().getFormat().angleFormat();
659 const angle::Format &destAngleFormat =
660 renderer->getFormat(destFormat.sizedInternalFormat).angleFormat();
661 size_t destinationAllocationSize =
662 sourceArea.width * sourceArea.height * destAngleFormat.pixelBytes;
663
664 // Allocate memory in the destination texture for the copy/conversion
665 uint8_t *destData = nullptr;
666 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateAndGetData(
667 contextVk, destinationAllocationSize, index,
668 gl::Extents(sourceArea.width, sourceArea.height, 1), destOffset, &destData));
669
670 // Source and dest data is tightly packed
671 GLuint sourceDataRowPitch = sourceArea.width * sourceAngleFormat.pixelBytes;
672 GLuint destDataRowPitch = sourceArea.width * destAngleFormat.pixelBytes;
673
674 CopyImageCHROMIUM(sourceData, sourceDataRowPitch, sourceAngleFormat.pixelBytes,
675 sourceAngleFormat.colorReadFunction, destData, destDataRowPitch,
676 destAngleFormat.pixelBytes, destAngleFormat.colorWriteFunction,
677 destFormat.format, destFormat.componentType, sourceArea.width,
678 sourceArea.height, unpackFlipY, unpackPremultiplyAlpha,
679 unpackUnmultiplyAlpha);
680
681 // Create a new graph node to store image initialization commands.
682 onResourceChanged(contextVk->getRenderer());
683
684 return angle::Result::Continue();
685}
686
Jamie Madill21061022018-07-12 23:56:30 -0400687angle::Result TextureVk::getCommandBufferForWrite(ContextVk *contextVk,
688 vk::CommandBuffer **commandBufferOut)
Luc Ferronfa7503c2018-05-08 11:25:06 -0400689{
Jamie Madill21061022018-07-12 23:56:30 -0400690 ANGLE_TRY(appendWriteResource(contextVk, commandBufferOut));
691 return angle::Result::Continue();
Luc Ferronfa7503c2018-05-08 11:25:06 -0400692}
693
Jamie Madillc564c072017-06-01 12:45:42 -0400694gl::Error TextureVk::setStorage(const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500695 gl::TextureType type,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400696 size_t levels,
697 GLenum internalFormat,
698 const gl::Extents &size)
699{
Luc Ferronfa7503c2018-05-08 11:25:06 -0400700 ContextVk *contextVk = GetAs<ContextVk>(context->getImplementation());
701 RendererVk *renderer = contextVk->getRenderer();
702 const vk::Format &format = renderer->getFormat(internalFormat);
703 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400704 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Luc Ferronf6e160f2018-06-12 10:13:57 -0400705 ANGLE_TRY(initImage(contextVk, format, size, static_cast<uint32_t>(levels), commandBuffer));
Luc Ferronfa7503c2018-05-08 11:25:06 -0400706 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400707}
708
Corentin Wallez99d492c2018-02-27 15:17:10 -0500709gl::Error TextureVk::setEGLImageTarget(const gl::Context *context,
710 gl::TextureType type,
711 egl::Image *image)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400712{
713 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500714 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400715}
716
Jamie Madill4928b7c2017-06-20 12:57:39 -0400717gl::Error TextureVk::setImageExternal(const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500718 gl::TextureType type,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400719 egl::Stream *stream,
720 const egl::Stream::GLTextureDescription &desc)
721{
722 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500723 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400724}
725
Geoff Langd691aee2018-07-11 16:32:06 -0400726angle::Result TextureVk::redefineImage(const gl::Context *context,
727 const gl::ImageIndex &index,
728 const gl::InternalFormat &internalFormat,
729 const gl::Extents &size)
730{
731 ContextVk *contextVk = vk::GetImpl(context);
732 RendererVk *renderer = contextVk->getRenderer();
733
734 // If there is any staged changes for this index, we can remove them since we're going to
735 // override them with this call.
736 mPixelBuffer.removeStagedUpdates(index);
737
738 if (mImage.valid())
739 {
740 const vk::Format &vkFormat = renderer->getFormat(internalFormat.sizedInternalFormat);
741
742 // Calculate the expected size for the index we are defining. If the size is different from
743 // the given size, or the format is different, we are redefining the image so we must
744 // release it.
745 if (mImage.getFormat() != vkFormat || size != mImage.getSize(index))
746 {
747 releaseImage(context, renderer);
748 }
749 }
750
751 return angle::Result::Continue();
752}
753
754angle::Result TextureVk::copyImageDataToBuffer(ContextVk *contextVk,
755 size_t sourceLevel,
756 const gl::Rectangle &sourceArea,
757 uint8_t **outDataPtr)
758{
759 if (sourceLevel != 0)
760 {
761 WARN() << "glCopyTextureCHROMIUM with sourceLevel != 0 not implemented.";
762 return angle::Result::Stop();
763 }
764
765 // Make sure the source is initialized and it's images are flushed.
766 ANGLE_TRY(ensureImageInitialized(contextVk));
767
768 const angle::Format &angleFormat = getImage().getFormat().textureFormat();
769 const gl::Extents imageSize =
770 mState.getImageDesc(NonCubeTextureTypeToTarget(mState.getType()), sourceLevel).size;
771 size_t sourceCopyAllocationSize = sourceArea.width * sourceArea.height * angleFormat.pixelBytes;
772
773 vk::CommandBuffer *commandBuffer = nullptr;
774 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
775
776 // Requirement of the copyImageToBuffer, the source image must be in SRC_OPTIMAL layout.
777 bool newBufferAllocated = false;
778 mImage.changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
779 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
780 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer);
781
782 // Allocate enough memory to copy the sourceArea region of the source texture into its pixel
783 // buffer.
784 VkBuffer copyBufferHandle;
785 uint32_t sourceCopyOffset = 0;
786 ANGLE_TRY(mPixelBuffer.allocate(contextVk, sourceCopyAllocationSize, outDataPtr,
787 &copyBufferHandle, &sourceCopyOffset, &newBufferAllocated));
788
789 VkBufferImageCopy region;
790 region.bufferOffset = static_cast<VkDeviceSize>(sourceCopyOffset);
791 region.bufferRowLength = imageSize.width;
792 region.bufferImageHeight = imageSize.height;
793 region.imageExtent.width = sourceArea.width;
794 region.imageExtent.height = sourceArea.height;
795 region.imageExtent.depth = 1;
796 region.imageOffset.x = sourceArea.x;
797 region.imageOffset.y = sourceArea.y;
798 region.imageOffset.z = 0;
799 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
800 region.imageSubresource.baseArrayLayer = 0;
801 region.imageSubresource.layerCount = 1;
802 region.imageSubresource.mipLevel = sourceLevel;
803
804 commandBuffer->copyImageToBuffer(mImage.getImage(), mImage.getCurrentLayout(), copyBufferHandle,
805 1, &region);
806
807 return angle::Result::Continue();
808}
809
Jamie Madill21061022018-07-12 23:56:30 -0400810angle::Result TextureVk::generateMipmapWithBlit(ContextVk *contextVk)
Luc Ferron05cd6df2018-05-24 15:51:29 -0400811{
812 uint32_t imageLayerCount = GetImageLayerCount(mState.getType());
813 const gl::Extents baseLevelExtents = mImage.getExtents();
814 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400815 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Luc Ferron05cd6df2018-05-24 15:51:29 -0400816
817 // We are able to use blitImage since the image format we are using supports it. This
818 // is a faster way we can generate the mips.
819 int32_t mipWidth = baseLevelExtents.width;
820 int32_t mipHeight = baseLevelExtents.height;
821
822 // Manually manage the image memory barrier because it uses a lot more parameters than our
823 // usual one.
824 VkImageMemoryBarrier barrier;
825 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
826 barrier.image = mImage.getImage().getHandle();
827 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
828 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
829 barrier.pNext = nullptr;
830 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
831 barrier.subresourceRange.baseArrayLayer = 0;
832 barrier.subresourceRange.layerCount = imageLayerCount;
833 barrier.subresourceRange.levelCount = 1;
834
835 for (uint32_t mipLevel = 1; mipLevel <= mState.getMipmapMaxLevel(); mipLevel++)
836 {
837 int32_t nextMipWidth = std::max<int32_t>(1, mipWidth >> 1);
838 int32_t nextMipHeight = std::max<int32_t>(1, mipHeight >> 1);
839
840 barrier.subresourceRange.baseMipLevel = mipLevel - 1;
841 barrier.oldLayout = mImage.getCurrentLayout();
842 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
843 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
844 barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
845
846 // We can do it for all layers at once.
847 commandBuffer->singleImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT,
848 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, barrier);
849
850 VkImageBlit blit = {};
851 blit.srcOffsets[0] = {0, 0, 0};
852 blit.srcOffsets[1] = {mipWidth, mipHeight, 1};
853 blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
854 blit.srcSubresource.mipLevel = mipLevel - 1;
855 blit.srcSubresource.baseArrayLayer = 0;
856 blit.srcSubresource.layerCount = imageLayerCount;
857 blit.dstOffsets[0] = {0, 0, 0};
858 blit.dstOffsets[1] = {nextMipWidth, nextMipHeight, 1};
859 blit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
860 blit.dstSubresource.mipLevel = mipLevel;
861 blit.dstSubresource.baseArrayLayer = 0;
862 blit.dstSubresource.layerCount = imageLayerCount;
863
864 mipWidth = nextMipWidth;
865 mipHeight = nextMipHeight;
866
867 commandBuffer->blitImage(mImage.getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
868 mImage.getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit,
869 VK_FILTER_LINEAR);
870 }
871
872 // Transition the last mip level to the same layout as all the other ones, so we can declare
873 // our whole image layout to be SRC_OPTIMAL.
874 barrier.subresourceRange.baseMipLevel = mState.getMipmapMaxLevel();
875 barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
876 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
877
878 // We can do it for all layers at once.
879 commandBuffer->singleImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT,
880 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, barrier);
881
882 // This is just changing the internal state of the image helper so that the next call
883 // to changeLayoutWithStages will use this layout as the "oldLayout" argument.
884 mImage.updateLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Jamie Madilleebe2192018-07-11 09:01:18 -0400885
Jamie Madill21061022018-07-12 23:56:30 -0400886 return angle::Result::Continue();
Luc Ferron05cd6df2018-05-24 15:51:29 -0400887}
888
Jamie Madill21061022018-07-12 23:56:30 -0400889angle::Result TextureVk::generateMipmapWithCPU(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400890{
Luc Ferron22695bf2018-05-22 15:52:08 -0400891 ContextVk *contextVk = vk::GetImpl(context);
892 RendererVk *renderer = contextVk->getRenderer();
Luc Ferronc5181702018-05-17 09:44:42 -0400893
Luc Ferronc5181702018-05-17 09:44:42 -0400894 bool newBufferAllocated = false;
Luc Ferronc5181702018-05-17 09:44:42 -0400895 const gl::Extents baseLevelExtents = mImage.getExtents();
Luc Ferron05cd6df2018-05-24 15:51:29 -0400896 uint32_t imageLayerCount = GetImageLayerCount(mState.getType());
897 const angle::Format &angleFormat = mImage.getFormat().textureFormat();
Luc Ferronc5181702018-05-17 09:44:42 -0400898 GLuint sourceRowPitch = baseLevelExtents.width * angleFormat.pixelBytes;
899 size_t baseLevelAllocationSize = sourceRowPitch * baseLevelExtents.height;
900
Luc Ferron22695bf2018-05-22 15:52:08 -0400901 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400902 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Luc Ferronc5181702018-05-17 09:44:42 -0400903
Luc Ferron22695bf2018-05-22 15:52:08 -0400904 // Requirement of the copyImageToBuffer, the source image must be in SRC_OPTIMAL layout.
905 mImage.changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
906 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
907 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer);
908
909 size_t totalAllocationSize = baseLevelAllocationSize * imageLayerCount;
910
911 VkBuffer copyBufferHandle;
912 uint8_t *baseLevelBuffers;
913 uint32_t copyBaseOffset;
914
915 // Allocate enough memory to copy every level 0 image (one for each layer of the texture).
Jamie Madill21061022018-07-12 23:56:30 -0400916 ANGLE_TRY(mPixelBuffer.allocate(contextVk, totalAllocationSize, &baseLevelBuffers,
Luc Ferron22695bf2018-05-22 15:52:08 -0400917 &copyBufferHandle, &copyBaseOffset, &newBufferAllocated));
918
919 // Do only one copy for all layers at once.
Luc Ferronc5181702018-05-17 09:44:42 -0400920 VkBufferImageCopy region;
921 region.bufferImageHeight = baseLevelExtents.height;
Luc Ferron22695bf2018-05-22 15:52:08 -0400922 region.bufferOffset = static_cast<VkDeviceSize>(copyBaseOffset);
Luc Ferronc5181702018-05-17 09:44:42 -0400923 region.bufferRowLength = baseLevelExtents.width;
924 region.imageExtent.width = baseLevelExtents.width;
925 region.imageExtent.height = baseLevelExtents.height;
926 region.imageExtent.depth = 1;
927 region.imageOffset.x = 0;
928 region.imageOffset.y = 0;
929 region.imageOffset.z = 0;
930 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
931 region.imageSubresource.baseArrayLayer = 0;
Luc Ferron22695bf2018-05-22 15:52:08 -0400932 region.imageSubresource.layerCount = imageLayerCount;
Luc Ferronc5181702018-05-17 09:44:42 -0400933 region.imageSubresource.mipLevel = mState.getEffectiveBaseLevel();
934
Luc Ferron22695bf2018-05-22 15:52:08 -0400935 commandBuffer->copyImageToBuffer(mImage.getImage(), mImage.getCurrentLayout(), copyBufferHandle,
936 1, &region);
Luc Ferronc5181702018-05-17 09:44:42 -0400937
Jamie Madill21061022018-07-12 23:56:30 -0400938 ANGLE_TRY(renderer->finish(contextVk));
Luc Ferronc5181702018-05-17 09:44:42 -0400939
Luc Ferron2f3f4142018-05-30 08:27:19 -0400940 const uint32_t levelCount = getLevelCount();
941
Luc Ferronc5181702018-05-17 09:44:42 -0400942 // We now have the base level available to be manipulated in the baseLevelBuffer pointer.
943 // Generate all the missing mipmaps with the slow path. We can optimize with vkCmdBlitImage
944 // later.
Luc Ferron22695bf2018-05-22 15:52:08 -0400945 // For each layer, use the copied data to generate all the mips.
946 for (GLuint layer = 0; layer < imageLayerCount; layer++)
947 {
948 size_t bufferOffset = layer * baseLevelAllocationSize;
Luc Ferron05cd6df2018-05-24 15:51:29 -0400949
950 ANGLE_TRY(generateMipmapLevelsWithCPU(
Luc Ferron22695bf2018-05-22 15:52:08 -0400951 contextVk, angleFormat, layer, mState.getEffectiveBaseLevel() + 1,
952 mState.getMipmapMaxLevel(), baseLevelExtents.width, baseLevelExtents.height,
953 sourceRowPitch, baseLevelBuffers + bufferOffset));
954 }
Luc Ferronc5181702018-05-17 09:44:42 -0400955
Jamie Madill21061022018-07-12 23:56:30 -0400956 return mPixelBuffer.flushUpdatesToImage(contextVk, levelCount, &mImage, commandBuffer);
Luc Ferron05cd6df2018-05-24 15:51:29 -0400957}
958
959gl::Error TextureVk::generateMipmap(const gl::Context *context)
960{
961 ContextVk *contextVk = vk::GetImpl(context);
Luc Ferron05cd6df2018-05-24 15:51:29 -0400962
963 // Some data is pending, or the image has not been defined at all yet
964 if (!mImage.valid())
965 {
966 // lets initialize the image so we can generate the next levels.
967 if (!mPixelBuffer.empty())
968 {
Luc Ferronf6e160f2018-06-12 10:13:57 -0400969 ANGLE_TRY(ensureImageInitialized(contextVk));
Luc Ferron05cd6df2018-05-24 15:51:29 -0400970 ASSERT(mImage.valid());
971 }
972 else
973 {
974 // There is nothing to generate if there is nothing uploaded so far.
975 return gl::NoError();
976 }
977 }
978
Luc Ferronf6e160f2018-06-12 10:13:57 -0400979 RendererVk *renderer = contextVk->getRenderer();
Luc Ferron05cd6df2018-05-24 15:51:29 -0400980 VkFormatProperties imageProperties;
981 vk::GetFormatProperties(renderer->getPhysicalDevice(), mImage.getFormat().vkTextureFormat,
982 &imageProperties);
983
984 // Check if the image supports blit. If it does, we can do the mipmap generation on the gpu
985 // only.
986 if (IsMaskFlagSet(kBlitFeatureFlags, imageProperties.linearTilingFeatures))
987 {
Jamie Madill21061022018-07-12 23:56:30 -0400988 ANGLE_TRY(generateMipmapWithBlit(contextVk));
Luc Ferron05cd6df2018-05-24 15:51:29 -0400989 }
990 else
991 {
992 ANGLE_TRY(generateMipmapWithCPU(context));
993 }
994
995 // We're changing this textureVk content, make sure we let the graph know.
996 onResourceChanged(renderer);
997
Luc Ferronc5181702018-05-17 09:44:42 -0400998 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400999}
1000
Jamie Madill4928b7c2017-06-20 12:57:39 -04001001gl::Error TextureVk::setBaseLevel(const gl::Context *context, GLuint baseLevel)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001002{
1003 UNIMPLEMENTED();
Jamie Madill4928b7c2017-06-20 12:57:39 -04001004 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001005}
1006
Jamie Madill4928b7c2017-06-20 12:57:39 -04001007gl::Error TextureVk::bindTexImage(const gl::Context *context, egl::Surface *surface)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001008{
1009 UNIMPLEMENTED();
Jamie Madill4928b7c2017-06-20 12:57:39 -04001010 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001011}
1012
Jamie Madill4928b7c2017-06-20 12:57:39 -04001013gl::Error TextureVk::releaseTexImage(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001014{
1015 UNIMPLEMENTED();
Jamie Madill4928b7c2017-06-20 12:57:39 -04001016 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001017}
1018
Jamie Madill4928b7c2017-06-20 12:57:39 -04001019gl::Error TextureVk::getAttachmentRenderTarget(const gl::Context *context,
1020 GLenum binding,
Jamie Madill4fd95d52017-04-05 11:22:18 -04001021 const gl::ImageIndex &imageIndex,
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001022 FramebufferAttachmentRenderTarget **rtOut)
1023{
Luc Ferronfa7503c2018-05-08 11:25:06 -04001024 // TODO(jmadill): Handle cube textures. http://anglebug.com/2470
Jamie Madillcc129372018-04-12 09:13:18 -04001025 ASSERT(imageIndex.getType() == gl::TextureType::_2D);
Jamie Madill26084d02018-04-09 13:44:04 -04001026
1027 // Non-zero mip level attachments are an ES 3.0 feature.
Jamie Madillcc129372018-04-12 09:13:18 -04001028 ASSERT(imageIndex.getLevelIndex() == 0 && !imageIndex.hasLayer());
Jamie Madill26084d02018-04-09 13:44:04 -04001029
1030 ContextVk *contextVk = vk::GetImpl(context);
Luc Ferronf6e160f2018-06-12 10:13:57 -04001031 ANGLE_TRY(ensureImageInitialized(contextVk));
Jamie Madill26084d02018-04-09 13:44:04 -04001032
Jamie Madillb79e7bb2017-10-24 13:55:50 -04001033 *rtOut = &mRenderTarget;
1034 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001035}
1036
Jamie Madill21061022018-07-12 23:56:30 -04001037angle::Result TextureVk::ensureImageInitialized(ContextVk *contextVk)
Jamie Madill26084d02018-04-09 13:44:04 -04001038{
Luc Ferron10434f62018-04-24 10:06:37 -04001039 if (mImage.valid() && mPixelBuffer.empty())
1040 {
Jamie Madill21061022018-07-12 23:56:30 -04001041 return angle::Result::Continue();
Luc Ferron10434f62018-04-24 10:06:37 -04001042 }
Luc Ferronf6e160f2018-06-12 10:13:57 -04001043 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill26084d02018-04-09 13:44:04 -04001044 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001045 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Jamie Madill26084d02018-04-09 13:44:04 -04001046
Luc Ferron2f3f4142018-05-30 08:27:19 -04001047 const gl::ImageDesc &baseLevelDesc = mState.getBaseLevelDesc();
1048 const gl::Extents &baseLevelExtents = baseLevelDesc.size;
1049 const uint32_t levelCount = getLevelCount();
1050
Jamie Madill26084d02018-04-09 13:44:04 -04001051 if (!mImage.valid())
1052 {
Jamie Madill26084d02018-04-09 13:44:04 -04001053 const vk::Format &format =
1054 renderer->getFormat(baseLevelDesc.format.info->sizedInternalFormat);
Jamie Madill26084d02018-04-09 13:44:04 -04001055
Luc Ferronf6e160f2018-06-12 10:13:57 -04001056 ANGLE_TRY(initImage(contextVk, format, baseLevelExtents, levelCount, commandBuffer));
Jamie Madill26084d02018-04-09 13:44:04 -04001057 }
1058
Jamie Madill21061022018-07-12 23:56:30 -04001059 ANGLE_TRY(mPixelBuffer.flushUpdatesToImage(contextVk, levelCount, &mImage, commandBuffer));
1060 return angle::Result::Continue();
Jamie Madill26084d02018-04-09 13:44:04 -04001061}
1062
Luc Ferron4bba74f2018-04-19 14:40:45 -04001063gl::Error TextureVk::syncState(const gl::Context *context, const gl::Texture::DirtyBits &dirtyBits)
Geoff Lang22416862016-06-08 16:14:36 -07001064{
Luc Ferron20610902018-04-19 14:41:13 -04001065 if (dirtyBits.none() && mSampler.valid())
1066 {
1067 return gl::NoError();
1068 }
1069
1070 ContextVk *contextVk = vk::GetImpl(context);
1071 if (mSampler.valid())
1072 {
1073 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc57ee252018-05-30 19:53:48 -04001074 renderer->releaseObject(getStoredQueueSerial(), &mSampler);
Luc Ferron20610902018-04-19 14:41:13 -04001075 }
1076
1077 const gl::SamplerState &samplerState = mState.getSamplerState();
1078
1079 // Create a simple sampler. Force basic parameter settings.
1080 VkSamplerCreateInfo samplerInfo;
1081 samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
1082 samplerInfo.pNext = nullptr;
1083 samplerInfo.flags = 0;
1084 samplerInfo.magFilter = gl_vk::GetFilter(samplerState.magFilter);
1085 samplerInfo.minFilter = gl_vk::GetFilter(samplerState.minFilter);
Luc Ferron66410532018-04-20 12:47:45 -04001086 samplerInfo.mipmapMode = gl_vk::GetSamplerMipmapMode(samplerState.minFilter);
Luc Ferron20610902018-04-19 14:41:13 -04001087 samplerInfo.addressModeU = gl_vk::GetSamplerAddressMode(samplerState.wrapS);
1088 samplerInfo.addressModeV = gl_vk::GetSamplerAddressMode(samplerState.wrapT);
1089 samplerInfo.addressModeW = gl_vk::GetSamplerAddressMode(samplerState.wrapR);
1090 samplerInfo.mipLodBias = 0.0f;
1091 samplerInfo.anisotropyEnable = VK_FALSE;
1092 samplerInfo.maxAnisotropy = 1.0f;
1093 samplerInfo.compareEnable = VK_FALSE;
1094 samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
Luc Ferron66410532018-04-20 12:47:45 -04001095 samplerInfo.minLod = samplerState.minLod;
1096 samplerInfo.maxLod = samplerState.maxLod;
Luc Ferron20610902018-04-19 14:41:13 -04001097 samplerInfo.borderColor = VK_BORDER_COLOR_INT_TRANSPARENT_BLACK;
1098 samplerInfo.unnormalizedCoordinates = VK_FALSE;
1099
Jamie Madill21061022018-07-12 23:56:30 -04001100 ANGLE_TRY(mSampler.init(contextVk, samplerInfo));
Luc Ferron4bba74f2018-04-19 14:40:45 -04001101 return gl::NoError();
Geoff Lang22416862016-06-08 16:14:36 -07001102}
1103
Jamie Madillc564c072017-06-01 12:45:42 -04001104gl::Error TextureVk::setStorageMultisample(const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -05001105 gl::TextureType type,
JiangYizhoubddc46b2016-12-09 09:50:51 +08001106 GLsizei samples,
1107 GLint internalformat,
1108 const gl::Extents &size,
Geoff Lang92019432017-11-20 13:09:34 -05001109 bool fixedSampleLocations)
JiangYizhoubddc46b2016-12-09 09:50:51 +08001110{
1111 UNIMPLEMENTED();
1112 return gl::InternalError() << "setStorageMultisample is unimplemented.";
1113}
1114
Jamie Madill05b35b22017-10-03 09:01:44 -04001115gl::Error TextureVk::initializeContents(const gl::Context *context,
1116 const gl::ImageIndex &imageIndex)
1117{
1118 UNIMPLEMENTED();
1119 return gl::NoError();
1120}
1121
Jamie Madill858c1cc2018-03-31 14:19:13 -04001122const vk::ImageHelper &TextureVk::getImage() const
Jamie Madill5547b382017-10-23 18:16:01 -04001123{
1124 ASSERT(mImage.valid());
Jamie Madill858c1cc2018-03-31 14:19:13 -04001125 return mImage;
Jamie Madill5547b382017-10-23 18:16:01 -04001126}
1127
1128const vk::ImageView &TextureVk::getImageView() const
1129{
Jamie Madill93edca12018-03-30 10:43:18 -04001130 ASSERT(mImage.valid());
Luc Ferron66410532018-04-20 12:47:45 -04001131
1132 const GLenum minFilter = mState.getSamplerState().minFilter;
1133 if (minFilter == GL_LINEAR || minFilter == GL_NEAREST)
1134 {
1135 return mBaseLevelImageView;
1136 }
1137
1138 return mMipmapImageView;
Jamie Madill5547b382017-10-23 18:16:01 -04001139}
1140
1141const vk::Sampler &TextureVk::getSampler() const
1142{
1143 ASSERT(mSampler.valid());
1144 return mSampler;
1145}
1146
Jamie Madill21061022018-07-12 23:56:30 -04001147angle::Result TextureVk::initImage(ContextVk *contextVk,
1148 const vk::Format &format,
1149 const gl::Extents &extents,
1150 const uint32_t levelCount,
1151 vk::CommandBuffer *commandBuffer)
Luc Ferronfa7503c2018-05-08 11:25:06 -04001152{
Luc Ferronf6e160f2018-06-12 10:13:57 -04001153 const RendererVk *renderer = contextVk->getRenderer();
Luc Ferronfa7503c2018-05-08 11:25:06 -04001154
1155 const VkImageUsageFlags usage =
1156 (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1157 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT);
1158
Jamie Madill21061022018-07-12 23:56:30 -04001159 ANGLE_TRY(mImage.init(contextVk, mState.getType(), extents, format, 1, usage, levelCount));
Luc Ferronfa7503c2018-05-08 11:25:06 -04001160
1161 const VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
1162
Jamie Madill21061022018-07-12 23:56:30 -04001163 ANGLE_TRY(mImage.initMemory(contextVk, renderer->getMemoryProperties(), flags));
Luc Ferronfa7503c2018-05-08 11:25:06 -04001164
1165 gl::SwizzleState mappedSwizzle;
1166 MapSwizzleState(format.internalFormat, mState.getSwizzleState(), &mappedSwizzle);
1167
Luc Ferronf6e160f2018-06-12 10:13:57 -04001168 // Renderable textures cannot have a swizzle.
1169 ASSERT(!contextVk->getTextureCaps().get(format.internalFormat).textureAttachment ||
1170 !mappedSwizzle.swizzleRequired());
1171
Luc Ferronfa7503c2018-05-08 11:25:06 -04001172 // TODO(jmadill): Separate imageviews for RenderTargets and Sampling.
Jamie Madill21061022018-07-12 23:56:30 -04001173 ANGLE_TRY(mImage.initImageView(contextVk, mState.getType(), VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferronfa7503c2018-05-08 11:25:06 -04001174 mappedSwizzle, &mMipmapImageView, levelCount));
Jamie Madill21061022018-07-12 23:56:30 -04001175 ANGLE_TRY(mImage.initImageView(contextVk, mState.getType(), VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferronfa7503c2018-05-08 11:25:06 -04001176 mappedSwizzle, &mBaseLevelImageView, 1));
1177
1178 // TODO(jmadill): Fold this into the RenderPass load/store ops. http://anglebug.com/2361
Luc Ferron7348fc52018-05-09 07:17:16 -04001179 VkClearColorValue black = {{0, 0, 0, 1.0f}};
Luc Ferronc20b9502018-05-24 09:30:17 -04001180 mImage.clearColor(black, 0, levelCount, commandBuffer);
Jamie Madill21061022018-07-12 23:56:30 -04001181 return angle::Result::Continue();
Luc Ferronfa7503c2018-05-08 11:25:06 -04001182}
1183
Jamie Madillc4f27e42018-03-31 14:19:18 -04001184void TextureVk::releaseImage(const gl::Context *context, RendererVk *renderer)
1185{
1186 mImage.release(renderer->getCurrentQueueSerial(), renderer);
Jamie Madillc57ee252018-05-30 19:53:48 -04001187 renderer->releaseObject(getStoredQueueSerial(), &mBaseLevelImageView);
1188 renderer->releaseObject(getStoredQueueSerial(), &mMipmapImageView);
Jamie Madillc4f27e42018-03-31 14:19:18 -04001189 onStateChange(context, angle::SubjectMessage::DEPENDENT_DIRTY_BITS);
1190}
1191
Luc Ferron66410532018-04-20 12:47:45 -04001192uint32_t TextureVk::getLevelCount() const
1193{
1194 ASSERT(mState.getEffectiveBaseLevel() == 0);
1195
1196 // getMipmapMaxLevel will be 0 here if mipmaps are not used, so the levelCount is always +1.
1197 return mState.getMipmapMaxLevel() + 1;
1198}
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001199} // namespace rx