blob: 58ee7e1f508115af74513a7238e4c6874b9b4fc9 [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 Madill7f232932018-09-12 11:03:06 -0400129 uint8_t *stagingPointer = nullptr;
130 bool newBufferAllocated = false;
Jamie Madill4c310832018-08-29 13:43:17 -0400131 VkDeviceSize stagingOffset = 0;
Jamie Madill7f232932018-09-12 11:03:06 -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
Jamie Madill7f232932018-09-12 11:03:06 -0400200 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;
Jamie Madill7f232932018-09-12 11:03:06 -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,
Jamie Madill0d0fb432018-09-07 17:43:32 -0400494 gl::Buffer *unpackBuffer,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400495 const uint8_t *pixels)
496{
Jamie Madill5b18f482017-11-30 17:24:22 -0500497 ContextVk *contextVk = vk::GetImpl(context);
498 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, type);
Luc Ferron33e05ba2018-04-23 15:12:34 -0400499 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdate(
500 contextVk, index, gl::Extents(area.width, area.height, area.depth),
501 gl::Offset(area.x, area.y, area.z), formatInfo, unpack, type, pixels));
Jamie Madillb2214862018-04-26 07:25:48 -0400502
503 // Create a new graph node to store image initialization commands.
Jamie Madill316c6062018-05-29 10:49:45 -0400504 onResourceChanged(contextVk->getRenderer());
Jamie Madillb2214862018-04-26 07:25:48 -0400505
Jamie Madill5b18f482017-11-30 17:24:22 -0500506 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400507}
508
Jamie Madillc564c072017-06-01 12:45:42 -0400509gl::Error TextureVk::setCompressedImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400510 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400511 GLenum internalFormat,
512 const gl::Extents &size,
513 const gl::PixelUnpackState &unpack,
514 size_t imageSize,
515 const uint8_t *pixels)
516{
517 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500518 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400519}
520
Jamie Madillc564c072017-06-01 12:45:42 -0400521gl::Error TextureVk::setCompressedSubImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400522 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400523 const gl::Box &area,
524 GLenum format,
525 const gl::PixelUnpackState &unpack,
526 size_t imageSize,
527 const uint8_t *pixels)
528{
529 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500530 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400531}
532
Jamie Madillc564c072017-06-01 12:45:42 -0400533gl::Error TextureVk::copyImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400534 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400535 const gl::Rectangle &sourceArea,
536 GLenum internalFormat,
Jamie Madill690c8eb2018-03-12 15:20:03 -0400537 gl::Framebuffer *source)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400538{
Luc Ferronf299a372018-05-14 14:44:54 -0400539 gl::Extents newImageSize(sourceArea.width, sourceArea.height, 1);
540 const gl::InternalFormat &internalFormatInfo =
541 gl::GetInternalFormatInfo(internalFormat, GL_UNSIGNED_BYTE);
Geoff Langd691aee2018-07-11 16:32:06 -0400542 ANGLE_TRY(redefineImage(context, index, internalFormatInfo, newImageSize));
Luc Ferronf299a372018-05-14 14:44:54 -0400543 return copySubImageImpl(context, index, gl::Offset(0, 0, 0), sourceArea, internalFormatInfo,
544 source);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400545}
546
Jamie Madillc564c072017-06-01 12:45:42 -0400547gl::Error TextureVk::copySubImage(const gl::Context *context,
Jamie Madillc4f27e42018-03-31 14:19:18 -0400548 const gl::ImageIndex &index,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400549 const gl::Offset &destOffset,
550 const gl::Rectangle &sourceArea,
Jamie Madill690c8eb2018-03-12 15:20:03 -0400551 gl::Framebuffer *source)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400552{
Luc Ferronf299a372018-05-14 14:44:54 -0400553 const gl::InternalFormat &currentFormat = *mState.getBaseLevelDesc().format.info;
554 return copySubImageImpl(context, index, destOffset, sourceArea, currentFormat, source);
555}
556
Geoff Langd691aee2018-07-11 16:32:06 -0400557gl::Error TextureVk::copyTexture(const gl::Context *context,
558 const gl::ImageIndex &index,
559 GLenum internalFormat,
560 GLenum type,
561 size_t sourceLevel,
562 bool unpackFlipY,
563 bool unpackPremultiplyAlpha,
564 bool unpackUnmultiplyAlpha,
565 const gl::Texture *source)
566{
567 TextureVk *sourceVk = vk::GetImpl(source);
568 const gl::ImageDesc &sourceImageDesc =
569 sourceVk->mState.getImageDesc(NonCubeTextureTypeToTarget(source->getType()), sourceLevel);
570 gl::Rectangle sourceArea(0, 0, sourceImageDesc.size.width, sourceImageDesc.size.height);
571
572 const gl::InternalFormat &destFormatInfo = gl::GetInternalFormatInfo(internalFormat, type);
573
574 ANGLE_TRY(redefineImage(context, index, destFormatInfo, sourceImageDesc.size));
575
576 return copySubTextureImpl(vk::GetImpl(context), index, gl::kOffsetZero, destFormatInfo,
577 sourceLevel, sourceArea, unpackFlipY, unpackPremultiplyAlpha,
578 unpackUnmultiplyAlpha, sourceVk);
579}
580
581gl::Error TextureVk::copySubTexture(const gl::Context *context,
582 const gl::ImageIndex &index,
583 const gl::Offset &destOffset,
584 size_t sourceLevel,
585 const gl::Rectangle &sourceArea,
586 bool unpackFlipY,
587 bool unpackPremultiplyAlpha,
588 bool unpackUnmultiplyAlpha,
589 const gl::Texture *source)
590{
591 gl::TextureTarget target = index.getTarget();
592 size_t level = static_cast<size_t>(index.getLevelIndex());
593 const gl::InternalFormat &destFormatInfo = *mState.getImageDesc(target, level).format.info;
594 return copySubTextureImpl(vk::GetImpl(context), index, destOffset, destFormatInfo, sourceLevel,
595 sourceArea, unpackFlipY, unpackPremultiplyAlpha,
596 unpackUnmultiplyAlpha, vk::GetImpl(source));
597}
598
Jamie Madill21061022018-07-12 23:56:30 -0400599angle::Result TextureVk::copySubImageImpl(const gl::Context *context,
600 const gl::ImageIndex &index,
601 const gl::Offset &destOffset,
602 const gl::Rectangle &sourceArea,
603 const gl::InternalFormat &internalFormat,
604 gl::Framebuffer *source)
Luc Ferronf299a372018-05-14 14:44:54 -0400605{
Luc Ferron018709f2018-05-10 13:53:11 -0400606 gl::Extents fbSize = source->getReadColorbuffer()->getSize();
607 gl::Rectangle clippedSourceArea;
608 if (!ClipRectangle(sourceArea, gl::Rectangle(0, 0, fbSize.width, fbSize.height),
609 &clippedSourceArea))
610 {
Jamie Madill21061022018-07-12 23:56:30 -0400611 return angle::Result::Continue();
Luc Ferron018709f2018-05-10 13:53:11 -0400612 }
613
614 const gl::Offset modifiedDestOffset(destOffset.x + sourceArea.x - sourceArea.x,
615 destOffset.y + sourceArea.y - sourceArea.y, 0);
616
Frank Henigmand9618bf2018-06-24 19:57:31 -0400617 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill316c6062018-05-29 10:49:45 -0400618 RendererVk *renderer = contextVk->getRenderer();
Luc Ferronf299a372018-05-14 14:44:54 -0400619 FramebufferVk *framebufferVk = vk::GetImpl(source);
Luc Ferron018709f2018-05-10 13:53:11 -0400620
621 // For now, favor conformance. We do a CPU readback that does the conversion, and then stage the
622 // change to the pixel buffer.
623 // Eventually we can improve this easily by implementing vkCmdBlitImage to do the conversion
624 // when its supported.
Jamie Madill58675012018-05-22 14:54:07 -0400625 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateFromFramebuffer(
Luc Ferron018709f2018-05-10 13:53:11 -0400626 context, index, clippedSourceArea, modifiedDestOffset,
Luc Ferronf299a372018-05-14 14:44:54 -0400627 gl::Extents(clippedSourceArea.width, clippedSourceArea.height, 1), internalFormat,
Jamie Madill58675012018-05-22 14:54:07 -0400628 framebufferVk));
Luc Ferron018709f2018-05-10 13:53:11 -0400629
Jamie Madill316c6062018-05-29 10:49:45 -0400630 onResourceChanged(renderer);
631 framebufferVk->addReadDependency(this);
Jamie Madill21061022018-07-12 23:56:30 -0400632 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400633}
634
Geoff Langd691aee2018-07-11 16:32:06 -0400635gl::Error TextureVk::copySubTextureImpl(ContextVk *contextVk,
636 const gl::ImageIndex &index,
637 const gl::Offset &destOffset,
638 const gl::InternalFormat &destFormat,
639 size_t sourceLevel,
640 const gl::Rectangle &sourceArea,
641 bool unpackFlipY,
642 bool unpackPremultiplyAlpha,
643 bool unpackUnmultiplyAlpha,
644 TextureVk *source)
645{
646 RendererVk *renderer = contextVk->getRenderer();
647
648 // Read back the requested region of the source texture
649 uint8_t *sourceData = nullptr;
650 ANGLE_TRY(source->copyImageDataToBuffer(contextVk, sourceLevel, sourceArea, &sourceData));
651
652 ANGLE_TRY(renderer->finish(contextVk));
653
654 // Using the front-end ANGLE format for the colorRead and colorWrite functions. Otherwise
655 // emulated formats like luminance-alpha would not know how to interpret the data.
656 const angle::Format &sourceAngleFormat = source->getImage().getFormat().angleFormat();
657 const angle::Format &destAngleFormat =
658 renderer->getFormat(destFormat.sizedInternalFormat).angleFormat();
659 size_t destinationAllocationSize =
660 sourceArea.width * sourceArea.height * destAngleFormat.pixelBytes;
661
662 // Allocate memory in the destination texture for the copy/conversion
663 uint8_t *destData = nullptr;
664 ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateAndGetData(
665 contextVk, destinationAllocationSize, index,
666 gl::Extents(sourceArea.width, sourceArea.height, 1), destOffset, &destData));
667
668 // Source and dest data is tightly packed
669 GLuint sourceDataRowPitch = sourceArea.width * sourceAngleFormat.pixelBytes;
670 GLuint destDataRowPitch = sourceArea.width * destAngleFormat.pixelBytes;
671
672 CopyImageCHROMIUM(sourceData, sourceDataRowPitch, sourceAngleFormat.pixelBytes,
Jamie Madill522095f2018-07-23 14:59:41 -0400673 sourceAngleFormat.pixelReadFunction, destData, destDataRowPitch,
674 destAngleFormat.pixelBytes, destAngleFormat.pixelWriteFunction,
Geoff Langd691aee2018-07-11 16:32:06 -0400675 destFormat.format, destFormat.componentType, sourceArea.width,
676 sourceArea.height, unpackFlipY, unpackPremultiplyAlpha,
677 unpackUnmultiplyAlpha);
678
679 // Create a new graph node to store image initialization commands.
680 onResourceChanged(contextVk->getRenderer());
681
682 return angle::Result::Continue();
683}
684
Jamie Madill21061022018-07-12 23:56:30 -0400685angle::Result TextureVk::getCommandBufferForWrite(ContextVk *contextVk,
686 vk::CommandBuffer **commandBufferOut)
Luc Ferronfa7503c2018-05-08 11:25:06 -0400687{
Jamie Madill21061022018-07-12 23:56:30 -0400688 ANGLE_TRY(appendWriteResource(contextVk, commandBufferOut));
689 return angle::Result::Continue();
Luc Ferronfa7503c2018-05-08 11:25:06 -0400690}
691
Jamie Madillc564c072017-06-01 12:45:42 -0400692gl::Error TextureVk::setStorage(const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500693 gl::TextureType type,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400694 size_t levels,
695 GLenum internalFormat,
696 const gl::Extents &size)
697{
Luc Ferronfa7503c2018-05-08 11:25:06 -0400698 ContextVk *contextVk = GetAs<ContextVk>(context->getImplementation());
699 RendererVk *renderer = contextVk->getRenderer();
700 const vk::Format &format = renderer->getFormat(internalFormat);
701 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400702 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Jamie Madill3e29cf32018-08-31 17:19:17 -0400703
704 if (mImage.valid())
705 {
706 releaseImage(context, renderer);
707 }
708
Luc Ferronf6e160f2018-06-12 10:13:57 -0400709 ANGLE_TRY(initImage(contextVk, format, size, static_cast<uint32_t>(levels), commandBuffer));
Luc Ferronfa7503c2018-05-08 11:25:06 -0400710 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400711}
712
Corentin Wallez99d492c2018-02-27 15:17:10 -0500713gl::Error TextureVk::setEGLImageTarget(const gl::Context *context,
714 gl::TextureType type,
715 egl::Image *image)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400716{
717 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500718 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400719}
720
Jamie Madill4928b7c2017-06-20 12:57:39 -0400721gl::Error TextureVk::setImageExternal(const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -0500722 gl::TextureType type,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400723 egl::Stream *stream,
724 const egl::Stream::GLTextureDescription &desc)
725{
726 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500727 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400728}
729
Geoff Langd691aee2018-07-11 16:32:06 -0400730angle::Result TextureVk::redefineImage(const gl::Context *context,
731 const gl::ImageIndex &index,
732 const gl::InternalFormat &internalFormat,
733 const gl::Extents &size)
734{
735 ContextVk *contextVk = vk::GetImpl(context);
736 RendererVk *renderer = contextVk->getRenderer();
737
738 // If there is any staged changes for this index, we can remove them since we're going to
739 // override them with this call.
740 mPixelBuffer.removeStagedUpdates(index);
741
742 if (mImage.valid())
743 {
744 const vk::Format &vkFormat = renderer->getFormat(internalFormat.sizedInternalFormat);
745
746 // Calculate the expected size for the index we are defining. If the size is different from
747 // the given size, or the format is different, we are redefining the image so we must
748 // release it.
749 if (mImage.getFormat() != vkFormat || size != mImage.getSize(index))
750 {
751 releaseImage(context, renderer);
752 }
753 }
754
755 return angle::Result::Continue();
756}
757
758angle::Result TextureVk::copyImageDataToBuffer(ContextVk *contextVk,
759 size_t sourceLevel,
760 const gl::Rectangle &sourceArea,
761 uint8_t **outDataPtr)
762{
763 if (sourceLevel != 0)
764 {
765 WARN() << "glCopyTextureCHROMIUM with sourceLevel != 0 not implemented.";
766 return angle::Result::Stop();
767 }
768
769 // Make sure the source is initialized and it's images are flushed.
770 ANGLE_TRY(ensureImageInitialized(contextVk));
771
772 const angle::Format &angleFormat = getImage().getFormat().textureFormat();
773 const gl::Extents imageSize =
774 mState.getImageDesc(NonCubeTextureTypeToTarget(mState.getType()), sourceLevel).size;
775 size_t sourceCopyAllocationSize = sourceArea.width * sourceArea.height * angleFormat.pixelBytes;
776
777 vk::CommandBuffer *commandBuffer = nullptr;
778 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
779
780 // Requirement of the copyImageToBuffer, the source image must be in SRC_OPTIMAL layout.
781 bool newBufferAllocated = false;
782 mImage.changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
783 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
784 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer);
785
786 // Allocate enough memory to copy the sourceArea region of the source texture into its pixel
787 // buffer.
788 VkBuffer copyBufferHandle;
Jamie Madill4c310832018-08-29 13:43:17 -0400789 VkDeviceSize sourceCopyOffset = 0;
Geoff Langd691aee2018-07-11 16:32:06 -0400790 ANGLE_TRY(mPixelBuffer.allocate(contextVk, sourceCopyAllocationSize, outDataPtr,
791 &copyBufferHandle, &sourceCopyOffset, &newBufferAllocated));
792
793 VkBufferImageCopy region;
Jamie Madill4c310832018-08-29 13:43:17 -0400794 region.bufferOffset = sourceCopyOffset;
Geoff Langd691aee2018-07-11 16:32:06 -0400795 region.bufferRowLength = imageSize.width;
796 region.bufferImageHeight = imageSize.height;
797 region.imageExtent.width = sourceArea.width;
798 region.imageExtent.height = sourceArea.height;
799 region.imageExtent.depth = 1;
800 region.imageOffset.x = sourceArea.x;
801 region.imageOffset.y = sourceArea.y;
802 region.imageOffset.z = 0;
803 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
804 region.imageSubresource.baseArrayLayer = 0;
805 region.imageSubresource.layerCount = 1;
Jamie Madillc6855862018-07-18 15:06:54 -0400806 region.imageSubresource.mipLevel = static_cast<uint32_t>(sourceLevel);
Geoff Langd691aee2018-07-11 16:32:06 -0400807
808 commandBuffer->copyImageToBuffer(mImage.getImage(), mImage.getCurrentLayout(), copyBufferHandle,
809 1, &region);
810
811 return angle::Result::Continue();
812}
813
Jamie Madill21061022018-07-12 23:56:30 -0400814angle::Result TextureVk::generateMipmapWithBlit(ContextVk *contextVk)
Luc Ferron05cd6df2018-05-24 15:51:29 -0400815{
816 uint32_t imageLayerCount = GetImageLayerCount(mState.getType());
817 const gl::Extents baseLevelExtents = mImage.getExtents();
818 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400819 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Luc Ferron05cd6df2018-05-24 15:51:29 -0400820
821 // We are able to use blitImage since the image format we are using supports it. This
822 // is a faster way we can generate the mips.
823 int32_t mipWidth = baseLevelExtents.width;
824 int32_t mipHeight = baseLevelExtents.height;
825
826 // Manually manage the image memory barrier because it uses a lot more parameters than our
827 // usual one.
828 VkImageMemoryBarrier barrier;
829 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
830 barrier.image = mImage.getImage().getHandle();
831 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
832 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
833 barrier.pNext = nullptr;
834 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
835 barrier.subresourceRange.baseArrayLayer = 0;
836 barrier.subresourceRange.layerCount = imageLayerCount;
837 barrier.subresourceRange.levelCount = 1;
838
839 for (uint32_t mipLevel = 1; mipLevel <= mState.getMipmapMaxLevel(); mipLevel++)
840 {
841 int32_t nextMipWidth = std::max<int32_t>(1, mipWidth >> 1);
842 int32_t nextMipHeight = std::max<int32_t>(1, mipHeight >> 1);
843
844 barrier.subresourceRange.baseMipLevel = mipLevel - 1;
845 barrier.oldLayout = mImage.getCurrentLayout();
846 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
847 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
848 barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
849
850 // We can do it for all layers at once.
851 commandBuffer->singleImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT,
852 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, barrier);
853
854 VkImageBlit blit = {};
855 blit.srcOffsets[0] = {0, 0, 0};
856 blit.srcOffsets[1] = {mipWidth, mipHeight, 1};
857 blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
858 blit.srcSubresource.mipLevel = mipLevel - 1;
859 blit.srcSubresource.baseArrayLayer = 0;
860 blit.srcSubresource.layerCount = imageLayerCount;
861 blit.dstOffsets[0] = {0, 0, 0};
862 blit.dstOffsets[1] = {nextMipWidth, nextMipHeight, 1};
863 blit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
864 blit.dstSubresource.mipLevel = mipLevel;
865 blit.dstSubresource.baseArrayLayer = 0;
866 blit.dstSubresource.layerCount = imageLayerCount;
867
868 mipWidth = nextMipWidth;
869 mipHeight = nextMipHeight;
870
871 commandBuffer->blitImage(mImage.getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
872 mImage.getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit,
873 VK_FILTER_LINEAR);
874 }
875
876 // Transition the last mip level to the same layout as all the other ones, so we can declare
877 // our whole image layout to be SRC_OPTIMAL.
878 barrier.subresourceRange.baseMipLevel = mState.getMipmapMaxLevel();
879 barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
880 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
881
882 // We can do it for all layers at once.
883 commandBuffer->singleImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT,
884 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, barrier);
885
886 // This is just changing the internal state of the image helper so that the next call
887 // to changeLayoutWithStages will use this layout as the "oldLayout" argument.
888 mImage.updateLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Jamie Madilleebe2192018-07-11 09:01:18 -0400889
Jamie Madill21061022018-07-12 23:56:30 -0400890 return angle::Result::Continue();
Luc Ferron05cd6df2018-05-24 15:51:29 -0400891}
892
Jamie Madill21061022018-07-12 23:56:30 -0400893angle::Result TextureVk::generateMipmapWithCPU(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400894{
Luc Ferron22695bf2018-05-22 15:52:08 -0400895 ContextVk *contextVk = vk::GetImpl(context);
896 RendererVk *renderer = contextVk->getRenderer();
Luc Ferronc5181702018-05-17 09:44:42 -0400897
Luc Ferronc5181702018-05-17 09:44:42 -0400898 bool newBufferAllocated = false;
Luc Ferronc5181702018-05-17 09:44:42 -0400899 const gl::Extents baseLevelExtents = mImage.getExtents();
Luc Ferron05cd6df2018-05-24 15:51:29 -0400900 uint32_t imageLayerCount = GetImageLayerCount(mState.getType());
901 const angle::Format &angleFormat = mImage.getFormat().textureFormat();
Luc Ferronc5181702018-05-17 09:44:42 -0400902 GLuint sourceRowPitch = baseLevelExtents.width * angleFormat.pixelBytes;
903 size_t baseLevelAllocationSize = sourceRowPitch * baseLevelExtents.height;
904
Luc Ferron22695bf2018-05-22 15:52:08 -0400905 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400906 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Luc Ferronc5181702018-05-17 09:44:42 -0400907
Luc Ferron22695bf2018-05-22 15:52:08 -0400908 // Requirement of the copyImageToBuffer, the source image must be in SRC_OPTIMAL layout.
909 mImage.changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
910 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
911 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer);
912
913 size_t totalAllocationSize = baseLevelAllocationSize * imageLayerCount;
914
915 VkBuffer copyBufferHandle;
916 uint8_t *baseLevelBuffers;
Jamie Madill4c310832018-08-29 13:43:17 -0400917 VkDeviceSize copyBaseOffset;
Luc Ferron22695bf2018-05-22 15:52:08 -0400918
919 // Allocate enough memory to copy every level 0 image (one for each layer of the texture).
Jamie Madill21061022018-07-12 23:56:30 -0400920 ANGLE_TRY(mPixelBuffer.allocate(contextVk, totalAllocationSize, &baseLevelBuffers,
Luc Ferron22695bf2018-05-22 15:52:08 -0400921 &copyBufferHandle, &copyBaseOffset, &newBufferAllocated));
922
923 // Do only one copy for all layers at once.
Luc Ferronc5181702018-05-17 09:44:42 -0400924 VkBufferImageCopy region;
925 region.bufferImageHeight = baseLevelExtents.height;
Jamie Madill4c310832018-08-29 13:43:17 -0400926 region.bufferOffset = copyBaseOffset;
Luc Ferronc5181702018-05-17 09:44:42 -0400927 region.bufferRowLength = baseLevelExtents.width;
928 region.imageExtent.width = baseLevelExtents.width;
929 region.imageExtent.height = baseLevelExtents.height;
930 region.imageExtent.depth = 1;
931 region.imageOffset.x = 0;
932 region.imageOffset.y = 0;
933 region.imageOffset.z = 0;
934 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
935 region.imageSubresource.baseArrayLayer = 0;
Luc Ferron22695bf2018-05-22 15:52:08 -0400936 region.imageSubresource.layerCount = imageLayerCount;
Luc Ferronc5181702018-05-17 09:44:42 -0400937 region.imageSubresource.mipLevel = mState.getEffectiveBaseLevel();
938
Luc Ferron22695bf2018-05-22 15:52:08 -0400939 commandBuffer->copyImageToBuffer(mImage.getImage(), mImage.getCurrentLayout(), copyBufferHandle,
940 1, &region);
Luc Ferronc5181702018-05-17 09:44:42 -0400941
Jamie Madill21061022018-07-12 23:56:30 -0400942 ANGLE_TRY(renderer->finish(contextVk));
Luc Ferronc5181702018-05-17 09:44:42 -0400943
Luc Ferron2f3f4142018-05-30 08:27:19 -0400944 const uint32_t levelCount = getLevelCount();
945
Luc Ferronc5181702018-05-17 09:44:42 -0400946 // We now have the base level available to be manipulated in the baseLevelBuffer pointer.
947 // Generate all the missing mipmaps with the slow path. We can optimize with vkCmdBlitImage
948 // later.
Luc Ferron22695bf2018-05-22 15:52:08 -0400949 // For each layer, use the copied data to generate all the mips.
950 for (GLuint layer = 0; layer < imageLayerCount; layer++)
951 {
952 size_t bufferOffset = layer * baseLevelAllocationSize;
Luc Ferron05cd6df2018-05-24 15:51:29 -0400953
954 ANGLE_TRY(generateMipmapLevelsWithCPU(
Luc Ferron22695bf2018-05-22 15:52:08 -0400955 contextVk, angleFormat, layer, mState.getEffectiveBaseLevel() + 1,
956 mState.getMipmapMaxLevel(), baseLevelExtents.width, baseLevelExtents.height,
957 sourceRowPitch, baseLevelBuffers + bufferOffset));
958 }
Luc Ferronc5181702018-05-17 09:44:42 -0400959
Jamie Madill21061022018-07-12 23:56:30 -0400960 return mPixelBuffer.flushUpdatesToImage(contextVk, levelCount, &mImage, commandBuffer);
Luc Ferron05cd6df2018-05-24 15:51:29 -0400961}
962
963gl::Error TextureVk::generateMipmap(const gl::Context *context)
964{
965 ContextVk *contextVk = vk::GetImpl(context);
Luc Ferron05cd6df2018-05-24 15:51:29 -0400966
967 // Some data is pending, or the image has not been defined at all yet
968 if (!mImage.valid())
969 {
970 // lets initialize the image so we can generate the next levels.
971 if (!mPixelBuffer.empty())
972 {
Luc Ferronf6e160f2018-06-12 10:13:57 -0400973 ANGLE_TRY(ensureImageInitialized(contextVk));
Luc Ferron05cd6df2018-05-24 15:51:29 -0400974 ASSERT(mImage.valid());
975 }
976 else
977 {
978 // There is nothing to generate if there is nothing uploaded so far.
979 return gl::NoError();
980 }
981 }
982
Luc Ferronf6e160f2018-06-12 10:13:57 -0400983 RendererVk *renderer = contextVk->getRenderer();
Luc Ferron05cd6df2018-05-24 15:51:29 -0400984 VkFormatProperties imageProperties;
985 vk::GetFormatProperties(renderer->getPhysicalDevice(), mImage.getFormat().vkTextureFormat,
986 &imageProperties);
987
988 // Check if the image supports blit. If it does, we can do the mipmap generation on the gpu
989 // only.
990 if (IsMaskFlagSet(kBlitFeatureFlags, imageProperties.linearTilingFeatures))
991 {
Jamie Madill21061022018-07-12 23:56:30 -0400992 ANGLE_TRY(generateMipmapWithBlit(contextVk));
Luc Ferron05cd6df2018-05-24 15:51:29 -0400993 }
994 else
995 {
996 ANGLE_TRY(generateMipmapWithCPU(context));
997 }
998
999 // We're changing this textureVk content, make sure we let the graph know.
1000 onResourceChanged(renderer);
1001
Luc Ferronc5181702018-05-17 09:44:42 -04001002 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001003}
1004
Jamie Madill4928b7c2017-06-20 12:57:39 -04001005gl::Error TextureVk::setBaseLevel(const gl::Context *context, GLuint baseLevel)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001006{
1007 UNIMPLEMENTED();
Jamie Madill4928b7c2017-06-20 12:57:39 -04001008 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001009}
1010
Jamie Madill4928b7c2017-06-20 12:57:39 -04001011gl::Error TextureVk::bindTexImage(const gl::Context *context, egl::Surface *surface)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001012{
1013 UNIMPLEMENTED();
Jamie Madill4928b7c2017-06-20 12:57:39 -04001014 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001015}
1016
Jamie Madill4928b7c2017-06-20 12:57:39 -04001017gl::Error TextureVk::releaseTexImage(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001018{
1019 UNIMPLEMENTED();
Jamie Madill4928b7c2017-06-20 12:57:39 -04001020 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001021}
1022
Jamie Madill4928b7c2017-06-20 12:57:39 -04001023gl::Error TextureVk::getAttachmentRenderTarget(const gl::Context *context,
1024 GLenum binding,
Jamie Madill4fd95d52017-04-05 11:22:18 -04001025 const gl::ImageIndex &imageIndex,
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001026 FramebufferAttachmentRenderTarget **rtOut)
1027{
Jamie Madill26084d02018-04-09 13:44:04 -04001028 // Non-zero mip level attachments are an ES 3.0 feature.
Jamie Madill71bb0262018-09-12 11:09:42 -04001029 ASSERT(imageIndex.getLevelIndex() == 0);
Jamie Madill26084d02018-04-09 13:44:04 -04001030
1031 ContextVk *contextVk = vk::GetImpl(context);
Luc Ferronf6e160f2018-06-12 10:13:57 -04001032 ANGLE_TRY(ensureImageInitialized(contextVk));
Jamie Madill26084d02018-04-09 13:44:04 -04001033
Jamie Madill71bb0262018-09-12 11:09:42 -04001034 switch (imageIndex.getType())
1035 {
1036 case gl::TextureType::_2D:
1037 *rtOut = &mRenderTarget;
1038 break;
1039 case gl::TextureType::CubeMap:
1040 ANGLE_TRY(initCubeMapRenderTargets(contextVk));
1041 *rtOut = &mCubeMapRenderTargets[imageIndex.cubeMapFaceIndex()];
1042 break;
1043 default:
1044 UNREACHABLE();
1045 }
1046
Jamie Madillb79e7bb2017-10-24 13:55:50 -04001047 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001048}
1049
Jamie Madill21061022018-07-12 23:56:30 -04001050angle::Result TextureVk::ensureImageInitialized(ContextVk *contextVk)
Jamie Madill26084d02018-04-09 13:44:04 -04001051{
Luc Ferron10434f62018-04-24 10:06:37 -04001052 if (mImage.valid() && mPixelBuffer.empty())
1053 {
Jamie Madill21061022018-07-12 23:56:30 -04001054 return angle::Result::Continue();
Luc Ferron10434f62018-04-24 10:06:37 -04001055 }
Luc Ferronf6e160f2018-06-12 10:13:57 -04001056 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill26084d02018-04-09 13:44:04 -04001057 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001058 ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer));
Jamie Madill26084d02018-04-09 13:44:04 -04001059
Luc Ferron2f3f4142018-05-30 08:27:19 -04001060 const gl::ImageDesc &baseLevelDesc = mState.getBaseLevelDesc();
1061 const gl::Extents &baseLevelExtents = baseLevelDesc.size;
1062 const uint32_t levelCount = getLevelCount();
1063
Jamie Madill26084d02018-04-09 13:44:04 -04001064 if (!mImage.valid())
1065 {
Jamie Madill26084d02018-04-09 13:44:04 -04001066 const vk::Format &format =
1067 renderer->getFormat(baseLevelDesc.format.info->sizedInternalFormat);
Jamie Madill26084d02018-04-09 13:44:04 -04001068
Luc Ferronf6e160f2018-06-12 10:13:57 -04001069 ANGLE_TRY(initImage(contextVk, format, baseLevelExtents, levelCount, commandBuffer));
Jamie Madill26084d02018-04-09 13:44:04 -04001070 }
1071
Jamie Madill71bb0262018-09-12 11:09:42 -04001072 return mPixelBuffer.flushUpdatesToImage(contextVk, levelCount, &mImage, commandBuffer);
1073}
1074
1075angle::Result TextureVk::initCubeMapRenderTargets(ContextVk *contextVk)
1076{
1077 // Lazy init. Check if already initialized.
1078 if (!mCubeMapFaceImageViews.empty())
1079 return angle::Result::Continue();
1080
1081 mCubeMapFaceImageViews.resize(gl::kCubeFaceCount);
1082
1083 for (size_t cubeMapFaceIndex = 0; cubeMapFaceIndex < gl::kCubeFaceCount; ++cubeMapFaceIndex)
1084 {
1085 vk::ImageView &imageView = mCubeMapFaceImageViews[cubeMapFaceIndex];
1086 ANGLE_TRY(mImage.initLayerImageView(contextVk, gl::TextureType::CubeMap,
1087 VK_IMAGE_ASPECT_COLOR_BIT, gl::SwizzleState(),
1088 &imageView, 1, cubeMapFaceIndex, 1));
1089 mCubeMapRenderTargets.emplace_back(&mImage, &imageView, this);
1090 }
Jamie Madill21061022018-07-12 23:56:30 -04001091 return angle::Result::Continue();
Jamie Madill26084d02018-04-09 13:44:04 -04001092}
1093
Luc Ferron4bba74f2018-04-19 14:40:45 -04001094gl::Error TextureVk::syncState(const gl::Context *context, const gl::Texture::DirtyBits &dirtyBits)
Geoff Lang22416862016-06-08 16:14:36 -07001095{
Luc Ferron20610902018-04-19 14:41:13 -04001096 if (dirtyBits.none() && mSampler.valid())
1097 {
1098 return gl::NoError();
1099 }
1100
1101 ContextVk *contextVk = vk::GetImpl(context);
1102 if (mSampler.valid())
1103 {
1104 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc57ee252018-05-30 19:53:48 -04001105 renderer->releaseObject(getStoredQueueSerial(), &mSampler);
Luc Ferron20610902018-04-19 14:41:13 -04001106 }
1107
1108 const gl::SamplerState &samplerState = mState.getSamplerState();
1109
1110 // Create a simple sampler. Force basic parameter settings.
1111 VkSamplerCreateInfo samplerInfo;
1112 samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
1113 samplerInfo.pNext = nullptr;
1114 samplerInfo.flags = 0;
Jamie Madill097d3c02018-09-12 11:03:05 -04001115 samplerInfo.magFilter = gl_vk::GetFilter(samplerState.getMagFilter());
1116 samplerInfo.minFilter = gl_vk::GetFilter(samplerState.getMinFilter());
1117 samplerInfo.mipmapMode = gl_vk::GetSamplerMipmapMode(samplerState.getMinFilter());
1118 samplerInfo.addressModeU = gl_vk::GetSamplerAddressMode(samplerState.getWrapS());
1119 samplerInfo.addressModeV = gl_vk::GetSamplerAddressMode(samplerState.getWrapT());
1120 samplerInfo.addressModeW = gl_vk::GetSamplerAddressMode(samplerState.getWrapR());
Luc Ferron20610902018-04-19 14:41:13 -04001121 samplerInfo.mipLodBias = 0.0f;
1122 samplerInfo.anisotropyEnable = VK_FALSE;
1123 samplerInfo.maxAnisotropy = 1.0f;
1124 samplerInfo.compareEnable = VK_FALSE;
1125 samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
Jamie Madill097d3c02018-09-12 11:03:05 -04001126 samplerInfo.minLod = samplerState.getMinLod();
1127 samplerInfo.maxLod = samplerState.getMaxLod();
Luc Ferron20610902018-04-19 14:41:13 -04001128 samplerInfo.borderColor = VK_BORDER_COLOR_INT_TRANSPARENT_BLACK;
1129 samplerInfo.unnormalizedCoordinates = VK_FALSE;
1130
Jamie Madill71bb0262018-09-12 11:09:42 -04001131 return mSampler.init(contextVk, samplerInfo);
Geoff Lang22416862016-06-08 16:14:36 -07001132}
1133
Jamie Madillc564c072017-06-01 12:45:42 -04001134gl::Error TextureVk::setStorageMultisample(const gl::Context *context,
Corentin Wallez99d492c2018-02-27 15:17:10 -05001135 gl::TextureType type,
JiangYizhoubddc46b2016-12-09 09:50:51 +08001136 GLsizei samples,
1137 GLint internalformat,
1138 const gl::Extents &size,
Geoff Lang92019432017-11-20 13:09:34 -05001139 bool fixedSampleLocations)
JiangYizhoubddc46b2016-12-09 09:50:51 +08001140{
1141 UNIMPLEMENTED();
1142 return gl::InternalError() << "setStorageMultisample is unimplemented.";
1143}
1144
Jamie Madill05b35b22017-10-03 09:01:44 -04001145gl::Error TextureVk::initializeContents(const gl::Context *context,
1146 const gl::ImageIndex &imageIndex)
1147{
1148 UNIMPLEMENTED();
1149 return gl::NoError();
1150}
1151
Jamie Madill858c1cc2018-03-31 14:19:13 -04001152const vk::ImageHelper &TextureVk::getImage() const
Jamie Madill5547b382017-10-23 18:16:01 -04001153{
1154 ASSERT(mImage.valid());
Jamie Madill858c1cc2018-03-31 14:19:13 -04001155 return mImage;
Jamie Madill5547b382017-10-23 18:16:01 -04001156}
1157
1158const vk::ImageView &TextureVk::getImageView() const
1159{
Jamie Madill93edca12018-03-30 10:43:18 -04001160 ASSERT(mImage.valid());
Luc Ferron66410532018-04-20 12:47:45 -04001161
Jamie Madill097d3c02018-09-12 11:03:05 -04001162 const GLenum minFilter = mState.getSamplerState().getMinFilter();
Luc Ferron66410532018-04-20 12:47:45 -04001163 if (minFilter == GL_LINEAR || minFilter == GL_NEAREST)
1164 {
1165 return mBaseLevelImageView;
1166 }
1167
1168 return mMipmapImageView;
Jamie Madill5547b382017-10-23 18:16:01 -04001169}
1170
1171const vk::Sampler &TextureVk::getSampler() const
1172{
1173 ASSERT(mSampler.valid());
1174 return mSampler;
1175}
1176
Jamie Madill21061022018-07-12 23:56:30 -04001177angle::Result TextureVk::initImage(ContextVk *contextVk,
1178 const vk::Format &format,
1179 const gl::Extents &extents,
1180 const uint32_t levelCount,
1181 vk::CommandBuffer *commandBuffer)
Luc Ferronfa7503c2018-05-08 11:25:06 -04001182{
Luc Ferronf6e160f2018-06-12 10:13:57 -04001183 const RendererVk *renderer = contextVk->getRenderer();
Luc Ferronfa7503c2018-05-08 11:25:06 -04001184
1185 const VkImageUsageFlags usage =
1186 (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1187 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT);
1188
Jamie Madill21061022018-07-12 23:56:30 -04001189 ANGLE_TRY(mImage.init(contextVk, mState.getType(), extents, format, 1, usage, levelCount));
Luc Ferronfa7503c2018-05-08 11:25:06 -04001190
1191 const VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
1192
Jamie Madill21061022018-07-12 23:56:30 -04001193 ANGLE_TRY(mImage.initMemory(contextVk, renderer->getMemoryProperties(), flags));
Luc Ferronfa7503c2018-05-08 11:25:06 -04001194
1195 gl::SwizzleState mappedSwizzle;
1196 MapSwizzleState(format.internalFormat, mState.getSwizzleState(), &mappedSwizzle);
1197
Luc Ferronf6e160f2018-06-12 10:13:57 -04001198 // Renderable textures cannot have a swizzle.
1199 ASSERT(!contextVk->getTextureCaps().get(format.internalFormat).textureAttachment ||
1200 !mappedSwizzle.swizzleRequired());
1201
Luc Ferronfa7503c2018-05-08 11:25:06 -04001202 // TODO(jmadill): Separate imageviews for RenderTargets and Sampling.
Jamie Madill21061022018-07-12 23:56:30 -04001203 ANGLE_TRY(mImage.initImageView(contextVk, mState.getType(), VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferronfa7503c2018-05-08 11:25:06 -04001204 mappedSwizzle, &mMipmapImageView, levelCount));
Jamie Madill21061022018-07-12 23:56:30 -04001205 ANGLE_TRY(mImage.initImageView(contextVk, mState.getType(), VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferronfa7503c2018-05-08 11:25:06 -04001206 mappedSwizzle, &mBaseLevelImageView, 1));
1207
1208 // TODO(jmadill): Fold this into the RenderPass load/store ops. http://anglebug.com/2361
Luc Ferron7348fc52018-05-09 07:17:16 -04001209 VkClearColorValue black = {{0, 0, 0, 1.0f}};
Luc Ferronc20b9502018-05-24 09:30:17 -04001210 mImage.clearColor(black, 0, levelCount, commandBuffer);
Jamie Madill21061022018-07-12 23:56:30 -04001211 return angle::Result::Continue();
Luc Ferronfa7503c2018-05-08 11:25:06 -04001212}
1213
Jamie Madillc4f27e42018-03-31 14:19:18 -04001214void TextureVk::releaseImage(const gl::Context *context, RendererVk *renderer)
1215{
1216 mImage.release(renderer->getCurrentQueueSerial(), renderer);
Jamie Madillc57ee252018-05-30 19:53:48 -04001217 renderer->releaseObject(getStoredQueueSerial(), &mBaseLevelImageView);
1218 renderer->releaseObject(getStoredQueueSerial(), &mMipmapImageView);
Jamie Madill71bb0262018-09-12 11:09:42 -04001219
1220 for (vk::ImageView &imageView : mCubeMapFaceImageViews)
1221 {
1222 renderer->releaseObject(getStoredQueueSerial(), &imageView);
1223 }
1224 mCubeMapFaceImageViews.clear();
1225 mCubeMapRenderTargets.clear();
1226
Jamie Madillc4f27e42018-03-31 14:19:18 -04001227 onStateChange(context, angle::SubjectMessage::DEPENDENT_DIRTY_BITS);
1228}
1229
Luc Ferron66410532018-04-20 12:47:45 -04001230uint32_t TextureVk::getLevelCount() const
1231{
1232 ASSERT(mState.getEffectiveBaseLevel() == 0);
1233
1234 // getMipmapMaxLevel will be 0 here if mipmaps are not used, so the levelCount is always +1.
1235 return mState.getMipmapMaxLevel() + 1;
1236}
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001237} // namespace rx