blob: a6ddcf29f99612418f482dc218a651105e6f8891 [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;
Jamie Madill4c310832018-08-29 13:43:17 -0400131 VkDeviceSize stagingOffset = 0;
Jamie Madill26084d02018-04-09 13:44:04 -0400132 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 Madill4c310832018-08-29 13:43:17 -0400145 copy.bufferOffset = stagingOffset;
Jamie Madill20fa8d52018-04-15 10:09:32 -0400146 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;
Jamie Madill4c310832018-08-29 13:43:17 -0400202 VkDeviceSize 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
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400218 const angle::Format &copyFormat =
219 GetFormatFromFormatType(formatInfo.internalFormat, formatInfo.type);
220 PackPixelsParams params(clippedRectangle, copyFormat, static_cast<GLuint>(outputRowPitch),
221 pixelPackState, nullptr, 0);
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400222
Luc Ferron018709f2018-05-10 13:53:11 -0400223 // 2- copy the source image region to the pixel buffer using a cpu readback
224 if (loadFunction.requiresConversion)
225 {
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400226 // When a conversion is required, we need to use the loadFunction to read from a temporary
227 // buffer instead so its an even slower path.
Luc Ferronaa2126c2018-07-09 15:36:36 -0400228 size_t bufferSize =
229 storageFormat.pixelBytes * clippedRectangle.width * clippedRectangle.height;
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400230 angle::MemoryBuffer *memoryBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400231 ANGLE_VK_CHECK_ALLOC(contextVk, context->getScratchBuffer(bufferSize, &memoryBuffer));
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400232
233 // Read into the scratch buffer
Luc Ferron1617e692018-07-11 11:08:19 -0400234 ANGLE_TRY(framebufferVk->readPixelsImpl(
Jamie Madill21061022018-07-12 23:56:30 -0400235 contextVk, clippedRectangle, params, VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferron1617e692018-07-11 11:08:19 -0400236 framebufferVk->getColorReadRenderTarget(), memoryBuffer->data()));
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400237
238 // Load from scratch buffer to our pixel buffer
Luc Ferronaa2126c2018-07-09 15:36:36 -0400239 loadFunction.loadFunction(clippedRectangle.width, clippedRectangle.height, 1,
240 memoryBuffer->data(), outputRowPitch, 0, stagingPointer,
241 outputRowPitch, 0);
Luc Ferron018709f2018-05-10 13:53:11 -0400242 }
243 else
244 {
Luc Ferrondaf7ace2018-05-14 13:44:15 -0400245 // We read directly from the framebuffer into our pixel buffer.
Luc Ferron1617e692018-07-11 11:08:19 -0400246 ANGLE_TRY(framebufferVk->readPixelsImpl(
Jamie Madill21061022018-07-12 23:56:30 -0400247 contextVk, clippedRectangle, params, VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferron1617e692018-07-11 11:08:19 -0400248 framebufferVk->getColorReadRenderTarget(), stagingPointer));
Luc Ferron018709f2018-05-10 13:53:11 -0400249 }
Luc Ferron2a849bf2018-05-10 13:19:11 -0400250
Luc Ferron018709f2018-05-10 13:53:11 -0400251 // 3- enqueue the destination image subresource update
Luc Ferron2a849bf2018-05-10 13:19:11 -0400252 VkBufferImageCopy copyToImage;
253 copyToImage.bufferOffset = static_cast<VkDeviceSize>(stagingOffset);
Luc Ferron018709f2018-05-10 13:53:11 -0400254 copyToImage.bufferRowLength = 0; // Tightly packed data can be specified as 0.
Luc Ferron2a849bf2018-05-10 13:19:11 -0400255 copyToImage.bufferImageHeight = clippedRectangle.height;
256 copyToImage.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
257 copyToImage.imageSubresource.mipLevel = index.getLevelIndex();
258 copyToImage.imageSubresource.baseArrayLayer = index.hasLayer() ? index.getLayerIndex() : 0;
259 copyToImage.imageSubresource.layerCount = index.getLayerCount();
260 gl_vk::GetOffset(dstOffset, &copyToImage.imageOffset);
261 gl_vk::GetExtent(dstExtent, &copyToImage.imageExtent);
262
263 // 3- enqueue the destination image subresource update
264 mSubresourceUpdates.emplace_back(bufferHandle, copyToImage);
Jamie Madill21061022018-07-12 23:56:30 -0400265 return angle::Result::Continue();
Luc Ferron2a849bf2018-05-10 13:19:11 -0400266}
267
Jamie Madill21061022018-07-12 23:56:30 -0400268angle::Result PixelBuffer::allocate(ContextVk *contextVk,
269 size_t sizeInBytes,
270 uint8_t **ptrOut,
271 VkBuffer *handleOut,
Jamie Madill4c310832018-08-29 13:43:17 -0400272 VkDeviceSize *offsetOut,
Jamie Madill21061022018-07-12 23:56:30 -0400273 bool *newBufferAllocatedOut)
Luc Ferronc5181702018-05-17 09:44:42 -0400274{
Jamie Madill21061022018-07-12 23:56:30 -0400275 return mStagingBuffer.allocate(contextVk, sizeInBytes, ptrOut, handleOut, offsetOut,
Luc Ferronc5181702018-05-17 09:44:42 -0400276 newBufferAllocatedOut);
277}
278
Jamie Madill21061022018-07-12 23:56:30 -0400279angle::Result PixelBuffer::flushUpdatesToImage(ContextVk *contextVk,
280 uint32_t levelCount,
281 vk::ImageHelper *image,
282 vk::CommandBuffer *commandBuffer)
Jamie Madill26084d02018-04-09 13:44:04 -0400283{
Jamie Madill20fa8d52018-04-15 10:09:32 -0400284 if (mSubresourceUpdates.empty())
Jamie Madill26084d02018-04-09 13:44:04 -0400285 {
Jamie Madill21061022018-07-12 23:56:30 -0400286 return angle::Result::Continue();
Jamie Madill26084d02018-04-09 13:44:04 -0400287 }
288
Jamie Madill21061022018-07-12 23:56:30 -0400289 ANGLE_TRY(mStagingBuffer.flush(contextVk));
Jamie Madill20fa8d52018-04-15 10:09:32 -0400290
Luc Ferron2f3f4142018-05-30 08:27:19 -0400291 std::vector<SubresourceUpdate> updatesToKeep;
292
Jamie Madill20fa8d52018-04-15 10:09:32 -0400293 for (const SubresourceUpdate &update : mSubresourceUpdates)
294 {
295 ASSERT(update.bufferHandle != VK_NULL_HANDLE);
Luc Ferron1a186b12018-04-24 15:25:35 -0400296
Luc Ferron2f3f4142018-05-30 08:27:19 -0400297 const uint32_t updateMipLevel = update.copyRegion.imageSubresource.mipLevel;
298 // It's possible we've accumulated updates that are no longer applicable if the image has
299 // never been flushed but the image description has changed. Check if this level exist for
300 // this image.
301 if (updateMipLevel >= levelCount)
302 {
303 updatesToKeep.emplace_back(update);
304 continue;
305 }
306
Luc Ferron1a186b12018-04-24 15:25:35 -0400307 // Conservatively flush all writes to the image. We could use a more restricted barrier.
308 // Do not move this above the for loop, otherwise multiple updates can have race conditions
309 // and not be applied correctly as seen i:
310 // dEQP-gles2.functional_texture_specification_texsubimage2d_align_2d* tests on Windows AMD
311 image->changeLayoutWithStages(
312 VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
313 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, commandBuffer);
314
Jamie Madill20fa8d52018-04-15 10:09:32 -0400315 commandBuffer->copyBufferToImage(update.bufferHandle, image->getImage(),
316 image->getCurrentLayout(), 1, &update.copyRegion);
317 }
318
Luc Ferron2f3f4142018-05-30 08:27:19 -0400319 // Only remove the updates that were actually applied to the image.
320 mSubresourceUpdates = std::move(updatesToKeep);
321
322 if (mSubresourceUpdates.empty())
323 {
Jamie Madill21061022018-07-12 23:56:30 -0400324 mStagingBuffer.releaseRetainedBuffers(contextVk->getRenderer());
Luc Ferron2f3f4142018-05-30 08:27:19 -0400325 }
326 else
327 {
328 WARN() << "Internal Vulkan bufffer could not be released. This is likely due to having "
329 "extra images defined in the Texture.";
330 }
Jamie Madill20fa8d52018-04-15 10:09:32 -0400331
Jamie Madill21061022018-07-12 23:56:30 -0400332 return angle::Result::Continue();
Jamie Madill26084d02018-04-09 13:44:04 -0400333}
334
Luc Ferron10434f62018-04-24 10:06:37 -0400335bool PixelBuffer::empty() const
336{
337 return mSubresourceUpdates.empty();
338}
339
Jamie Madill21061022018-07-12 23:56:30 -0400340angle::Result PixelBuffer::stageSubresourceUpdateAndGetData(ContextVk *contextVk,
341 size_t allocationSize,
342 const gl::ImageIndex &imageIndex,
343 const gl::Extents &extents,
344 const gl::Offset &offset,
345 uint8_t **destData)
Luc Ferronc5181702018-05-17 09:44:42 -0400346{
347 VkBuffer bufferHandle;
Jamie Madill4c310832018-08-29 13:43:17 -0400348 VkDeviceSize stagingOffset = 0;
Luc Ferronc5181702018-05-17 09:44:42 -0400349 bool newBufferAllocated = false;
Jamie Madill21061022018-07-12 23:56:30 -0400350 ANGLE_TRY(mStagingBuffer.allocate(contextVk, allocationSize, destData, &bufferHandle,
Luc Ferronc5181702018-05-17 09:44:42 -0400351 &stagingOffset, &newBufferAllocated));
352
353 VkBufferImageCopy copy;
Jamie Madill4c310832018-08-29 13:43:17 -0400354 copy.bufferOffset = stagingOffset;
Luc Ferronc5181702018-05-17 09:44:42 -0400355 copy.bufferRowLength = extents.width;
356 copy.bufferImageHeight = extents.height;
357 copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
358 copy.imageSubresource.mipLevel = imageIndex.getLevelIndex();
359 copy.imageSubresource.baseArrayLayer = imageIndex.hasLayer() ? imageIndex.getLayerIndex() : 0;
360 copy.imageSubresource.layerCount = imageIndex.getLayerCount();
361
362 gl_vk::GetOffset(offset, &copy.imageOffset);
363 gl_vk::GetExtent(extents, &copy.imageExtent);
364
365 mSubresourceUpdates.emplace_back(bufferHandle, copy);
366
Jamie Madill21061022018-07-12 23:56:30 -0400367 return angle::Result::Continue();
Luc Ferronc5181702018-05-17 09:44:42 -0400368}
369
Jamie Madill21061022018-07-12 23:56:30 -0400370angle::Result TextureVk::generateMipmapLevelsWithCPU(ContextVk *contextVk,
371 const angle::Format &sourceFormat,
372 GLuint layer,
373 GLuint firstMipLevel,
374 GLuint maxMipLevel,
375 const size_t sourceWidth,
376 const size_t sourceHeight,
377 const size_t sourceRowPitch,
378 uint8_t *sourceData)
Luc Ferronc5181702018-05-17 09:44:42 -0400379{
Luc Ferronc5181702018-05-17 09:44:42 -0400380 size_t previousLevelWidth = sourceWidth;
381 size_t previousLevelHeight = sourceHeight;
382 uint8_t *previousLevelData = sourceData;
383 size_t previousLevelRowPitch = sourceRowPitch;
384
385 for (GLuint currentMipLevel = firstMipLevel; currentMipLevel <= maxMipLevel; currentMipLevel++)
386 {
387 // Compute next level width and height.
388 size_t mipWidth = std::max<size_t>(1, previousLevelWidth >> 1);
389 size_t mipHeight = std::max<size_t>(1, previousLevelHeight >> 1);
390
391 // With the width and height of the next mip, we can allocate the next buffer we need.
392 uint8_t *destData = nullptr;
393 size_t destRowPitch = mipWidth * sourceFormat.pixelBytes;
394
395 size_t mipAllocationSize = destRowPitch * mipHeight;
396 gl::Extents mipLevelExtents(static_cast<int>(mipWidth), static_cast<int>(mipHeight), 1);
397
398 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateAndGetData(
Jamie Madill21061022018-07-12 23:56:30 -0400399 contextVk, mipAllocationSize,
Luc Ferron22695bf2018-05-22 15:52:08 -0400400 gl::ImageIndex::MakeFromType(mState.getType(), currentMipLevel, layer), mipLevelExtents,
Luc Ferronc5181702018-05-17 09:44:42 -0400401 gl::Offset(), &destData));
402
403 // Generate the mipmap into that new buffer
404 sourceFormat.mipGenerationFunction(previousLevelWidth, previousLevelHeight, 1,
405 previousLevelData, previousLevelRowPitch, 0, destData,
406 destRowPitch, 0);
407
408 // Swap for the next iteration
409 previousLevelWidth = mipWidth;
410 previousLevelHeight = mipHeight;
411 previousLevelData = destData;
412 previousLevelRowPitch = destRowPitch;
413 }
414
Jamie Madill21061022018-07-12 23:56:30 -0400415 return angle::Result::Continue();
Luc Ferronc5181702018-05-17 09:44:42 -0400416}
417
Jamie Madilla7be1f72018-04-13 15:16:26 -0400418PixelBuffer::SubresourceUpdate::SubresourceUpdate() : bufferHandle(VK_NULL_HANDLE)
Jamie Madill20fa8d52018-04-15 10:09:32 -0400419{
420}
421
Jamie Madilla7be1f72018-04-13 15:16:26 -0400422PixelBuffer::SubresourceUpdate::SubresourceUpdate(VkBuffer bufferHandleIn,
423 const VkBufferImageCopy &copyRegionIn)
Jamie Madill20fa8d52018-04-15 10:09:32 -0400424 : bufferHandle(bufferHandleIn), copyRegion(copyRegionIn)
425{
426}
427
Jamie Madilla7be1f72018-04-13 15:16:26 -0400428PixelBuffer::SubresourceUpdate::SubresourceUpdate(const SubresourceUpdate &other) = default;
Jamie Madill20fa8d52018-04-15 10:09:32 -0400429
Jamie Madill26084d02018-04-09 13:44:04 -0400430// TextureVk implementation.
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400431TextureVk::TextureVk(const gl::TextureState &state, RendererVk *renderer)
Jamie Madillbcf467f2018-05-23 09:46:00 -0400432 : TextureImpl(state), mRenderTarget(&mImage, &mBaseLevelImageView, this), mPixelBuffer(renderer)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400433{
434}
435
436TextureVk::~TextureVk()
437{
438}
439
Jamie Madill035fd6b2017-10-03 15:43:22 -0400440gl::Error TextureVk::onDestroy(const gl::Context *context)
441{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400442 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill035fd6b2017-10-03 15:43:22 -0400443 RendererVk *renderer = contextVk->getRenderer();
444
Jamie Madillc4f27e42018-03-31 14:19:18 -0400445 releaseImage(context, renderer);
Jamie Madillc57ee252018-05-30 19:53:48 -0400446 renderer->releaseObject(getStoredQueueSerial(), &mSampler);
Jamie Madill035fd6b2017-10-03 15:43:22 -0400447
Jamie Madilla7be1f72018-04-13 15:16:26 -0400448 mPixelBuffer.release(renderer);
Jamie Madill035fd6b2017-10-03 15:43:22 -0400449 return gl::NoError();
450}
451
Jamie Madillc564c072017-06-01 12:45:42 -0400452gl::Error TextureVk::setImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400453 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400454 GLenum internalFormat,
455 const gl::Extents &size,
456 GLenum format,
457 GLenum type,
458 const gl::PixelUnpackState &unpack,
459 const uint8_t *pixels)
460{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400461 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill1b038242017-11-01 15:14:36 -0400462 RendererVk *renderer = contextVk->getRenderer();
463
Jamie Madillc4f27e42018-03-31 14:19:18 -0400464 // Convert internalFormat to sized internal format.
465 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat, type);
Jamie Madill035fd6b2017-10-03 15:43:22 -0400466
Geoff Langd691aee2018-07-11 16:32:06 -0400467 ANGLE_TRY(redefineImage(context, index, formatInfo, size));
Jamie Madill035fd6b2017-10-03 15:43:22 -0400468
Geoff Langbd6ae4a2018-01-29 15:51:18 -0500469 // Early-out on empty textures, don't create a zero-sized storage.
Jamie Madill26084d02018-04-09 13:44:04 -0400470 if (size.empty())
Geoff Langbd6ae4a2018-01-29 15:51:18 -0500471 {
472 return gl::NoError();
473 }
474
Jamie Madill26084d02018-04-09 13:44:04 -0400475 // Create a new graph node to store image initialization commands.
Jamie Madill316c6062018-05-29 10:49:45 -0400476 onResourceChanged(renderer);
Jamie Madill26084d02018-04-09 13:44:04 -0400477
Jamie Madill035fd6b2017-10-03 15:43:22 -0400478 // Handle initial data.
Jamie Madill035fd6b2017-10-03 15:43:22 -0400479 if (pixels)
480 {
Luc Ferron33e05ba2018-04-23 15:12:34 -0400481 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdate(contextVk, index, size, gl::Offset(),
482 formatInfo, unpack, type, pixels));
Jamie Madill035fd6b2017-10-03 15:43:22 -0400483 }
484
485 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400486}
487
Jamie Madillc564c072017-06-01 12:45:42 -0400488gl::Error TextureVk::setSubImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400489 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400490 const gl::Box &area,
491 GLenum format,
492 GLenum type,
493 const gl::PixelUnpackState &unpack,
494 const uint8_t *pixels)
495{
Jamie Madill5b18f482017-11-30 17:24:22 -0500496 ContextVk *contextVk = vk::GetImpl(context);
497 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, type);
Luc Ferron33e05ba2018-04-23 15:12:34 -0400498 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdate(
499 contextVk, index, gl::Extents(area.width, area.height, area.depth),
500 gl::Offset(area.x, area.y, area.z), formatInfo, unpack, type, pixels));
Jamie Madillb2214862018-04-26 07:25:48 -0400501
502 // Create a new graph node to store image initialization commands.
Jamie Madill316c6062018-05-29 10:49:45 -0400503 onResourceChanged(contextVk->getRenderer());
Jamie Madillb2214862018-04-26 07:25:48 -0400504
Jamie Madill5b18f482017-11-30 17:24:22 -0500505 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400506}
507
Jamie Madillc564c072017-06-01 12:45:42 -0400508gl::Error TextureVk::setCompressedImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400509 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400510 GLenum internalFormat,
511 const gl::Extents &size,
512 const gl::PixelUnpackState &unpack,
513 size_t imageSize,
514 const uint8_t *pixels)
515{
516 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500517 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400518}
519
Jamie Madillc564c072017-06-01 12:45:42 -0400520gl::Error TextureVk::setCompressedSubImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400521 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400522 const gl::Box &area,
523 GLenum format,
524 const gl::PixelUnpackState &unpack,
525 size_t imageSize,
526 const uint8_t *pixels)
527{
528 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500529 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400530}
531
Jamie Madillc564c072017-06-01 12:45:42 -0400532gl::Error TextureVk::copyImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400533 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400534 const gl::Rectangle &sourceArea,
535 GLenum internalFormat,
Jamie Madill690c8eb2018-03-12 15:20:03 -0400536 gl::Framebuffer *source)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400537{
Luc Ferronf299a372018-05-14 14:44:54 -0400538 gl::Extents newImageSize(sourceArea.width, sourceArea.height, 1);
539 const gl::InternalFormat &internalFormatInfo =
540 gl::GetInternalFormatInfo(internalFormat, GL_UNSIGNED_BYTE);
Geoff Langd691aee2018-07-11 16:32:06 -0400541 ANGLE_TRY(redefineImage(context, index, internalFormatInfo, newImageSize));
Luc Ferronf299a372018-05-14 14:44:54 -0400542 return copySubImageImpl(context, index, gl::Offset(0, 0, 0), sourceArea, internalFormatInfo,
543 source);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400544}
545
Jamie Madillc564c072017-06-01 12:45:42 -0400546gl::Error TextureVk::copySubImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400547 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400548 const gl::Offset &destOffset,
549 const gl::Rectangle &sourceArea,
Jamie Madill690c8eb2018-03-12 15:20:03 -0400550 gl::Framebuffer *source)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400551{
Luc Ferronf299a372018-05-14 14:44:54 -0400552 const gl::InternalFormat &currentFormat = *mState.getBaseLevelDesc().format.info;
553 return copySubImageImpl(context, index, destOffset, sourceArea, currentFormat, source);
554}
555
Geoff Langd691aee2018-07-11 16:32:06 -0400556gl::Error TextureVk::copyTexture(const gl::Context *context,
557 const gl::ImageIndex &index,
558 GLenum internalFormat,
559 GLenum type,
560 size_t sourceLevel,
561 bool unpackFlipY,
562 bool unpackPremultiplyAlpha,
563 bool unpackUnmultiplyAlpha,
564 const gl::Texture *source)
565{
566 TextureVk *sourceVk = vk::GetImpl(source);
567 const gl::ImageDesc &sourceImageDesc =
568 sourceVk->mState.getImageDesc(NonCubeTextureTypeToTarget(source->getType()), sourceLevel);
569 gl::Rectangle sourceArea(0, 0, sourceImageDesc.size.width, sourceImageDesc.size.height);
570
571 const gl::InternalFormat &destFormatInfo = gl::GetInternalFormatInfo(internalFormat, type);
572
573 ANGLE_TRY(redefineImage(context, index, destFormatInfo, sourceImageDesc.size));
574
575 return copySubTextureImpl(vk::GetImpl(context), index, gl::kOffsetZero, destFormatInfo,
576 sourceLevel, sourceArea, unpackFlipY, unpackPremultiplyAlpha,
577 unpackUnmultiplyAlpha, sourceVk);
578}
579
580gl::Error TextureVk::copySubTexture(const gl::Context *context,
581 const gl::ImageIndex &index,
582 const gl::Offset &destOffset,
583 size_t sourceLevel,
584 const gl::Rectangle &sourceArea,
585 bool unpackFlipY,
586 bool unpackPremultiplyAlpha,
587 bool unpackUnmultiplyAlpha,
588 const gl::Texture *source)
589{
590 gl::TextureTarget target = index.getTarget();
591 size_t level = static_cast<size_t>(index.getLevelIndex());
592 const gl::InternalFormat &destFormatInfo = *mState.getImageDesc(target, level).format.info;
593 return copySubTextureImpl(vk::GetImpl(context), index, destOffset, destFormatInfo, sourceLevel,
594 sourceArea, unpackFlipY, unpackPremultiplyAlpha,
595 unpackUnmultiplyAlpha, vk::GetImpl(source));
596}
597
Jamie Madill21061022018-07-12 23:56:30 -0400598angle::Result TextureVk::copySubImageImpl(const gl::Context *context,
599 const gl::ImageIndex &index,
600 const gl::Offset &destOffset,
601 const gl::Rectangle &sourceArea,
602 const gl::InternalFormat &internalFormat,
603 gl::Framebuffer *source)
Luc Ferronf299a372018-05-14 14:44:54 -0400604{
Luc Ferron018709f2018-05-10 13:53:11 -0400605 gl::Extents fbSize = source->getReadColorbuffer()->getSize();
606 gl::Rectangle clippedSourceArea;
607 if (!ClipRectangle(sourceArea, gl::Rectangle(0, 0, fbSize.width, fbSize.height),
608 &clippedSourceArea))
609 {
Jamie Madill21061022018-07-12 23:56:30 -0400610 return angle::Result::Continue();
Luc Ferron018709f2018-05-10 13:53:11 -0400611 }
612
613 const gl::Offset modifiedDestOffset(destOffset.x + sourceArea.x - sourceArea.x,
614 destOffset.y + sourceArea.y - sourceArea.y, 0);
615
Frank Henigmand9618bf2018-06-24 19:57:31 -0400616 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill316c6062018-05-29 10:49:45 -0400617 RendererVk *renderer = contextVk->getRenderer();
Luc Ferronf299a372018-05-14 14:44:54 -0400618 FramebufferVk *framebufferVk = vk::GetImpl(source);
Luc Ferron018709f2018-05-10 13:53:11 -0400619
620 // For now, favor conformance. We do a CPU readback that does the conversion, and then stage the
621 // change to the pixel buffer.
622 // Eventually we can improve this easily by implementing vkCmdBlitImage to do the conversion
623 // when its supported.
Jamie Madill58675012018-05-22 14:54:07 -0400624 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateFromFramebuffer(
Luc Ferron018709f2018-05-10 13:53:11 -0400625 context, index, clippedSourceArea, modifiedDestOffset,
Luc Ferronf299a372018-05-14 14:44:54 -0400626 gl::Extents(clippedSourceArea.width, clippedSourceArea.height, 1), internalFormat,
Jamie Madill58675012018-05-22 14:54:07 -0400627 framebufferVk));
Luc Ferron018709f2018-05-10 13:53:11 -0400628
Jamie Madill316c6062018-05-29 10:49:45 -0400629 onResourceChanged(renderer);
630 framebufferVk->addReadDependency(this);
Jamie Madill21061022018-07-12 23:56:30 -0400631 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400632}
633
Geoff Langd691aee2018-07-11 16:32:06 -0400634gl::Error TextureVk::copySubTextureImpl(ContextVk *contextVk,
635 const gl::ImageIndex &index,
636 const gl::Offset &destOffset,
637 const gl::InternalFormat &destFormat,
638 size_t sourceLevel,
639 const gl::Rectangle &sourceArea,
640 bool unpackFlipY,
641 bool unpackPremultiplyAlpha,
642 bool unpackUnmultiplyAlpha,
643 TextureVk *source)
644{
645 RendererVk *renderer = contextVk->getRenderer();
646
647 // Read back the requested region of the source texture
648 uint8_t *sourceData = nullptr;
649 ANGLE_TRY(source->copyImageDataToBuffer(contextVk, sourceLevel, sourceArea, &sourceData));
650
651 ANGLE_TRY(renderer->finish(contextVk));
652
653 // Using the front-end ANGLE format for the colorRead and colorWrite functions. Otherwise
654 // emulated formats like luminance-alpha would not know how to interpret the data.
655 const angle::Format &sourceAngleFormat = source->getImage().getFormat().angleFormat();
656 const angle::Format &destAngleFormat =
657 renderer->getFormat(destFormat.sizedInternalFormat).angleFormat();
658 size_t destinationAllocationSize =
659 sourceArea.width * sourceArea.height * destAngleFormat.pixelBytes;
660
661 // Allocate memory in the destination texture for the copy/conversion
662 uint8_t *destData = nullptr;
663 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateAndGetData(
664 contextVk, destinationAllocationSize, index,
665 gl::Extents(sourceArea.width, sourceArea.height, 1), destOffset, &destData));
666
667 // Source and dest data is tightly packed
668 GLuint sourceDataRowPitch = sourceArea.width * sourceAngleFormat.pixelBytes;
669 GLuint destDataRowPitch = sourceArea.width * destAngleFormat.pixelBytes;
670
671 CopyImageCHROMIUM(sourceData, sourceDataRowPitch, sourceAngleFormat.pixelBytes,
Jamie Madill522095f2018-07-23 14:59:41 -0400672 sourceAngleFormat.pixelReadFunction, destData, destDataRowPitch,
673 destAngleFormat.pixelBytes, destAngleFormat.pixelWriteFunction,
Geoff Langd691aee2018-07-11 16:32:06 -0400674 destFormat.format, destFormat.componentType, sourceArea.width,
675 sourceArea.height, unpackFlipY, unpackPremultiplyAlpha,
676 unpackUnmultiplyAlpha);
677
678 // Create a new graph node to store image initialization commands.
679 onResourceChanged(contextVk->getRenderer());
680
681 return angle::Result::Continue();
682}
683
Jamie Madill21061022018-07-12 23:56:30 -0400684angle::Result TextureVk::getCommandBufferForWrite(ContextVk *contextVk,
685 vk::CommandBuffer **commandBufferOut)
Luc Ferronfa7503c2018-05-08 11:25:06 -0400686{
Jamie Madill21061022018-07-12 23:56:30 -0400687 ANGLE_TRY(appendWriteResource(contextVk, commandBufferOut));
688 return angle::Result::Continue();
Luc Ferronfa7503c2018-05-08 11:25:06 -0400689}
690
Jamie Madillc564c072017-06-01 12:45:42 -0400691gl::Error TextureVk::setStorage(const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500692 gl::TextureType type,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400693 size_t levels,
694 GLenum internalFormat,
695 const gl::Extents &size)
696{
Luc Ferronfa7503c2018-05-08 11:25:06 -0400697 ContextVk *contextVk = GetAs<ContextVk>(context->getImplementation());
698 RendererVk *renderer = contextVk->getRenderer();
699 const vk::Format &format = renderer->getFormat(internalFormat);
700 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400701 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Luc Ferronf6e160f2018-06-12 10:13:57 -0400702 ANGLE_TRY(initImage(contextVk, format, size, static_cast<uint32_t>(levels), commandBuffer));
Luc Ferronfa7503c2018-05-08 11:25:06 -0400703 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400704}
705
Corentin Wallez99d492c2018-02-27 15:17:10 -0500706gl::Error TextureVk::setEGLImageTarget(const gl::Context *context,
707 gl::TextureType type,
708 egl::Image *image)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400709{
710 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500711 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400712}
713
Jamie Madill4928b7c2017-06-20 12:57:39 -0400714gl::Error TextureVk::setImageExternal(const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500715 gl::TextureType type,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400716 egl::Stream *stream,
717 const egl::Stream::GLTextureDescription &desc)
718{
719 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500720 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400721}
722
Geoff Langd691aee2018-07-11 16:32:06 -0400723angle::Result TextureVk::redefineImage(const gl::Context *context,
724 const gl::ImageIndex &index,
725 const gl::InternalFormat &internalFormat,
726 const gl::Extents &size)
727{
728 ContextVk *contextVk = vk::GetImpl(context);
729 RendererVk *renderer = contextVk->getRenderer();
730
731 // If there is any staged changes for this index, we can remove them since we're going to
732 // override them with this call.
733 mPixelBuffer.removeStagedUpdates(index);
734
735 if (mImage.valid())
736 {
737 const vk::Format &vkFormat = renderer->getFormat(internalFormat.sizedInternalFormat);
738
739 // Calculate the expected size for the index we are defining. If the size is different from
740 // the given size, or the format is different, we are redefining the image so we must
741 // release it.
742 if (mImage.getFormat() != vkFormat || size != mImage.getSize(index))
743 {
744 releaseImage(context, renderer);
745 }
746 }
747
748 return angle::Result::Continue();
749}
750
751angle::Result TextureVk::copyImageDataToBuffer(ContextVk *contextVk,
752 size_t sourceLevel,
753 const gl::Rectangle &sourceArea,
754 uint8_t **outDataPtr)
755{
756 if (sourceLevel != 0)
757 {
758 WARN() << "glCopyTextureCHROMIUM with sourceLevel != 0 not implemented.";
759 return angle::Result::Stop();
760 }
761
762 // Make sure the source is initialized and it's images are flushed.
763 ANGLE_TRY(ensureImageInitialized(contextVk));
764
765 const angle::Format &angleFormat = getImage().getFormat().textureFormat();
766 const gl::Extents imageSize =
767 mState.getImageDesc(NonCubeTextureTypeToTarget(mState.getType()), sourceLevel).size;
768 size_t sourceCopyAllocationSize = sourceArea.width * sourceArea.height * angleFormat.pixelBytes;
769
770 vk::CommandBuffer *commandBuffer = nullptr;
771 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
772
773 // Requirement of the copyImageToBuffer, the source image must be in SRC_OPTIMAL layout.
774 bool newBufferAllocated = false;
775 mImage.changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
776 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
777 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer);
778
779 // Allocate enough memory to copy the sourceArea region of the source texture into its pixel
780 // buffer.
781 VkBuffer copyBufferHandle;
Jamie Madill4c310832018-08-29 13:43:17 -0400782 VkDeviceSize sourceCopyOffset = 0;
Geoff Langd691aee2018-07-11 16:32:06 -0400783 ANGLE_TRY(mPixelBuffer.allocate(contextVk, sourceCopyAllocationSize, outDataPtr,
784 &copyBufferHandle, &sourceCopyOffset, &newBufferAllocated));
785
786 VkBufferImageCopy region;
Jamie Madill4c310832018-08-29 13:43:17 -0400787 region.bufferOffset = sourceCopyOffset;
Geoff Langd691aee2018-07-11 16:32:06 -0400788 region.bufferRowLength = imageSize.width;
789 region.bufferImageHeight = imageSize.height;
790 region.imageExtent.width = sourceArea.width;
791 region.imageExtent.height = sourceArea.height;
792 region.imageExtent.depth = 1;
793 region.imageOffset.x = sourceArea.x;
794 region.imageOffset.y = sourceArea.y;
795 region.imageOffset.z = 0;
796 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
797 region.imageSubresource.baseArrayLayer = 0;
798 region.imageSubresource.layerCount = 1;
Jamie Madillc6855862018-07-18 15:06:54 -0400799 region.imageSubresource.mipLevel = static_cast<uint32_t>(sourceLevel);
Geoff Langd691aee2018-07-11 16:32:06 -0400800
801 commandBuffer->copyImageToBuffer(mImage.getImage(), mImage.getCurrentLayout(), copyBufferHandle,
802 1, &region);
803
804 return angle::Result::Continue();
805}
806
Jamie Madill21061022018-07-12 23:56:30 -0400807angle::Result TextureVk::generateMipmapWithBlit(ContextVk *contextVk)
Luc Ferron05cd6df2018-05-24 15:51:29 -0400808{
809 uint32_t imageLayerCount = GetImageLayerCount(mState.getType());
810 const gl::Extents baseLevelExtents = mImage.getExtents();
811 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400812 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Luc Ferron05cd6df2018-05-24 15:51:29 -0400813
814 // We are able to use blitImage since the image format we are using supports it. This
815 // is a faster way we can generate the mips.
816 int32_t mipWidth = baseLevelExtents.width;
817 int32_t mipHeight = baseLevelExtents.height;
818
819 // Manually manage the image memory barrier because it uses a lot more parameters than our
820 // usual one.
821 VkImageMemoryBarrier barrier;
822 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
823 barrier.image = mImage.getImage().getHandle();
824 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
825 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
826 barrier.pNext = nullptr;
827 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
828 barrier.subresourceRange.baseArrayLayer = 0;
829 barrier.subresourceRange.layerCount = imageLayerCount;
830 barrier.subresourceRange.levelCount = 1;
831
832 for (uint32_t mipLevel = 1; mipLevel <= mState.getMipmapMaxLevel(); mipLevel++)
833 {
834 int32_t nextMipWidth = std::max<int32_t>(1, mipWidth >> 1);
835 int32_t nextMipHeight = std::max<int32_t>(1, mipHeight >> 1);
836
837 barrier.subresourceRange.baseMipLevel = mipLevel - 1;
838 barrier.oldLayout = mImage.getCurrentLayout();
839 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
840 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
841 barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
842
843 // We can do it for all layers at once.
844 commandBuffer->singleImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT,
845 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, barrier);
846
847 VkImageBlit blit = {};
848 blit.srcOffsets[0] = {0, 0, 0};
849 blit.srcOffsets[1] = {mipWidth, mipHeight, 1};
850 blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
851 blit.srcSubresource.mipLevel = mipLevel - 1;
852 blit.srcSubresource.baseArrayLayer = 0;
853 blit.srcSubresource.layerCount = imageLayerCount;
854 blit.dstOffsets[0] = {0, 0, 0};
855 blit.dstOffsets[1] = {nextMipWidth, nextMipHeight, 1};
856 blit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
857 blit.dstSubresource.mipLevel = mipLevel;
858 blit.dstSubresource.baseArrayLayer = 0;
859 blit.dstSubresource.layerCount = imageLayerCount;
860
861 mipWidth = nextMipWidth;
862 mipHeight = nextMipHeight;
863
864 commandBuffer->blitImage(mImage.getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
865 mImage.getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit,
866 VK_FILTER_LINEAR);
867 }
868
869 // Transition the last mip level to the same layout as all the other ones, so we can declare
870 // our whole image layout to be SRC_OPTIMAL.
871 barrier.subresourceRange.baseMipLevel = mState.getMipmapMaxLevel();
872 barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
873 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
874
875 // We can do it for all layers at once.
876 commandBuffer->singleImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT,
877 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, barrier);
878
879 // This is just changing the internal state of the image helper so that the next call
880 // to changeLayoutWithStages will use this layout as the "oldLayout" argument.
881 mImage.updateLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Jamie Madilleebe2192018-07-11 09:01:18 -0400882
Jamie Madill21061022018-07-12 23:56:30 -0400883 return angle::Result::Continue();
Luc Ferron05cd6df2018-05-24 15:51:29 -0400884}
885
Jamie Madill21061022018-07-12 23:56:30 -0400886angle::Result TextureVk::generateMipmapWithCPU(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400887{
Luc Ferron22695bf2018-05-22 15:52:08 -0400888 ContextVk *contextVk = vk::GetImpl(context);
889 RendererVk *renderer = contextVk->getRenderer();
Luc Ferronc5181702018-05-17 09:44:42 -0400890
Luc Ferronc5181702018-05-17 09:44:42 -0400891 bool newBufferAllocated = false;
Luc Ferronc5181702018-05-17 09:44:42 -0400892 const gl::Extents baseLevelExtents = mImage.getExtents();
Luc Ferron05cd6df2018-05-24 15:51:29 -0400893 uint32_t imageLayerCount = GetImageLayerCount(mState.getType());
894 const angle::Format &angleFormat = mImage.getFormat().textureFormat();
Luc Ferronc5181702018-05-17 09:44:42 -0400895 GLuint sourceRowPitch = baseLevelExtents.width * angleFormat.pixelBytes;
896 size_t baseLevelAllocationSize = sourceRowPitch * baseLevelExtents.height;
897
Luc Ferron22695bf2018-05-22 15:52:08 -0400898 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400899 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Luc Ferronc5181702018-05-17 09:44:42 -0400900
Luc Ferron22695bf2018-05-22 15:52:08 -0400901 // Requirement of the copyImageToBuffer, the source image must be in SRC_OPTIMAL layout.
902 mImage.changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
903 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
904 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer);
905
906 size_t totalAllocationSize = baseLevelAllocationSize * imageLayerCount;
907
908 VkBuffer copyBufferHandle;
909 uint8_t *baseLevelBuffers;
Jamie Madill4c310832018-08-29 13:43:17 -0400910 VkDeviceSize copyBaseOffset;
Luc Ferron22695bf2018-05-22 15:52:08 -0400911
912 // Allocate enough memory to copy every level 0 image (one for each layer of the texture).
Jamie Madill21061022018-07-12 23:56:30 -0400913 ANGLE_TRY(mPixelBuffer.allocate(contextVk, totalAllocationSize, &baseLevelBuffers,
Luc Ferron22695bf2018-05-22 15:52:08 -0400914 &copyBufferHandle, &copyBaseOffset, &newBufferAllocated));
915
916 // Do only one copy for all layers at once.
Luc Ferronc5181702018-05-17 09:44:42 -0400917 VkBufferImageCopy region;
918 region.bufferImageHeight = baseLevelExtents.height;
Jamie Madill4c310832018-08-29 13:43:17 -0400919 region.bufferOffset = copyBaseOffset;
Luc Ferronc5181702018-05-17 09:44:42 -0400920 region.bufferRowLength = baseLevelExtents.width;
921 region.imageExtent.width = baseLevelExtents.width;
922 region.imageExtent.height = baseLevelExtents.height;
923 region.imageExtent.depth = 1;
924 region.imageOffset.x = 0;
925 region.imageOffset.y = 0;
926 region.imageOffset.z = 0;
927 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
928 region.imageSubresource.baseArrayLayer = 0;
Luc Ferron22695bf2018-05-22 15:52:08 -0400929 region.imageSubresource.layerCount = imageLayerCount;
Luc Ferronc5181702018-05-17 09:44:42 -0400930 region.imageSubresource.mipLevel = mState.getEffectiveBaseLevel();
931
Luc Ferron22695bf2018-05-22 15:52:08 -0400932 commandBuffer->copyImageToBuffer(mImage.getImage(), mImage.getCurrentLayout(), copyBufferHandle,
933 1, &region);
Luc Ferronc5181702018-05-17 09:44:42 -0400934
Jamie Madill21061022018-07-12 23:56:30 -0400935 ANGLE_TRY(renderer->finish(contextVk));
Luc Ferronc5181702018-05-17 09:44:42 -0400936
Luc Ferron2f3f4142018-05-30 08:27:19 -0400937 const uint32_t levelCount = getLevelCount();
938
Luc Ferronc5181702018-05-17 09:44:42 -0400939 // We now have the base level available to be manipulated in the baseLevelBuffer pointer.
940 // Generate all the missing mipmaps with the slow path. We can optimize with vkCmdBlitImage
941 // later.
Luc Ferron22695bf2018-05-22 15:52:08 -0400942 // For each layer, use the copied data to generate all the mips.
943 for (GLuint layer = 0; layer < imageLayerCount; layer++)
944 {
945 size_t bufferOffset = layer * baseLevelAllocationSize;
Luc Ferron05cd6df2018-05-24 15:51:29 -0400946
947 ANGLE_TRY(generateMipmapLevelsWithCPU(
Luc Ferron22695bf2018-05-22 15:52:08 -0400948 contextVk, angleFormat, layer, mState.getEffectiveBaseLevel() + 1,
949 mState.getMipmapMaxLevel(), baseLevelExtents.width, baseLevelExtents.height,
950 sourceRowPitch, baseLevelBuffers + bufferOffset));
951 }
Luc Ferronc5181702018-05-17 09:44:42 -0400952
Jamie Madill21061022018-07-12 23:56:30 -0400953 return mPixelBuffer.flushUpdatesToImage(contextVk, levelCount, &mImage, commandBuffer);
Luc Ferron05cd6df2018-05-24 15:51:29 -0400954}
955
956gl::Error TextureVk::generateMipmap(const gl::Context *context)
957{
958 ContextVk *contextVk = vk::GetImpl(context);
Luc Ferron05cd6df2018-05-24 15:51:29 -0400959
960 // Some data is pending, or the image has not been defined at all yet
961 if (!mImage.valid())
962 {
963 // lets initialize the image so we can generate the next levels.
964 if (!mPixelBuffer.empty())
965 {
Luc Ferronf6e160f2018-06-12 10:13:57 -0400966 ANGLE_TRY(ensureImageInitialized(contextVk));
Luc Ferron05cd6df2018-05-24 15:51:29 -0400967 ASSERT(mImage.valid());
968 }
969 else
970 {
971 // There is nothing to generate if there is nothing uploaded so far.
972 return gl::NoError();
973 }
974 }
975
Luc Ferronf6e160f2018-06-12 10:13:57 -0400976 RendererVk *renderer = contextVk->getRenderer();
Luc Ferron05cd6df2018-05-24 15:51:29 -0400977 VkFormatProperties imageProperties;
978 vk::GetFormatProperties(renderer->getPhysicalDevice(), mImage.getFormat().vkTextureFormat,
979 &imageProperties);
980
981 // Check if the image supports blit. If it does, we can do the mipmap generation on the gpu
982 // only.
983 if (IsMaskFlagSet(kBlitFeatureFlags, imageProperties.linearTilingFeatures))
984 {
Jamie Madill21061022018-07-12 23:56:30 -0400985 ANGLE_TRY(generateMipmapWithBlit(contextVk));
Luc Ferron05cd6df2018-05-24 15:51:29 -0400986 }
987 else
988 {
989 ANGLE_TRY(generateMipmapWithCPU(context));
990 }
991
992 // We're changing this textureVk content, make sure we let the graph know.
993 onResourceChanged(renderer);
994
Luc Ferronc5181702018-05-17 09:44:42 -0400995 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400996}
997
Jamie Madill4928b7c2017-06-20 12:57:39 -0400998gl::Error TextureVk::setBaseLevel(const gl::Context *context, GLuint baseLevel)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400999{
1000 UNIMPLEMENTED();
Jamie Madill4928b7c2017-06-20 12:57:39 -04001001 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001002}
1003
Jamie Madill4928b7c2017-06-20 12:57:39 -04001004gl::Error TextureVk::bindTexImage(const gl::Context *context, egl::Surface *surface)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001005{
1006 UNIMPLEMENTED();
Jamie Madill4928b7c2017-06-20 12:57:39 -04001007 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001008}
1009
Jamie Madill4928b7c2017-06-20 12:57:39 -04001010gl::Error TextureVk::releaseTexImage(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001011{
1012 UNIMPLEMENTED();
Jamie Madill4928b7c2017-06-20 12:57:39 -04001013 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001014}
1015
Jamie Madill4928b7c2017-06-20 12:57:39 -04001016gl::Error TextureVk::getAttachmentRenderTarget(const gl::Context *context,
1017 GLenum binding,
Jamie Madill4fd95d52017-04-05 11:22:18 -04001018 const gl::ImageIndex &imageIndex,
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001019 FramebufferAttachmentRenderTarget **rtOut)
1020{
Luc Ferronfa7503c2018-05-08 11:25:06 -04001021 // TODO(jmadill): Handle cube textures. http://anglebug.com/2470
Jamie Madillcc129372018-04-12 09:13:18 -04001022 ASSERT(imageIndex.getType() == gl::TextureType::_2D);
Jamie Madill26084d02018-04-09 13:44:04 -04001023
1024 // Non-zero mip level attachments are an ES 3.0 feature.
Jamie Madillcc129372018-04-12 09:13:18 -04001025 ASSERT(imageIndex.getLevelIndex() == 0 && !imageIndex.hasLayer());
Jamie Madill26084d02018-04-09 13:44:04 -04001026
1027 ContextVk *contextVk = vk::GetImpl(context);
Luc Ferronf6e160f2018-06-12 10:13:57 -04001028 ANGLE_TRY(ensureImageInitialized(contextVk));
Jamie Madill26084d02018-04-09 13:44:04 -04001029
Jamie Madillb79e7bb2017-10-24 13:55:50 -04001030 *rtOut = &mRenderTarget;
1031 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001032}
1033
Jamie Madill21061022018-07-12 23:56:30 -04001034angle::Result TextureVk::ensureImageInitialized(ContextVk *contextVk)
Jamie Madill26084d02018-04-09 13:44:04 -04001035{
Luc Ferron10434f62018-04-24 10:06:37 -04001036 if (mImage.valid() && mPixelBuffer.empty())
1037 {
Jamie Madill21061022018-07-12 23:56:30 -04001038 return angle::Result::Continue();
Luc Ferron10434f62018-04-24 10:06:37 -04001039 }
Luc Ferronf6e160f2018-06-12 10:13:57 -04001040 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill26084d02018-04-09 13:44:04 -04001041 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001042 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Jamie Madill26084d02018-04-09 13:44:04 -04001043
Luc Ferron2f3f4142018-05-30 08:27:19 -04001044 const gl::ImageDesc &baseLevelDesc = mState.getBaseLevelDesc();
1045 const gl::Extents &baseLevelExtents = baseLevelDesc.size;
1046 const uint32_t levelCount = getLevelCount();
1047
Jamie Madill26084d02018-04-09 13:44:04 -04001048 if (!mImage.valid())
1049 {
Jamie Madill26084d02018-04-09 13:44:04 -04001050 const vk::Format &format =
1051 renderer->getFormat(baseLevelDesc.format.info->sizedInternalFormat);
Jamie Madill26084d02018-04-09 13:44:04 -04001052
Luc Ferronf6e160f2018-06-12 10:13:57 -04001053 ANGLE_TRY(initImage(contextVk, format, baseLevelExtents, levelCount, commandBuffer));
Jamie Madill26084d02018-04-09 13:44:04 -04001054 }
1055
Jamie Madill21061022018-07-12 23:56:30 -04001056 ANGLE_TRY(mPixelBuffer.flushUpdatesToImage(contextVk, levelCount, &mImage, commandBuffer));
1057 return angle::Result::Continue();
Jamie Madill26084d02018-04-09 13:44:04 -04001058}
1059
Luc Ferron4bba74f2018-04-19 14:40:45 -04001060gl::Error TextureVk::syncState(const gl::Context *context, const gl::Texture::DirtyBits &dirtyBits)
Geoff Lang22416862016-06-08 16:14:36 -07001061{
Luc Ferron20610902018-04-19 14:41:13 -04001062 if (dirtyBits.none() && mSampler.valid())
1063 {
1064 return gl::NoError();
1065 }
1066
1067 ContextVk *contextVk = vk::GetImpl(context);
1068 if (mSampler.valid())
1069 {
1070 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc57ee252018-05-30 19:53:48 -04001071 renderer->releaseObject(getStoredQueueSerial(), &mSampler);
Luc Ferron20610902018-04-19 14:41:13 -04001072 }
1073
1074 const gl::SamplerState &samplerState = mState.getSamplerState();
1075
1076 // Create a simple sampler. Force basic parameter settings.
1077 VkSamplerCreateInfo samplerInfo;
1078 samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
1079 samplerInfo.pNext = nullptr;
1080 samplerInfo.flags = 0;
1081 samplerInfo.magFilter = gl_vk::GetFilter(samplerState.magFilter);
1082 samplerInfo.minFilter = gl_vk::GetFilter(samplerState.minFilter);
Luc Ferron66410532018-04-20 12:47:45 -04001083 samplerInfo.mipmapMode = gl_vk::GetSamplerMipmapMode(samplerState.minFilter);
Luc Ferron20610902018-04-19 14:41:13 -04001084 samplerInfo.addressModeU = gl_vk::GetSamplerAddressMode(samplerState.wrapS);
1085 samplerInfo.addressModeV = gl_vk::GetSamplerAddressMode(samplerState.wrapT);
1086 samplerInfo.addressModeW = gl_vk::GetSamplerAddressMode(samplerState.wrapR);
1087 samplerInfo.mipLodBias = 0.0f;
1088 samplerInfo.anisotropyEnable = VK_FALSE;
1089 samplerInfo.maxAnisotropy = 1.0f;
1090 samplerInfo.compareEnable = VK_FALSE;
1091 samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
Luc Ferron66410532018-04-20 12:47:45 -04001092 samplerInfo.minLod = samplerState.minLod;
1093 samplerInfo.maxLod = samplerState.maxLod;
Luc Ferron20610902018-04-19 14:41:13 -04001094 samplerInfo.borderColor = VK_BORDER_COLOR_INT_TRANSPARENT_BLACK;
1095 samplerInfo.unnormalizedCoordinates = VK_FALSE;
1096
Jamie Madill21061022018-07-12 23:56:30 -04001097 ANGLE_TRY(mSampler.init(contextVk, samplerInfo));
Luc Ferron4bba74f2018-04-19 14:40:45 -04001098 return gl::NoError();
Geoff Lang22416862016-06-08 16:14:36 -07001099}
1100
Jamie Madillc564c072017-06-01 12:45:42 -04001101gl::Error TextureVk::setStorageMultisample(const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -05001102 gl::TextureType type,
JiangYizhoubddc46b2016-12-09 09:50:51 +08001103 GLsizei samples,
1104 GLint internalformat,
1105 const gl::Extents &size,
Geoff Lang92019432017-11-20 13:09:34 -05001106 bool fixedSampleLocations)
JiangYizhoubddc46b2016-12-09 09:50:51 +08001107{
1108 UNIMPLEMENTED();
1109 return gl::InternalError() << "setStorageMultisample is unimplemented.";
1110}
1111
Jamie Madill05b35b22017-10-03 09:01:44 -04001112gl::Error TextureVk::initializeContents(const gl::Context *context,
1113 const gl::ImageIndex &imageIndex)
1114{
1115 UNIMPLEMENTED();
1116 return gl::NoError();
1117}
1118
Jamie Madill858c1cc2018-03-31 14:19:13 -04001119const vk::ImageHelper &TextureVk::getImage() const
Jamie Madill5547b382017-10-23 18:16:01 -04001120{
1121 ASSERT(mImage.valid());
Jamie Madill858c1cc2018-03-31 14:19:13 -04001122 return mImage;
Jamie Madill5547b382017-10-23 18:16:01 -04001123}
1124
1125const vk::ImageView &TextureVk::getImageView() const
1126{
Jamie Madill93edca12018-03-30 10:43:18 -04001127 ASSERT(mImage.valid());
Luc Ferron66410532018-04-20 12:47:45 -04001128
1129 const GLenum minFilter = mState.getSamplerState().minFilter;
1130 if (minFilter == GL_LINEAR || minFilter == GL_NEAREST)
1131 {
1132 return mBaseLevelImageView;
1133 }
1134
1135 return mMipmapImageView;
Jamie Madill5547b382017-10-23 18:16:01 -04001136}
1137
1138const vk::Sampler &TextureVk::getSampler() const
1139{
1140 ASSERT(mSampler.valid());
1141 return mSampler;
1142}
1143
Jamie Madill21061022018-07-12 23:56:30 -04001144angle::Result TextureVk::initImage(ContextVk *contextVk,
1145 const vk::Format &format,
1146 const gl::Extents &extents,
1147 const uint32_t levelCount,
1148 vk::CommandBuffer *commandBuffer)
Luc Ferronfa7503c2018-05-08 11:25:06 -04001149{
Luc Ferronf6e160f2018-06-12 10:13:57 -04001150 const RendererVk *renderer = contextVk->getRenderer();
Luc Ferronfa7503c2018-05-08 11:25:06 -04001151
1152 const VkImageUsageFlags usage =
1153 (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1154 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT);
1155
Jamie Madill21061022018-07-12 23:56:30 -04001156 ANGLE_TRY(mImage.init(contextVk, mState.getType(), extents, format, 1, usage, levelCount));
Luc Ferronfa7503c2018-05-08 11:25:06 -04001157
1158 const VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
1159
Jamie Madill21061022018-07-12 23:56:30 -04001160 ANGLE_TRY(mImage.initMemory(contextVk, renderer->getMemoryProperties(), flags));
Luc Ferronfa7503c2018-05-08 11:25:06 -04001161
1162 gl::SwizzleState mappedSwizzle;
1163 MapSwizzleState(format.internalFormat, mState.getSwizzleState(), &mappedSwizzle);
1164
Luc Ferronf6e160f2018-06-12 10:13:57 -04001165 // Renderable textures cannot have a swizzle.
1166 ASSERT(!contextVk->getTextureCaps().get(format.internalFormat).textureAttachment ||
1167 !mappedSwizzle.swizzleRequired());
1168
Luc Ferronfa7503c2018-05-08 11:25:06 -04001169 // TODO(jmadill): Separate imageviews for RenderTargets and Sampling.
Jamie Madill21061022018-07-12 23:56:30 -04001170 ANGLE_TRY(mImage.initImageView(contextVk, mState.getType(), VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferronfa7503c2018-05-08 11:25:06 -04001171 mappedSwizzle, &mMipmapImageView, levelCount));
Jamie Madill21061022018-07-12 23:56:30 -04001172 ANGLE_TRY(mImage.initImageView(contextVk, mState.getType(), VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferronfa7503c2018-05-08 11:25:06 -04001173 mappedSwizzle, &mBaseLevelImageView, 1));
1174
1175 // TODO(jmadill): Fold this into the RenderPass load/store ops. http://anglebug.com/2361
Luc Ferron7348fc52018-05-09 07:17:16 -04001176 VkClearColorValue black = {{0, 0, 0, 1.0f}};
Luc Ferronc20b9502018-05-24 09:30:17 -04001177 mImage.clearColor(black, 0, levelCount, commandBuffer);
Jamie Madill21061022018-07-12 23:56:30 -04001178 return angle::Result::Continue();
Luc Ferronfa7503c2018-05-08 11:25:06 -04001179}
1180
Jamie Madillc4f27e42018-03-31 14:19:18 -04001181void TextureVk::releaseImage(const gl::Context *context, RendererVk *renderer)
1182{
1183 mImage.release(renderer->getCurrentQueueSerial(), renderer);
Jamie Madillc57ee252018-05-30 19:53:48 -04001184 renderer->releaseObject(getStoredQueueSerial(), &mBaseLevelImageView);
1185 renderer->releaseObject(getStoredQueueSerial(), &mMipmapImageView);
Jamie Madillc4f27e42018-03-31 14:19:18 -04001186 onStateChange(context, angle::SubjectMessage::DEPENDENT_DIRTY_BITS);
1187}
1188
Luc Ferron66410532018-04-20 12:47:45 -04001189uint32_t TextureVk::getLevelCount() const
1190{
1191 ASSERT(mState.getEffectiveBaseLevel() == 0);
1192
1193 // getMipmapMaxLevel will be 0 here if mipmaps are not used, so the levelCount is always +1.
1194 return mState.getMipmapMaxLevel() + 1;
1195}
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001196} // namespace rx