blob: 4b7885cab03e6cf657e2e3c1bf777b17ac3b1fd3 [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// ContextVk.cpp:
7// Implements the class methods for ContextVk.
8//
9
10#include "libANGLE/renderer/vulkan/ContextVk.h"
11
Jamie Madill20e005b2017-04-07 14:19:22 -040012#include "common/bitset_utils.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040013#include "common/debug.h"
Frank Henigmana53d0e12018-02-13 00:06:06 -050014#include "common/utilities.h"
Jamie Madillbd159f02017-10-09 19:39:06 -040015#include "libANGLE/Context.h"
Jamie Madilldf68a6f2017-01-13 17:29:53 -050016#include "libANGLE/Program.h"
Geoff Langcaa55cd2018-07-05 13:19:35 -040017#include "libANGLE/Surface.h"
Jamie Madillacccc6c2016-05-03 17:22:10 -040018#include "libANGLE/renderer/vulkan/BufferVk.h"
Jamie Madill1f46bc12018-02-20 16:09:43 -050019#include "libANGLE/renderer/vulkan/CommandGraph.h"
Jamie Madillacccc6c2016-05-03 17:22:10 -040020#include "libANGLE/renderer/vulkan/CompilerVk.h"
Jamie Madillacccc6c2016-05-03 17:22:10 -040021#include "libANGLE/renderer/vulkan/FenceNVVk.h"
Jamie Madillacccc6c2016-05-03 17:22:10 -040022#include "libANGLE/renderer/vulkan/FramebufferVk.h"
Yunchao Hea336b902017-08-02 16:05:21 +080023#include "libANGLE/renderer/vulkan/ProgramPipelineVk.h"
Jamie Madillacccc6c2016-05-03 17:22:10 -040024#include "libANGLE/renderer/vulkan/ProgramVk.h"
25#include "libANGLE/renderer/vulkan/QueryVk.h"
26#include "libANGLE/renderer/vulkan/RenderbufferVk.h"
27#include "libANGLE/renderer/vulkan/RendererVk.h"
28#include "libANGLE/renderer/vulkan/SamplerVk.h"
29#include "libANGLE/renderer/vulkan/ShaderVk.h"
Jamie Madill70b5bb02017-08-28 13:32:37 -040030#include "libANGLE/renderer/vulkan/SyncVk.h"
Jamie Madillacccc6c2016-05-03 17:22:10 -040031#include "libANGLE/renderer/vulkan/TextureVk.h"
32#include "libANGLE/renderer/vulkan/TransformFeedbackVk.h"
33#include "libANGLE/renderer/vulkan/VertexArrayVk.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040034
35namespace rx
36{
Luc Ferron14f48172018-04-11 08:43:28 -040037
38namespace
39{
Jamie Madill21061022018-07-12 23:56:30 -040040GLenum DefaultGLErrorCode(VkResult result)
41{
42 switch (result)
43 {
44 case VK_ERROR_OUT_OF_HOST_MEMORY:
45 case VK_ERROR_OUT_OF_DEVICE_MEMORY:
46 case VK_ERROR_TOO_MANY_OBJECTS:
47 return GL_OUT_OF_MEMORY;
48 default:
49 return GL_INVALID_OPERATION;
50 }
51}
52
Jamie Madill88fc6da2018-08-30 16:18:36 -040053void BindNonNullVertexBufferRanges(vk::CommandBuffer *commandBuffer,
54 const gl::AttributesMask &nonNullAttribMask,
55 uint32_t maxAttrib,
56 const gl::AttribArray<VkBuffer> &arrayBufferHandles,
57 const gl::AttribArray<VkDeviceSize> &arrayBufferOffsets)
58{
59 // Vulkan does not allow binding a null vertex buffer but the default state of null buffers is
60 // valid.
61
62 // We can detect if there are no gaps in active attributes by using the mask of the program
63 // attribs and the max enabled attrib.
64 ASSERT(maxAttrib > 0);
65 if (nonNullAttribMask.to_ulong() == (maxAttrib - 1))
66 {
67 commandBuffer->bindVertexBuffers(0, maxAttrib, arrayBufferHandles.data(),
68 arrayBufferOffsets.data());
69 return;
70 }
71
72 // Find ranges of non-null buffers and bind them all together.
73 for (uint32_t attribIdx = 0; attribIdx < maxAttrib; attribIdx++)
74 {
75 if (arrayBufferHandles[attribIdx] != VK_NULL_HANDLE)
76 {
77 // Find the end of this range of non-null handles
78 uint32_t rangeCount = 1;
79 while (attribIdx + rangeCount < maxAttrib &&
80 arrayBufferHandles[attribIdx + rangeCount] != VK_NULL_HANDLE)
81 {
82 rangeCount++;
83 }
84
85 commandBuffer->bindVertexBuffers(attribIdx, rangeCount, &arrayBufferHandles[attribIdx],
86 &arrayBufferOffsets[attribIdx]);
87 attribIdx += rangeCount;
88 }
89 }
90}
91
Luc Ferron14f48172018-04-11 08:43:28 -040092constexpr gl::Rectangle kMaxSizedScissor(0,
93 0,
94 std::numeric_limits<int>::max(),
95 std::numeric_limits<int>::max());
96
Jamie Madill9aef3672018-04-27 11:45:06 -040097constexpr VkColorComponentFlags kAllColorChannelsMask =
98 (VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT |
99 VK_COLOR_COMPONENT_A_BIT);
Jamie Madill5a4c9322018-07-16 11:01:58 -0400100
101constexpr VkBufferUsageFlags kVertexBufferUsage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
102constexpr size_t kDefaultValueSize = sizeof(float) * 4;
103constexpr size_t kDefaultBufferSize = kDefaultValueSize * 16;
Luc Ferron14f48172018-04-11 08:43:28 -0400104} // anonymous namespace
105
Jamie Madill5a4c9322018-07-16 11:01:58 -0400106// std::array only uses aggregate init. Thus we make a helper macro to reduce on code duplication.
107#define INIT \
108 { \
109 kVertexBufferUsage, kDefaultBufferSize \
110 }
111
Jamie Madillacccc6c2016-05-03 17:22:10 -0400112ContextVk::ContextVk(const gl::ContextState &state, RendererVk *renderer)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500113 : ContextImpl(state),
Jamie Madill21061022018-07-12 23:56:30 -0400114 vk::Context(renderer),
Jamie Madill493f9572018-05-24 19:52:15 -0400115 mCurrentDrawMode(gl::PrimitiveMode::InvalidEnum),
Jamie Madill88fc6da2018-08-30 16:18:36 -0400116 mDirtyDefaultAttribs(false),
117 mPipelineDirty(false),
Frank Henigman17448952017-01-05 15:48:26 -0500118 mTexturesDirty(false),
Jamie Madill88fc6da2018-08-30 16:18:36 -0400119 mVertexBuffersDirty(false),
120 mIndexBufferDirty(false),
121 mDescriptorSetsDirty(false),
122 mLastIndexBufferOffset(0),
123 mCurrentDrawElementsType(GL_NONE),
Geoff Langcaa55cd2018-07-05 13:19:35 -0400124 mClearColorMask(kAllColorChannelsMask),
Jamie Madill834a3a12018-07-09 13:32:39 -0400125 mFlipYForCurrentSurface(false),
126 mDriverUniformsBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, sizeof(DriverUniforms) * 16),
Jamie Madill5a4c9322018-07-16 11:01:58 -0400127 mDriverUniformsDescriptorSet(VK_NULL_HANDLE),
128 mDefaultAttribBuffers{{INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT,
129 INIT, INIT, INIT, INIT}}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400130{
Jamie Madillf4d693c2018-02-14 16:38:16 -0500131 memset(&mClearColorValue, 0, sizeof(mClearColorValue));
132 memset(&mClearDepthStencilValue, 0, sizeof(mClearDepthStencilValue));
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400133}
134
Jamie Madill84c662b2018-07-12 15:56:55 -0400135ContextVk::~ContextVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400136
Jamie Madill76e471e2017-10-21 09:56:01 -0400137void ContextVk::onDestroy(const gl::Context *context)
138{
Jamie Madill834a3a12018-07-09 13:32:39 -0400139 mDriverUniformsSetLayout.reset();
Luc Ferron90968362018-05-04 08:47:22 -0400140 mIncompleteTextures.onDestroy(context);
Jamie Madill834a3a12018-07-09 13:32:39 -0400141 mDriverUniformsBuffer.destroy(getDevice());
Jamie Madilledeaa832018-06-22 09:18:41 -0400142
143 for (vk::DynamicDescriptorPool &descriptorPool : mDynamicDescriptorPools)
144 {
145 descriptorPool.destroy(getDevice());
146 }
Jamie Madill5a4c9322018-07-16 11:01:58 -0400147
148 for (vk::DynamicBuffer &defaultBuffer : mDefaultAttribBuffers)
149 {
150 defaultBuffer.destroy(getDevice());
151 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400152}
153
Luc Ferron90968362018-05-04 08:47:22 -0400154gl::Error ContextVk::getIncompleteTexture(const gl::Context *context,
155 gl::TextureType type,
156 gl::Texture **textureOut)
157{
158 // At some point, we'll need to support multisample and we'll pass "this" instead of nullptr
159 // and implement the necessary interface.
160 return mIncompleteTextures.getIncompleteTexture(context, type, nullptr, textureOut);
161}
162
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400163gl::Error ContextVk::initialize()
164{
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400165 // Note that this may reserve more sets than strictly necessary for a particular layout.
Jamie Madille4a6d7a2018-07-09 13:32:37 -0400166 VkDescriptorPoolSize uniformPoolSize = {
167 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
168 GetUniformBufferDescriptorCount() * vk::kDefaultDescriptorPoolMaxSets};
Jamie Madill834a3a12018-07-09 13:32:39 -0400169
Jamie Madill21061022018-07-12 23:56:30 -0400170 ANGLE_TRY(mDynamicDescriptorPools[kUniformsDescriptorSetIndex].init(this, uniformPoolSize));
Jamie Madille4a6d7a2018-07-09 13:32:37 -0400171
172 VkDescriptorPoolSize imageSamplerPoolSize = {
173 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
174 mRenderer->getMaxActiveTextures() * vk::kDefaultDescriptorPoolMaxSets};
Jamie Madill21061022018-07-12 23:56:30 -0400175 ANGLE_TRY(mDynamicDescriptorPools[kTextureDescriptorSetIndex].init(this, imageSamplerPoolSize));
Jamie Madill76e471e2017-10-21 09:56:01 -0400176
Jamie Madill834a3a12018-07-09 13:32:39 -0400177 VkDescriptorPoolSize driverUniformsPoolSize = {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
178 vk::kDefaultDescriptorPoolMaxSets};
179 ANGLE_TRY(mDynamicDescriptorPools[kDriverUniformsDescriptorSetIndex].init(
Jamie Madill21061022018-07-12 23:56:30 -0400180 this, driverUniformsPoolSize));
Jamie Madill834a3a12018-07-09 13:32:39 -0400181
Frank Henigman18f7e502018-07-19 16:06:43 -0400182 size_t minAlignment = static_cast<size_t>(
183 mRenderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
184 mDriverUniformsBuffer.init(minAlignment, mRenderer);
185
Jamie Madillf2f6d372018-01-10 21:37:23 -0500186 mPipelineDesc.reset(new vk::PipelineDesc());
187 mPipelineDesc->initDefaults();
188
Jamie Madill5a4c9322018-07-16 11:01:58 -0400189 // Initialize current value/default attribute buffers.
190 for (vk::DynamicBuffer &buffer : mDefaultAttribBuffers)
191 {
192 buffer.init(1, mRenderer);
193 }
194
Jamie Madille09bd5d2016-11-29 16:20:35 -0500195 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400196}
197
Jamie Madillafa02a22017-11-23 12:57:38 -0500198gl::Error ContextVk::flush(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400199{
Jamie Madilla2f043d2018-07-10 17:21:20 -0400200 // TODO(jmadill): Multiple flushes will need to insert semaphores. http://anglebug.com/2504
Luc Ferron33140402018-03-08 13:57:52 -0500201
202 // dEQP tests rely on having no errors thrown at the end of the test and they always call
203 // flush at the end of the their tests. Just returning NoError until we implement flush
204 // allow us to work on enabling many tests in the meantime.
Jamie Madilla2f043d2018-07-10 17:21:20 -0400205 WARN() << "Flush is unimplemented. http://anglebug.com/2504";
Luc Ferron33140402018-03-08 13:57:52 -0500206 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400207}
208
Jamie Madillafa02a22017-11-23 12:57:38 -0500209gl::Error ContextVk::finish(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400210{
Jamie Madill21061022018-07-12 23:56:30 -0400211 return mRenderer->finish(this);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400212}
213
Jamie Madill88fc6da2018-08-30 16:18:36 -0400214angle::Result ContextVk::initPipeline(const gl::DrawCallParams &drawCallParams)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400215{
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500216 ASSERT(!mCurrentPipeline);
Jamie Madill72106562017-03-24 14:18:50 -0400217
Jamie Madillf2f6d372018-01-10 21:37:23 -0500218 const gl::State &state = mState.getState();
219 VertexArrayVk *vertexArrayVk = vk::GetImpl(state.getVertexArray());
220 FramebufferVk *framebufferVk = vk::GetImpl(state.getDrawFramebuffer());
221 ProgramVk *programVk = vk::GetImpl(state.getProgram());
Luc Ferronceb71902018-02-05 15:18:47 -0500222 const gl::AttributesMask activeAttribLocationsMask =
223 state.getProgram()->getActiveAttribLocationsMask();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500224
225 // Ensure the topology of the pipeline description is updated.
226 mPipelineDesc->updateTopology(mCurrentDrawMode);
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500227
Jamie Madill112a3a82018-01-23 13:04:06 -0500228 // Copy over the latest attrib and binding descriptions.
Frank Henigman419acc82018-06-24 19:57:31 -0400229 vertexArrayVk->getPackedInputDescriptions(mPipelineDesc.get());
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500230
Jamie Madillf2f6d372018-01-10 21:37:23 -0500231 // Ensure that the RenderPass description is updated.
Jamie Madillb90779e2018-04-27 11:45:01 -0400232 mPipelineDesc->updateRenderPassDesc(framebufferVk->getRenderPassDesc());
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500233
Jamie Madill06ca6342018-07-12 15:56:53 -0400234 // Trigger draw call shader patching and fill out the pipeline desc.
235 const vk::ShaderAndSerial *vertexShaderAndSerial = nullptr;
236 const vk::ShaderAndSerial *fragmentShaderAndSerial = nullptr;
Jamie Madill242c4fe2018-07-12 15:56:56 -0400237 const vk::PipelineLayout *pipelineLayout = nullptr;
Jamie Madill06ca6342018-07-12 15:56:53 -0400238 ANGLE_TRY(programVk->initShaders(this, drawCallParams, &vertexShaderAndSerial,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400239 &fragmentShaderAndSerial, &pipelineLayout));
Jamie Madill06ca6342018-07-12 15:56:53 -0400240
241 mPipelineDesc->updateShaders(vertexShaderAndSerial->getSerial(),
242 fragmentShaderAndSerial->getSerial());
243
Jamie Madill06ca6342018-07-12 15:56:53 -0400244 ANGLE_TRY(mRenderer->getPipeline(this, *vertexShaderAndSerial, *fragmentShaderAndSerial,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400245 *pipelineLayout, *mPipelineDesc, activeAttribLocationsMask,
Jamie Madill06ca6342018-07-12 15:56:53 -0400246 &mCurrentPipeline));
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500247
Jamie Madill88fc6da2018-08-30 16:18:36 -0400248 return angle::Result::Continue();
Jamie Madill72106562017-03-24 14:18:50 -0400249}
250
Jamie Madill88fc6da2018-08-30 16:18:36 -0400251angle::Result ContextVk::setupDraw(const gl::Context *context,
252 const gl::DrawCallParams &drawCallParams,
253 bool useIndexBuffer,
254 vk::CommandBuffer **commandBufferOut)
Jamie Madill72106562017-03-24 14:18:50 -0400255{
Jamie Madill32fd63b2018-03-31 11:20:35 -0400256 if (drawCallParams.mode() != mCurrentDrawMode)
Jamie Madill72106562017-03-24 14:18:50 -0400257 {
258 invalidateCurrentPipeline();
Jamie Madill32fd63b2018-03-31 11:20:35 -0400259 mCurrentDrawMode = drawCallParams.mode();
Jamie Madill72106562017-03-24 14:18:50 -0400260 }
261
Jamie Madill88fc6da2018-08-30 16:18:36 -0400262 const gl::State &state = mState.getState();
263 const gl::Program *programGL = state.getProgram();
264 ProgramVk *programVk = vk::GetImpl(programGL);
265 FramebufferVk *framebufferVk = vk::GetImpl(state.getDrawFramebuffer());
266 VertexArrayVk *vertexArrayVk = vk::GetImpl(state.getVertexArray());
Jamie Madill5a4c9322018-07-16 11:01:58 -0400267
Jamie Madill88fc6da2018-08-30 16:18:36 -0400268 vk::RecordingMode mode;
Jamie Madill316c6062018-05-29 10:49:45 -0400269 ANGLE_TRY(framebufferVk->getCommandBufferForDraw(this, commandBufferOut, &mode));
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500270
Jamie Madill88fc6da2018-08-30 16:18:36 -0400271 // Set any dirty bits that depend on draw call parameters or other objects.
Jamie Madill316c6062018-05-29 10:49:45 -0400272 if (mode == vk::RecordingMode::Start)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500273 {
Jamie Madill88fc6da2018-08-30 16:18:36 -0400274 mPipelineDirty = true;
275 mTexturesDirty = true;
276 mVertexBuffersDirty = true;
277 mIndexBufferDirty = true;
278 mDescriptorSetsDirty = true;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500279 }
Jamie Madill88fc6da2018-08-30 16:18:36 -0400280
281 if (context->getStateCache().hasAnyActiveClientAttrib())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500282 {
Jamie Madill88fc6da2018-08-30 16:18:36 -0400283 ANGLE_TRY(vertexArrayVk->updateClientAttribs(context, drawCallParams));
284 mVertexBuffersDirty = true;
285 }
286
287 if (programVk->dirtyUniforms())
288 {
289 ANGLE_TRY(programVk->updateUniforms(this));
290 mDescriptorSetsDirty = true;
291 }
292
293 // Flush any relevant dirty bits.
294 if (mDirtyDefaultAttribs)
295 {
296 ANGLE_TRY(updateDefaultAttributes());
297 mDirtyDefaultAttribs = false;
298 }
299
300 if (mPipelineDirty)
301 {
302 if (!mCurrentPipeline)
303 {
304 ANGLE_TRY(initPipeline(drawCallParams));
305 }
306
307 (*commandBufferOut)->bindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, mCurrentPipeline->get());
308
309 // Update the queue serial for the pipeline object.
310 ASSERT(mCurrentPipeline && mCurrentPipeline->valid());
311 mCurrentPipeline->updateSerial(mRenderer->getCurrentQueueSerial());
312 mPipelineDirty = false;
313 mVertexBuffersDirty = true;
314 mIndexBufferDirty = true;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500315 }
316
317 // Ensure any writes to the textures are flushed before we read from them.
318 if (mTexturesDirty)
319 {
Jamie Madill88fc6da2018-08-30 16:18:36 -0400320 ANGLE_TRY(updateActiveTextures(context));
Jamie Madill84c662b2018-07-12 15:56:55 -0400321
Jamie Madill49ac74b2017-12-21 14:42:33 -0500322 // TODO(jmadill): Should probably merge this for loop with programVk's descriptor update.
Jamie Madill7e4eff12018-08-08 15:49:26 -0400323 for (size_t textureIndex : programGL->getActiveSamplersMask())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500324 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400325 TextureVk *textureVk = mActiveTextures[textureIndex];
326 ANGLE_TRY(textureVk->ensureImageInitialized(this));
327 textureVk->addReadDependency(framebufferVk);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500328 }
Jamie Madill88fc6da2018-08-30 16:18:36 -0400329
330 if (programVk->hasTextures())
331 {
332 ANGLE_TRY(programVk->updateTexturesDescriptorSet(this));
333 }
334
335 mDescriptorSetsDirty = true;
336 mTexturesDirty = false;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500337 }
338
Jamie Madill88fc6da2018-08-30 16:18:36 -0400339 if (mDescriptorSetsDirty)
340 {
341 ANGLE_TRY(programVk->updateDescriptorSets(this, drawCallParams, *commandBufferOut));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500342
Jamie Madill88fc6da2018-08-30 16:18:36 -0400343 // Bind the graphics descriptor sets.
344 (*commandBufferOut)
345 ->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, programVk->getPipelineLayout(),
346 kDriverUniformsDescriptorSetIndex, 1,
347 &mDriverUniformsDescriptorSet, 0, nullptr);
348 mDescriptorSetsDirty = false;
349 }
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500350
Jamie Madill88fc6da2018-08-30 16:18:36 -0400351 uint32_t maxAttrib = programGL->getState().getMaxActiveAttribLocation();
352 if (mVertexBuffersDirty && maxAttrib > 0)
353 {
354 const gl::AttributesMask &programAttribsMask = programGL->getActiveAttribLocationsMask();
355 BindNonNullVertexBufferRanges(*commandBufferOut, programAttribsMask, maxAttrib,
356 vertexArrayVk->getCurrentArrayBufferHandles(),
357 vertexArrayVk->getCurrentArrayBufferOffsets());
358
359 const auto &arrayBufferResources = vertexArrayVk->getCurrentArrayBufferResources();
360
361 for (size_t attribIndex : context->getStateCache().getActiveBufferedAttribsMask())
362 {
363 if (arrayBufferResources[attribIndex])
364 arrayBufferResources[attribIndex]->addReadDependency(framebufferVk);
365 }
366
367 mVertexBuffersDirty = false;
368 }
369
370 if (useIndexBuffer && mIndexBufferDirty)
371 {
372 (*commandBufferOut)
373 ->bindIndexBuffer(vertexArrayVk->getCurrentElementArrayBufferHandle(),
374 vertexArrayVk->getCurrentElementArrayBufferOffset(),
375 gl_vk::GetIndexType(mCurrentDrawElementsType));
376
377 vk::CommandGraphResource *elementArrayBufferResource =
378 vertexArrayVk->getCurrentElementArrayBufferResource();
379 if (elementArrayBufferResource)
380 {
381 elementArrayBufferResource->addReadDependency(framebufferVk);
382 }
383
384 mIndexBufferDirty = false;
385 }
386
387 return angle::Result::Continue();
388}
389
390angle::Result ContextVk::setupIndexedDraw(const gl::Context *context,
391 const gl::DrawCallParams &drawCallParams,
392 vk::CommandBuffer **commandBufferOut)
393{
394 VertexArrayVk *vertexArrayVk = vk::GetImpl(mState.getState().getVertexArray());
395
396 if (drawCallParams.type() != mCurrentDrawElementsType)
397 {
398 mIndexBufferDirty = true;
399 mCurrentDrawElementsType = drawCallParams.type();
400 }
401
402 const gl::Buffer *elementArrayBuffer = vertexArrayVk->getState().getElementArrayBuffer().get();
403 if (!elementArrayBuffer)
404 {
405 mIndexBufferDirty = true;
406 ANGLE_TRY(vertexArrayVk->updateIndexTranslation(this, drawCallParams));
407 }
408 else
409 {
410 if (drawCallParams.indices() != mLastIndexBufferOffset)
411 {
412 mIndexBufferDirty = true;
413 mLastIndexBufferOffset = drawCallParams.indices();
414 vertexArrayVk->updateCurrentElementArrayBufferOffset(mLastIndexBufferOffset);
415 }
416
417 if (drawCallParams.type() == GL_UNSIGNED_BYTE && mIndexBufferDirty)
418 {
419 ANGLE_TRY(vertexArrayVk->updateIndexTranslation(this, drawCallParams));
420 }
421 }
422
423 return setupDraw(context, drawCallParams, true, commandBufferOut);
424}
425
426angle::Result ContextVk::setupLineLoopDraw(const gl::Context *context,
427 const gl::DrawCallParams &drawCallParams,
428 vk::CommandBuffer **commandBufferOut)
429{
430 VertexArrayVk *vertexArrayVk = vk::GetImpl(mState.getState().getVertexArray());
431 ANGLE_TRY(vertexArrayVk->handleLineLoop(this, drawCallParams));
432 mIndexBufferDirty = true;
433 mCurrentDrawElementsType =
434 drawCallParams.isDrawElements() ? drawCallParams.type() : GL_UNSIGNED_INT;
435 return setupDraw(context, drawCallParams, true, commandBufferOut);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400436}
437
Jamie Madill493f9572018-05-24 19:52:15 -0400438gl::Error ContextVk::drawArrays(const gl::Context *context,
439 gl::PrimitiveMode mode,
440 GLint first,
441 GLsizei count)
Jamie Madilld03a8492017-10-03 15:46:06 -0400442{
Jamie Madill32fd63b2018-03-31 11:20:35 -0400443 const gl::DrawCallParams &drawCallParams = context->getParams<gl::DrawCallParams>();
444
Jamie Madill316c6062018-05-29 10:49:45 -0400445 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill88fc6da2018-08-30 16:18:36 -0400446 uint32_t clampedVertexCount = drawCallParams.getClampedVertexCount<uint32_t>();
Luc Ferron360098d2018-02-21 07:33:50 -0500447
Jamie Madill88fc6da2018-08-30 16:18:36 -0400448 if (mode == gl::PrimitiveMode::LineLoop)
449 {
450 ANGLE_TRY(setupLineLoopDraw(context, drawCallParams, &commandBuffer));
451 vk::LineLoopHelper::Draw(clampedVertexCount, commandBuffer);
452 }
453 else
454 {
455 ANGLE_TRY(setupDraw(context, drawCallParams, false, &commandBuffer));
456 commandBuffer->draw(clampedVertexCount, 1, drawCallParams.firstVertex(), 0);
457 }
Luc Ferron360098d2018-02-21 07:33:50 -0500458
Jamie Madilld03a8492017-10-03 15:46:06 -0400459 return gl::NoError();
460}
461
Jamie Madillc564c072017-06-01 12:45:42 -0400462gl::Error ContextVk::drawArraysInstanced(const gl::Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400463 gl::PrimitiveMode mode,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400464 GLint first,
465 GLsizei count,
466 GLsizei instanceCount)
467{
468 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500469 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400470}
471
Jamie Madillc564c072017-06-01 12:45:42 -0400472gl::Error ContextVk::drawElements(const gl::Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400473 gl::PrimitiveMode mode,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400474 GLsizei count,
475 GLenum type,
Qin Jiajia1da00652017-06-20 17:16:25 +0800476 const void *indices)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400477{
Jamie Madill32fd63b2018-03-31 11:20:35 -0400478 const gl::DrawCallParams &drawCallParams = context->getParams<gl::DrawCallParams>();
479
Jamie Madill316c6062018-05-29 10:49:45 -0400480 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill88fc6da2018-08-30 16:18:36 -0400481 if (mode == gl::PrimitiveMode::LineLoop)
482 {
483 ANGLE_TRY(setupLineLoopDraw(context, drawCallParams, &commandBuffer));
484 vk::LineLoopHelper::Draw(count, commandBuffer);
485 }
486 else
487 {
488 ANGLE_TRY(setupIndexedDraw(context, drawCallParams, &commandBuffer));
489 commandBuffer->drawIndexed(count, 1, 0, 0, 0);
490 }
Jamie Madilld03a8492017-10-03 15:46:06 -0400491
492 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400493}
494
Jamie Madillc564c072017-06-01 12:45:42 -0400495gl::Error ContextVk::drawElementsInstanced(const gl::Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400496 gl::PrimitiveMode mode,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400497 GLsizei count,
498 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400499 const void *indices,
Qin Jiajia1da00652017-06-20 17:16:25 +0800500 GLsizei instances)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400501{
502 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500503 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400504}
505
Jamie Madillc564c072017-06-01 12:45:42 -0400506gl::Error ContextVk::drawRangeElements(const gl::Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400507 gl::PrimitiveMode mode,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400508 GLuint start,
509 GLuint end,
510 GLsizei count,
511 GLenum type,
Qin Jiajia1da00652017-06-20 17:16:25 +0800512 const void *indices)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400513{
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500514 return gl::NoError();
515}
516
517VkDevice ContextVk::getDevice() const
518{
519 return mRenderer->getDevice();
520}
521
Jamie Madillc564c072017-06-01 12:45:42 -0400522gl::Error ContextVk::drawArraysIndirect(const gl::Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400523 gl::PrimitiveMode mode,
Jamie Madillc564c072017-06-01 12:45:42 -0400524 const void *indirect)
Jiajia Qind9671222016-11-29 16:30:31 +0800525{
526 UNIMPLEMENTED();
527 return gl::InternalError() << "DrawArraysIndirect hasn't been implemented for vulkan backend.";
528}
529
Jamie Madillc564c072017-06-01 12:45:42 -0400530gl::Error ContextVk::drawElementsIndirect(const gl::Context *context,
Jamie Madill493f9572018-05-24 19:52:15 -0400531 gl::PrimitiveMode mode,
Jamie Madillc564c072017-06-01 12:45:42 -0400532 GLenum type,
533 const void *indirect)
Jiajia Qind9671222016-11-29 16:30:31 +0800534{
535 UNIMPLEMENTED();
536 return gl::InternalError()
537 << "DrawElementsIndirect hasn't been implemented for vulkan backend.";
538}
539
Corentin Wallez87fbe1c2016-08-03 14:41:42 -0400540GLenum ContextVk::getResetStatus()
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400541{
542 UNIMPLEMENTED();
Corentin Wallez87fbe1c2016-08-03 14:41:42 -0400543 return GL_NO_ERROR;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400544}
545
546std::string ContextVk::getVendorString() const
547{
548 UNIMPLEMENTED();
549 return std::string();
550}
551
552std::string ContextVk::getRendererDescription() const
553{
Jamie Madille09bd5d2016-11-29 16:20:35 -0500554 return mRenderer->getRendererDescription();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400555}
556
557void ContextVk::insertEventMarker(GLsizei length, const char *marker)
558{
559 UNIMPLEMENTED();
560}
561
562void ContextVk::pushGroupMarker(GLsizei length, const char *marker)
563{
564 UNIMPLEMENTED();
565}
566
567void ContextVk::popGroupMarker()
568{
569 UNIMPLEMENTED();
570}
571
Geoff Lang5d5253a2017-11-22 14:51:12 -0500572void ContextVk::pushDebugGroup(GLenum source, GLuint id, GLsizei length, const char *message)
573{
574 UNIMPLEMENTED();
575}
576
577void ContextVk::popDebugGroup()
578{
579 UNIMPLEMENTED();
580}
581
Luc Ferron82eda932018-07-09 15:10:22 -0400582bool ContextVk::isViewportFlipEnabledForDrawFBO() const
Luc Ferronbf6dc372018-06-28 15:24:19 -0400583{
Luc Ferron82eda932018-07-09 15:10:22 -0400584 return mFlipViewportForDrawFramebuffer && mFlipYForCurrentSurface;
585}
586
587bool ContextVk::isViewportFlipEnabledForReadFBO() const
588{
589 return mFlipViewportForReadFramebuffer;
Luc Ferronbf6dc372018-06-28 15:24:19 -0400590}
591
Luc Ferron0bb940a2018-06-22 09:59:34 -0400592void ContextVk::updateColorMask(const gl::BlendState &blendState)
Luc Ferron5fd36932018-06-19 14:55:50 -0400593{
594 mClearColorMask =
595 gl_vk::GetColorComponentFlags(blendState.colorMaskRed, blendState.colorMaskGreen,
596 blendState.colorMaskBlue, blendState.colorMaskAlpha);
597
598 FramebufferVk *framebufferVk = vk::GetImpl(mState.getState().getDrawFramebuffer());
599 mPipelineDesc->updateColorWriteMask(mClearColorMask,
600 framebufferVk->getEmulatedAlphaAttachmentMask());
601}
602
Jamie Madill84c662b2018-07-12 15:56:55 -0400603void ContextVk::updateScissor(const gl::State &glState) const
Luc Ferrond17bdfe2018-04-05 13:50:10 -0400604{
Jamie Madill84c662b2018-07-12 15:56:55 -0400605 FramebufferVk *framebufferVk = vk::GetImpl(glState.getDrawFramebuffer());
Luc Ferronbf6dc372018-06-28 15:24:19 -0400606 gl::Box dimensions = framebufferVk->getState().getDimensions();
607 gl::Rectangle renderArea(0, 0, dimensions.width, dimensions.height);
608
Luc Ferrond17bdfe2018-04-05 13:50:10 -0400609 if (glState.isScissorTestEnabled())
610 {
Luc Ferron82eda932018-07-09 15:10:22 -0400611 mPipelineDesc->updateScissor(glState.getScissor(), isViewportFlipEnabledForDrawFBO(),
612 renderArea);
Luc Ferrond17bdfe2018-04-05 13:50:10 -0400613 }
614 else
615 {
Luc Ferron14f48172018-04-11 08:43:28 -0400616 // If the scissor test isn't enabled, we can simply use a really big scissor that's
617 // certainly larger than the current surface using the maximum size of a 2D texture
618 // for the width and height.
Luc Ferron82eda932018-07-09 15:10:22 -0400619 mPipelineDesc->updateScissor(kMaxSizedScissor, isViewportFlipEnabledForDrawFBO(),
620 renderArea);
Luc Ferrond17bdfe2018-04-05 13:50:10 -0400621 }
622}
623
Jamie Madill189ad872018-07-09 13:32:37 -0400624gl::Error ContextVk::syncState(const gl::Context *context, const gl::State::DirtyBits &dirtyBits)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400625{
Jamie Madill72106562017-03-24 14:18:50 -0400626 if (dirtyBits.any())
627 {
628 invalidateCurrentPipeline();
629 }
Jamie Madillebf72992017-10-13 14:09:45 -0400630
Jamie Madill88fc6da2018-08-30 16:18:36 -0400631 const gl::State &glState = context->getGLState();
Jamie Madillebf72992017-10-13 14:09:45 -0400632
Jamie Madill88fc6da2018-08-30 16:18:36 -0400633 for (size_t dirtyBit : dirtyBits)
Jamie Madillebf72992017-10-13 14:09:45 -0400634 {
635 switch (dirtyBit)
636 {
637 case gl::State::DIRTY_BIT_SCISSOR_TEST_ENABLED:
Jamie Madillebf72992017-10-13 14:09:45 -0400638 case gl::State::DIRTY_BIT_SCISSOR:
Luc Ferron14f48172018-04-11 08:43:28 -0400639 updateScissor(glState);
Jamie Madillebf72992017-10-13 14:09:45 -0400640 break;
641 case gl::State::DIRTY_BIT_VIEWPORT:
Luc Ferron1a135ad2018-07-04 10:35:31 -0400642 {
643 FramebufferVk *framebufferVk = vk::GetImpl(glState.getDrawFramebuffer());
644 mPipelineDesc->updateViewport(framebufferVk, glState.getViewport(),
645 glState.getNearPlane(), glState.getFarPlane(),
Luc Ferron82eda932018-07-09 15:10:22 -0400646 isViewportFlipEnabledForDrawFBO());
Luc Ferrone8356092018-07-12 12:36:47 -0400647 ANGLE_TRY(updateDriverUniforms(glState));
Jamie Madillebf72992017-10-13 14:09:45 -0400648 break;
Luc Ferron1a135ad2018-07-04 10:35:31 -0400649 }
Jamie Madillebf72992017-10-13 14:09:45 -0400650 case gl::State::DIRTY_BIT_DEPTH_RANGE:
Luc Ferron0986f1c2018-04-16 13:47:23 -0400651 mPipelineDesc->updateDepthRange(glState.getNearPlane(), glState.getFarPlane());
Luc Ferrone8356092018-07-12 12:36:47 -0400652 ANGLE_TRY(updateDriverUniforms(glState));
Jamie Madillebf72992017-10-13 14:09:45 -0400653 break;
654 case gl::State::DIRTY_BIT_BLEND_ENABLED:
Luc Ferronf8be7562018-02-06 15:59:11 -0500655 mPipelineDesc->updateBlendEnabled(glState.isBlendEnabled());
Jamie Madillebf72992017-10-13 14:09:45 -0400656 break;
657 case gl::State::DIRTY_BIT_BLEND_COLOR:
Luc Ferronf8be7562018-02-06 15:59:11 -0500658 mPipelineDesc->updateBlendColor(glState.getBlendColor());
Jamie Madillebf72992017-10-13 14:09:45 -0400659 break;
660 case gl::State::DIRTY_BIT_BLEND_FUNCS:
Luc Ferronf8be7562018-02-06 15:59:11 -0500661 mPipelineDesc->updateBlendFuncs(glState.getBlendState());
Jamie Madillebf72992017-10-13 14:09:45 -0400662 break;
663 case gl::State::DIRTY_BIT_BLEND_EQUATIONS:
Luc Ferronf8be7562018-02-06 15:59:11 -0500664 mPipelineDesc->updateBlendEquations(glState.getBlendState());
Jamie Madillebf72992017-10-13 14:09:45 -0400665 break;
666 case gl::State::DIRTY_BIT_COLOR_MASK:
Luc Ferron0bb940a2018-06-22 09:59:34 -0400667 updateColorMask(glState.getBlendState());
Jamie Madillebf72992017-10-13 14:09:45 -0400668 break;
669 case gl::State::DIRTY_BIT_SAMPLE_ALPHA_TO_COVERAGE_ENABLED:
Jamie Madillebf72992017-10-13 14:09:45 -0400670 break;
671 case gl::State::DIRTY_BIT_SAMPLE_COVERAGE_ENABLED:
Jamie Madillebf72992017-10-13 14:09:45 -0400672 break;
673 case gl::State::DIRTY_BIT_SAMPLE_COVERAGE:
Jamie Madillebf72992017-10-13 14:09:45 -0400674 break;
675 case gl::State::DIRTY_BIT_SAMPLE_MASK_ENABLED:
Jamie Madillebf72992017-10-13 14:09:45 -0400676 break;
Jamie Madillc67323a2017-11-02 23:11:41 -0400677 case gl::State::DIRTY_BIT_SAMPLE_MASK:
Jamie Madillebf72992017-10-13 14:09:45 -0400678 break;
679 case gl::State::DIRTY_BIT_DEPTH_TEST_ENABLED:
Geoff Langc16f5182018-07-18 10:40:03 -0400680 mPipelineDesc->updateDepthTestEnabled(glState.getDepthStencilState(),
681 glState.getDrawFramebuffer());
Jamie Madillebf72992017-10-13 14:09:45 -0400682 break;
683 case gl::State::DIRTY_BIT_DEPTH_FUNC:
Jamie Madill0cec82a2018-03-14 09:21:07 -0400684 mPipelineDesc->updateDepthFunc(glState.getDepthStencilState());
Jamie Madillebf72992017-10-13 14:09:45 -0400685 break;
686 case gl::State::DIRTY_BIT_DEPTH_MASK:
Geoff Langc16f5182018-07-18 10:40:03 -0400687 mPipelineDesc->updateDepthWriteEnabled(glState.getDepthStencilState(),
688 glState.getDrawFramebuffer());
Jamie Madillebf72992017-10-13 14:09:45 -0400689 break;
690 case gl::State::DIRTY_BIT_STENCIL_TEST_ENABLED:
Geoff Langc16f5182018-07-18 10:40:03 -0400691 mPipelineDesc->updateStencilTestEnabled(glState.getDepthStencilState(),
692 glState.getDrawFramebuffer());
Jamie Madillebf72992017-10-13 14:09:45 -0400693 break;
694 case gl::State::DIRTY_BIT_STENCIL_FUNCS_FRONT:
Luc Ferron364a9552018-03-29 09:44:51 -0400695 mPipelineDesc->updateStencilFrontFuncs(glState.getStencilRef(),
696 glState.getDepthStencilState());
Jamie Madillebf72992017-10-13 14:09:45 -0400697 break;
698 case gl::State::DIRTY_BIT_STENCIL_FUNCS_BACK:
Luc Ferron364a9552018-03-29 09:44:51 -0400699 mPipelineDesc->updateStencilBackFuncs(glState.getStencilBackRef(),
700 glState.getDepthStencilState());
Jamie Madillebf72992017-10-13 14:09:45 -0400701 break;
702 case gl::State::DIRTY_BIT_STENCIL_OPS_FRONT:
Luc Ferron364a9552018-03-29 09:44:51 -0400703 mPipelineDesc->updateStencilFrontOps(glState.getDepthStencilState());
Jamie Madillebf72992017-10-13 14:09:45 -0400704 break;
705 case gl::State::DIRTY_BIT_STENCIL_OPS_BACK:
Luc Ferron364a9552018-03-29 09:44:51 -0400706 mPipelineDesc->updateStencilBackOps(glState.getDepthStencilState());
Jamie Madillebf72992017-10-13 14:09:45 -0400707 break;
708 case gl::State::DIRTY_BIT_STENCIL_WRITEMASK_FRONT:
Geoff Langc16f5182018-07-18 10:40:03 -0400709 mPipelineDesc->updateStencilFrontWriteMask(glState.getDepthStencilState(),
710 glState.getDrawFramebuffer());
Jamie Madillebf72992017-10-13 14:09:45 -0400711 break;
712 case gl::State::DIRTY_BIT_STENCIL_WRITEMASK_BACK:
Geoff Langc16f5182018-07-18 10:40:03 -0400713 mPipelineDesc->updateStencilBackWriteMask(glState.getDepthStencilState(),
714 glState.getDrawFramebuffer());
Jamie Madillebf72992017-10-13 14:09:45 -0400715 break;
716 case gl::State::DIRTY_BIT_CULL_FACE_ENABLED:
717 case gl::State::DIRTY_BIT_CULL_FACE:
Luc Ferronb70ad522018-07-09 16:06:26 -0400718 mPipelineDesc->updateCullMode(glState.getRasterizerState());
Jamie Madillebf72992017-10-13 14:09:45 -0400719 break;
720 case gl::State::DIRTY_BIT_FRONT_FACE:
Luc Ferronb70ad522018-07-09 16:06:26 -0400721 mPipelineDesc->updateFrontFace(glState.getRasterizerState(),
722 isViewportFlipEnabledForDrawFBO());
Jamie Madillebf72992017-10-13 14:09:45 -0400723 break;
724 case gl::State::DIRTY_BIT_POLYGON_OFFSET_FILL_ENABLED:
Frank Henigmand731ff82018-08-13 18:18:51 -0400725 mPipelineDesc->updatePolygonOffsetFillEnabled(glState.isPolygonOffsetFillEnabled());
Jamie Madillebf72992017-10-13 14:09:45 -0400726 break;
727 case gl::State::DIRTY_BIT_POLYGON_OFFSET:
Frank Henigmand731ff82018-08-13 18:18:51 -0400728 mPipelineDesc->updatePolygonOffset(glState.getRasterizerState());
Jamie Madillebf72992017-10-13 14:09:45 -0400729 break;
730 case gl::State::DIRTY_BIT_RASTERIZER_DISCARD_ENABLED:
Jamie Madillebf72992017-10-13 14:09:45 -0400731 break;
732 case gl::State::DIRTY_BIT_LINE_WIDTH:
Jamie Madillf2f6d372018-01-10 21:37:23 -0500733 mPipelineDesc->updateLineWidth(glState.getLineWidth());
Jamie Madillebf72992017-10-13 14:09:45 -0400734 break;
735 case gl::State::DIRTY_BIT_PRIMITIVE_RESTART_ENABLED:
Jamie Madillebf72992017-10-13 14:09:45 -0400736 break;
737 case gl::State::DIRTY_BIT_CLEAR_COLOR:
Jamie Madillf4d693c2018-02-14 16:38:16 -0500738 mClearColorValue.color.float32[0] = glState.getColorClearValue().red;
739 mClearColorValue.color.float32[1] = glState.getColorClearValue().green;
740 mClearColorValue.color.float32[2] = glState.getColorClearValue().blue;
741 mClearColorValue.color.float32[3] = glState.getColorClearValue().alpha;
Jamie Madillebf72992017-10-13 14:09:45 -0400742 break;
743 case gl::State::DIRTY_BIT_CLEAR_DEPTH:
Jamie Madillf4d693c2018-02-14 16:38:16 -0500744 mClearDepthStencilValue.depthStencil.depth = glState.getDepthClearValue();
Jamie Madillebf72992017-10-13 14:09:45 -0400745 break;
746 case gl::State::DIRTY_BIT_CLEAR_STENCIL:
Jamie Madillf4d693c2018-02-14 16:38:16 -0500747 mClearDepthStencilValue.depthStencil.stencil =
748 static_cast<uint32_t>(glState.getStencilClearValue());
Jamie Madillebf72992017-10-13 14:09:45 -0400749 break;
Jamie Madillc67323a2017-11-02 23:11:41 -0400750 case gl::State::DIRTY_BIT_UNPACK_STATE:
Luc Ferronf9749ea2018-04-24 15:34:53 -0400751 // This is a no-op, its only important to use the right unpack state when we do
752 // setImage or setSubImage in TextureVk, which is plumbed through the frontend call
Jamie Madillebf72992017-10-13 14:09:45 -0400753 break;
Corentin Wallez29a20992017-11-06 18:23:16 -0500754 case gl::State::DIRTY_BIT_UNPACK_BUFFER_BINDING:
Corentin Wallez29a20992017-11-06 18:23:16 -0500755 break;
Jamie Madillc67323a2017-11-02 23:11:41 -0400756 case gl::State::DIRTY_BIT_PACK_STATE:
Luc Ferrona1c72422018-05-14 15:58:28 -0400757 // This is a no-op, its only important to use the right pack state when we do
758 // call readPixels later on.
Jamie Madillebf72992017-10-13 14:09:45 -0400759 break;
Corentin Wallez29a20992017-11-06 18:23:16 -0500760 case gl::State::DIRTY_BIT_PACK_BUFFER_BINDING:
Corentin Wallez29a20992017-11-06 18:23:16 -0500761 break;
Jamie Madillebf72992017-10-13 14:09:45 -0400762 case gl::State::DIRTY_BIT_DITHER_ENABLED:
Jamie Madillebf72992017-10-13 14:09:45 -0400763 break;
764 case gl::State::DIRTY_BIT_GENERATE_MIPMAP_HINT:
Jamie Madillebf72992017-10-13 14:09:45 -0400765 break;
766 case gl::State::DIRTY_BIT_SHADER_DERIVATIVE_HINT:
Jamie Madillebf72992017-10-13 14:09:45 -0400767 break;
768 case gl::State::DIRTY_BIT_READ_FRAMEBUFFER_BINDING:
Luc Ferron82eda932018-07-09 15:10:22 -0400769 updateFlipViewportReadFramebuffer(context->getGLState());
Jamie Madillebf72992017-10-13 14:09:45 -0400770 break;
771 case gl::State::DIRTY_BIT_DRAW_FRAMEBUFFER_BINDING:
Luc Ferron1a135ad2018-07-04 10:35:31 -0400772 {
Luc Ferrone8356092018-07-12 12:36:47 -0400773 ANGLE_TRY(updateDriverUniforms(glState));
774 updateFlipViewportDrawFramebuffer(glState);
Luc Ferron1a135ad2018-07-04 10:35:31 -0400775 FramebufferVk *framebufferVk = vk::GetImpl(glState.getDrawFramebuffer());
776 mPipelineDesc->updateViewport(framebufferVk, glState.getViewport(),
777 glState.getNearPlane(), glState.getFarPlane(),
Luc Ferron82eda932018-07-09 15:10:22 -0400778 isViewportFlipEnabledForDrawFBO());
Luc Ferron0bb940a2018-06-22 09:59:34 -0400779 updateColorMask(glState.getBlendState());
Luc Ferronb70ad522018-07-09 16:06:26 -0400780 mPipelineDesc->updateCullMode(glState.getRasterizerState());
Luc Ferronbf6dc372018-06-28 15:24:19 -0400781 updateScissor(glState);
Geoff Langc16f5182018-07-18 10:40:03 -0400782 mPipelineDesc->updateDepthTestEnabled(glState.getDepthStencilState(),
783 glState.getDrawFramebuffer());
784 mPipelineDesc->updateDepthWriteEnabled(glState.getDepthStencilState(),
785 glState.getDrawFramebuffer());
786 mPipelineDesc->updateStencilTestEnabled(glState.getDepthStencilState(),
787 glState.getDrawFramebuffer());
788 mPipelineDesc->updateStencilFrontWriteMask(glState.getDepthStencilState(),
789 glState.getDrawFramebuffer());
790 mPipelineDesc->updateStencilBackWriteMask(glState.getDepthStencilState(),
791 glState.getDrawFramebuffer());
Jamie Madillebf72992017-10-13 14:09:45 -0400792 break;
Luc Ferron1a135ad2018-07-04 10:35:31 -0400793 }
Jamie Madillebf72992017-10-13 14:09:45 -0400794 case gl::State::DIRTY_BIT_RENDERBUFFER_BINDING:
Jamie Madillebf72992017-10-13 14:09:45 -0400795 break;
796 case gl::State::DIRTY_BIT_VERTEX_ARRAY_BINDING:
Jamie Madill5a4c9322018-07-16 11:01:58 -0400797 {
Jamie Madill88fc6da2018-08-30 16:18:36 -0400798 invalidateDefaultAttributes(context->getStateCache().getActiveDefaultAttribsMask());
Jamie Madillebf72992017-10-13 14:09:45 -0400799 break;
Jamie Madill5a4c9322018-07-16 11:01:58 -0400800 }
Jamie Madillebf72992017-10-13 14:09:45 -0400801 case gl::State::DIRTY_BIT_DRAW_INDIRECT_BUFFER_BINDING:
Jamie Madillebf72992017-10-13 14:09:45 -0400802 break;
Qin Jiajiaa98a2812017-11-30 18:12:06 +0800803 case gl::State::DIRTY_BIT_DISPATCH_INDIRECT_BUFFER_BINDING:
Qin Jiajiaa98a2812017-11-30 18:12:06 +0800804 break;
Jamie Madillebf72992017-10-13 14:09:45 -0400805 case gl::State::DIRTY_BIT_PROGRAM_BINDING:
Jamie Madillebf72992017-10-13 14:09:45 -0400806 break;
807 case gl::State::DIRTY_BIT_PROGRAM_EXECUTABLE:
808 {
Jamie Madill88fc6da2018-08-30 16:18:36 -0400809 mTexturesDirty = true;
Jamie Madill06ca6342018-07-12 15:56:53 -0400810 // No additional work is needed here. We will update the pipeline desc later.
Jamie Madill88fc6da2018-08-30 16:18:36 -0400811 invalidateDefaultAttributes(context->getStateCache().getActiveDefaultAttribsMask());
Jamie Madillebf72992017-10-13 14:09:45 -0400812 break;
813 }
814 case gl::State::DIRTY_BIT_TEXTURE_BINDINGS:
Jamie Madill88fc6da2018-08-30 16:18:36 -0400815 mTexturesDirty = true;
Jamie Madillebf72992017-10-13 14:09:45 -0400816 break;
817 case gl::State::DIRTY_BIT_SAMPLER_BINDINGS:
Jamie Madill88fc6da2018-08-30 16:18:36 -0400818 mTexturesDirty = true;
Jamie Madillebf72992017-10-13 14:09:45 -0400819 break;
Geoff Langded79232017-11-28 15:21:11 -0500820 case gl::State::DIRTY_BIT_TRANSFORM_FEEDBACK_BINDING:
Geoff Langded79232017-11-28 15:21:11 -0500821 break;
Xinghua Cao10a4d432017-11-28 14:46:26 +0800822 case gl::State::DIRTY_BIT_SHADER_STORAGE_BUFFER_BINDING:
Xinghua Cao10a4d432017-11-28 14:46:26 +0800823 break;
Jamie Madillf4141212017-12-12 15:08:07 -0500824 case gl::State::DIRTY_BIT_UNIFORM_BUFFER_BINDINGS:
Jamie Madillf4141212017-12-12 15:08:07 -0500825 break;
Jamie Madillebf72992017-10-13 14:09:45 -0400826 case gl::State::DIRTY_BIT_MULTISAMPLING:
Jamie Madillebf72992017-10-13 14:09:45 -0400827 break;
828 case gl::State::DIRTY_BIT_SAMPLE_ALPHA_TO_ONE:
Jamie Madillebf72992017-10-13 14:09:45 -0400829 break;
830 case gl::State::DIRTY_BIT_COVERAGE_MODULATION:
Jamie Madillebf72992017-10-13 14:09:45 -0400831 break;
832 case gl::State::DIRTY_BIT_PATH_RENDERING_MATRIX_MV:
Jamie Madillebf72992017-10-13 14:09:45 -0400833 break;
834 case gl::State::DIRTY_BIT_PATH_RENDERING_MATRIX_PROJ:
Jamie Madillebf72992017-10-13 14:09:45 -0400835 break;
836 case gl::State::DIRTY_BIT_PATH_RENDERING_STENCIL_STATE:
Jamie Madillebf72992017-10-13 14:09:45 -0400837 break;
838 case gl::State::DIRTY_BIT_FRAMEBUFFER_SRGB:
Jamie Madillebf72992017-10-13 14:09:45 -0400839 break;
Jamie Madillc67323a2017-11-02 23:11:41 -0400840 case gl::State::DIRTY_BIT_CURRENT_VALUES:
Jamie Madill5a4c9322018-07-16 11:01:58 -0400841 {
Jamie Madill88fc6da2018-08-30 16:18:36 -0400842 invalidateDefaultAttributes(glState.getAndResetDirtyCurrentValues());
Jamie Madillc67323a2017-11-02 23:11:41 -0400843 break;
Jamie Madill5a4c9322018-07-16 11:01:58 -0400844 }
845 break;
Jamie Madillebf72992017-10-13 14:09:45 -0400846 default:
Jamie Madillc67323a2017-11-02 23:11:41 -0400847 UNREACHABLE();
Jamie Madillebf72992017-10-13 14:09:45 -0400848 break;
849 }
850 }
Jamie Madill5547b382017-10-23 18:16:01 -0400851
Jamie Madill189ad872018-07-09 13:32:37 -0400852 return gl::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400853}
854
855GLint ContextVk::getGPUDisjoint()
856{
857 UNIMPLEMENTED();
858 return GLint();
859}
860
861GLint64 ContextVk::getTimestamp()
862{
863 UNIMPLEMENTED();
864 return GLint64();
865}
866
Luc Ferron5396f2a2018-07-12 08:24:23 -0400867gl::Error ContextVk::onMakeCurrent(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400868{
Geoff Langcaa55cd2018-07-05 13:19:35 -0400869 // Flip viewports if FeaturesVk::flipViewportY is enabled and the user did not request that the
870 // surface is flipped.
871 egl::Surface *drawSurface = context->getCurrentDrawSurface();
872 mFlipYForCurrentSurface =
873 drawSurface != nullptr && mRenderer->getFeatures().flipViewportY &&
874 !IsMaskFlagSet(drawSurface->getOrientation(), EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE);
Luc Ferron82eda932018-07-09 15:10:22 -0400875
876 const gl::State &glState = context->getGLState();
877 updateFlipViewportDrawFramebuffer(glState);
878 updateFlipViewportReadFramebuffer(glState);
Luc Ferrone8356092018-07-12 12:36:47 -0400879 ANGLE_TRY(updateDriverUniforms(glState));
Luc Ferron5396f2a2018-07-12 08:24:23 -0400880 return gl::NoError();
Luc Ferron82eda932018-07-09 15:10:22 -0400881}
882
883void ContextVk::updateFlipViewportDrawFramebuffer(const gl::State &glState)
884{
885 gl::Framebuffer *drawFramebuffer = glState.getDrawFramebuffer();
886 mFlipViewportForDrawFramebuffer =
887 drawFramebuffer->isDefault() && mRenderer->getFeatures().flipViewportY;
888}
889
890void ContextVk::updateFlipViewportReadFramebuffer(const gl::State &glState)
891{
892 gl::Framebuffer *readFramebuffer = glState.getReadFramebuffer();
893 mFlipViewportForReadFramebuffer =
894 readFramebuffer->isDefault() && mRenderer->getFeatures().flipViewportY;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400895}
896
Jiawei Shaod0a7d102018-05-07 12:40:20 +0800897gl::Caps ContextVk::getNativeCaps() const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400898{
Jamie Madillacccc6c2016-05-03 17:22:10 -0400899 return mRenderer->getNativeCaps();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400900}
901
902const gl::TextureCapsMap &ContextVk::getNativeTextureCaps() const
903{
Jamie Madillacccc6c2016-05-03 17:22:10 -0400904 return mRenderer->getNativeTextureCaps();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400905}
906
907const gl::Extensions &ContextVk::getNativeExtensions() const
908{
Jamie Madillacccc6c2016-05-03 17:22:10 -0400909 return mRenderer->getNativeExtensions();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400910}
911
912const gl::Limitations &ContextVk::getNativeLimitations() const
913{
Jamie Madillacccc6c2016-05-03 17:22:10 -0400914 return mRenderer->getNativeLimitations();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400915}
916
917CompilerImpl *ContextVk::createCompiler()
918{
Jamie Madillacccc6c2016-05-03 17:22:10 -0400919 return new CompilerVk();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400920}
921
Jamie Madillacccc6c2016-05-03 17:22:10 -0400922ShaderImpl *ContextVk::createShader(const gl::ShaderState &state)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400923{
Jamie Madillacccc6c2016-05-03 17:22:10 -0400924 return new ShaderVk(state);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400925}
926
Jamie Madillacccc6c2016-05-03 17:22:10 -0400927ProgramImpl *ContextVk::createProgram(const gl::ProgramState &state)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400928{
Jamie Madillacccc6c2016-05-03 17:22:10 -0400929 return new ProgramVk(state);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400930}
931
Jamie Madillacccc6c2016-05-03 17:22:10 -0400932FramebufferImpl *ContextVk::createFramebuffer(const gl::FramebufferState &state)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400933{
Jamie Madill639bc902018-07-18 17:08:27 -0400934 return FramebufferVk::CreateUserFBO(mRenderer, state);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400935}
936
937TextureImpl *ContextVk::createTexture(const gl::TextureState &state)
938{
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400939 return new TextureVk(state, mRenderer);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400940}
941
Jamie Madille703c602018-02-20 10:21:48 -0500942RenderbufferImpl *ContextVk::createRenderbuffer(const gl::RenderbufferState &state)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400943{
Jamie Madille703c602018-02-20 10:21:48 -0500944 return new RenderbufferVk(state);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400945}
946
Jamie Madill8f775602016-11-03 16:45:34 -0400947BufferImpl *ContextVk::createBuffer(const gl::BufferState &state)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400948{
Jamie Madill8f775602016-11-03 16:45:34 -0400949 return new BufferVk(state);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400950}
951
Jamie Madillacccc6c2016-05-03 17:22:10 -0400952VertexArrayImpl *ContextVk::createVertexArray(const gl::VertexArrayState &state)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400953{
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400954 return new VertexArrayVk(state, mRenderer);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400955}
956
Corentin Wallezad3ae902018-03-09 13:40:42 -0500957QueryImpl *ContextVk::createQuery(gl::QueryType type)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400958{
Jamie Madillacccc6c2016-05-03 17:22:10 -0400959 return new QueryVk(type);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400960}
961
962FenceNVImpl *ContextVk::createFenceNV()
963{
Jamie Madillacccc6c2016-05-03 17:22:10 -0400964 return new FenceNVVk();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400965}
966
Jamie Madill70b5bb02017-08-28 13:32:37 -0400967SyncImpl *ContextVk::createSync()
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400968{
Jamie Madill70b5bb02017-08-28 13:32:37 -0400969 return new SyncVk();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400970}
971
Geoff Lang73bd2182016-07-15 13:01:24 -0400972TransformFeedbackImpl *ContextVk::createTransformFeedback(const gl::TransformFeedbackState &state)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400973{
Geoff Lang73bd2182016-07-15 13:01:24 -0400974 return new TransformFeedbackVk(state);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400975}
976
Jamie Madill06ef36b2017-09-09 23:32:46 -0400977SamplerImpl *ContextVk::createSampler(const gl::SamplerState &state)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400978{
Jamie Madill06ef36b2017-09-09 23:32:46 -0400979 return new SamplerVk(state);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400980}
981
Yunchao Hea336b902017-08-02 16:05:21 +0800982ProgramPipelineImpl *ContextVk::createProgramPipeline(const gl::ProgramPipelineState &state)
983{
984 return new ProgramPipelineVk(state);
985}
986
Sami Väisänene45e53b2016-05-25 10:36:04 +0300987std::vector<PathImpl *> ContextVk::createPaths(GLsizei)
988{
989 return std::vector<PathImpl *>();
990}
991
Jamie Madill72106562017-03-24 14:18:50 -0400992void ContextVk::invalidateCurrentPipeline()
993{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400994 mPipelineDirty = true;
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500995 mCurrentPipeline = nullptr;
Jamie Madill72106562017-03-24 14:18:50 -0400996}
997
Jamie Madillfe548342017-06-19 11:13:24 -0400998gl::Error ContextVk::dispatchCompute(const gl::Context *context,
999 GLuint numGroupsX,
1000 GLuint numGroupsY,
1001 GLuint numGroupsZ)
Xinghua Cao2b396592017-03-29 15:36:04 +08001002{
1003 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001004 return gl::InternalError();
Xinghua Cao2b396592017-03-29 15:36:04 +08001005}
1006
Qin Jiajia62fcf622017-11-30 16:16:12 +08001007gl::Error ContextVk::dispatchComputeIndirect(const gl::Context *context, GLintptr indirect)
1008{
1009 UNIMPLEMENTED();
1010 return gl::InternalError();
1011}
1012
Xinghua Cao89c422a2017-11-29 18:24:20 +08001013gl::Error ContextVk::memoryBarrier(const gl::Context *context, GLbitfield barriers)
1014{
1015 UNIMPLEMENTED();
1016 return gl::InternalError();
1017}
1018
1019gl::Error ContextVk::memoryBarrierByRegion(const gl::Context *context, GLbitfield barriers)
1020{
1021 UNIMPLEMENTED();
1022 return gl::InternalError();
1023}
1024
Jamie Madilledeaa832018-06-22 09:18:41 -04001025vk::DynamicDescriptorPool *ContextVk::getDynamicDescriptorPool(uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -04001026{
Jamie Madilledeaa832018-06-22 09:18:41 -04001027 return &mDynamicDescriptorPools[descriptorSetIndex];
Jamie Madill76e471e2017-10-21 09:56:01 -04001028}
1029
Jamie Madillf4d693c2018-02-14 16:38:16 -05001030const VkClearValue &ContextVk::getClearColorValue() const
1031{
1032 return mClearColorValue;
1033}
1034
1035const VkClearValue &ContextVk::getClearDepthStencilValue() const
1036{
1037 return mClearDepthStencilValue;
1038}
1039
Jamie Madill9aef3672018-04-27 11:45:06 -04001040VkColorComponentFlags ContextVk::getClearColorMask() const
1041{
1042 return mClearColorMask;
1043}
Jamie Madill834a3a12018-07-09 13:32:39 -04001044
Jamie Madill1266d202018-06-29 09:11:34 -04001045const FeaturesVk &ContextVk::getFeatures() const
1046{
1047 return mRenderer->getFeatures();
1048}
Jamie Madill834a3a12018-07-09 13:32:39 -04001049
Luc Ferrone8356092018-07-12 12:36:47 -04001050angle::Result ContextVk::updateDriverUniforms(const gl::State &glState)
Jamie Madill834a3a12018-07-09 13:32:39 -04001051{
Jamie Madill834a3a12018-07-09 13:32:39 -04001052 // Release any previously retained buffers.
1053 mDriverUniformsBuffer.releaseRetainedBuffers(mRenderer);
1054
Luc Ferrone8356092018-07-12 12:36:47 -04001055 const gl::Rectangle &glViewport = glState.getViewport();
Jamie Madill834a3a12018-07-09 13:32:39 -04001056
1057 // Allocate a new region in the dynamic buffer.
1058 uint8_t *ptr = nullptr;
1059 VkBuffer buffer = VK_NULL_HANDLE;
Jamie Madill4c310832018-08-29 13:43:17 -04001060 VkDeviceSize offset = 0;
Jamie Madill834a3a12018-07-09 13:32:39 -04001061 bool newBufferAllocated = false;
Jamie Madill21061022018-07-12 23:56:30 -04001062 ANGLE_TRY(mDriverUniformsBuffer.allocate(this, sizeof(DriverUniforms), &ptr, &buffer, &offset,
1063 &newBufferAllocated));
Luc Ferron9ff9c772018-07-11 13:08:18 -04001064 float scaleY = isViewportFlipEnabledForDrawFBO() ? 1.0f : -1.0f;
Jamie Madill834a3a12018-07-09 13:32:39 -04001065
Luc Ferrone8356092018-07-12 12:36:47 -04001066 float depthRangeNear = glState.getNearPlane();
1067 float depthRangeFar = glState.getFarPlane();
1068 float depthRangeDiff = depthRangeFar - depthRangeNear;
1069
Jamie Madill834a3a12018-07-09 13:32:39 -04001070 // Copy and flush to the device.
1071 DriverUniforms *driverUniforms = reinterpret_cast<DriverUniforms *>(ptr);
Luc Ferron9ff9c772018-07-11 13:08:18 -04001072 *driverUniforms = {
1073 {static_cast<float>(glViewport.x), static_cast<float>(glViewport.y),
1074 static_cast<float>(glViewport.width), static_cast<float>(glViewport.height)},
Luc Ferrone8356092018-07-12 12:36:47 -04001075 {1.0f, scaleY, 1.0f, 1.0f},
1076 {depthRangeNear, depthRangeFar, depthRangeDiff, 0.0f}};
Jamie Madill834a3a12018-07-09 13:32:39 -04001077
Jamie Madill21061022018-07-12 23:56:30 -04001078 ANGLE_TRY(mDriverUniformsBuffer.flush(this));
Jamie Madill834a3a12018-07-09 13:32:39 -04001079
1080 // Get the descriptor set layout.
1081 if (!mDriverUniformsSetLayout.valid())
1082 {
1083 vk::DescriptorSetLayoutDesc desc;
1084 desc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
1085
Jamie Madill21061022018-07-12 23:56:30 -04001086 ANGLE_TRY(mRenderer->getDescriptorSetLayout(this, desc, &mDriverUniformsSetLayout));
Jamie Madill834a3a12018-07-09 13:32:39 -04001087 }
1088
1089 // Allocate a new descriptor set.
1090 ANGLE_TRY(mDynamicDescriptorPools[kDriverUniformsDescriptorSetIndex].allocateSets(
1091 this, mDriverUniformsSetLayout.get().ptr(), 1, &mDriverUniformsDescriptorSet));
1092
1093 // Update the driver uniform descriptor set.
1094 VkDescriptorBufferInfo bufferInfo;
1095 bufferInfo.buffer = buffer;
1096 bufferInfo.offset = offset;
1097 bufferInfo.range = sizeof(DriverUniforms);
1098
1099 VkWriteDescriptorSet writeInfo;
1100 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1101 writeInfo.pNext = nullptr;
1102 writeInfo.dstSet = mDriverUniformsDescriptorSet;
1103 writeInfo.dstBinding = 0;
1104 writeInfo.dstArrayElement = 0;
1105 writeInfo.descriptorCount = 1;
1106 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1107 writeInfo.pImageInfo = nullptr;
1108 writeInfo.pTexelBufferView = nullptr;
1109 writeInfo.pBufferInfo = &bufferInfo;
1110
1111 vkUpdateDescriptorSets(getDevice(), 1, &writeInfo, 0, nullptr);
1112
Jamie Madill21061022018-07-12 23:56:30 -04001113 return angle::Result::Continue();
1114}
1115
1116void ContextVk::handleError(VkResult errorCode, const char *file, unsigned int line)
1117{
1118 GLenum glErrorCode = DefaultGLErrorCode(errorCode);
1119
1120 std::stringstream errorStream;
1121 errorStream << "Internal Vulkan error: " << VulkanResultString(errorCode) << ", in " << file
1122 << ", line " << line << ".";
1123
1124 mErrors->handleError(gl::Error(glErrorCode, glErrorCode, errorStream.str()));
Jamie Madill834a3a12018-07-09 13:32:39 -04001125}
Jamie Madill84c662b2018-07-12 15:56:55 -04001126
Jamie Madill88fc6da2018-08-30 16:18:36 -04001127angle::Result ContextVk::updateActiveTextures(const gl::Context *context)
Jamie Madill84c662b2018-07-12 15:56:55 -04001128{
Jamie Madill4787d702018-08-08 15:49:26 -04001129 const gl::State &glState = mState.getState();
1130 const gl::Program *program = glState.getProgram();
Jamie Madill84c662b2018-07-12 15:56:55 -04001131
1132 mActiveTextures.fill(nullptr);
1133
Jamie Madill4787d702018-08-08 15:49:26 -04001134 const gl::ActiveTexturePointerArray &textures = glState.getActiveTexturesCache();
1135 const gl::ActiveTextureMask &activeTextures = program->getActiveSamplersMask();
1136 const gl::ActiveTextureTypeArray &textureTypes = program->getActiveSamplerTypes();
1137
1138 for (size_t textureUnit : activeTextures)
Jamie Madill84c662b2018-07-12 15:56:55 -04001139 {
Jamie Madill4787d702018-08-08 15:49:26 -04001140 gl::Texture *texture = textures[textureUnit];
1141 gl::TextureType textureType = textureTypes[textureUnit];
Jamie Madill84c662b2018-07-12 15:56:55 -04001142
Jamie Madill4787d702018-08-08 15:49:26 -04001143 // Null textures represent incomplete textures.
1144 if (texture == nullptr)
Jamie Madill84c662b2018-07-12 15:56:55 -04001145 {
Jamie Madill88fc6da2018-08-30 16:18:36 -04001146 ANGLE_TRY_HANDLE(context, getIncompleteTexture(context, textureType, &texture));
Jamie Madill84c662b2018-07-12 15:56:55 -04001147 }
Jamie Madill4787d702018-08-08 15:49:26 -04001148
1149 mActiveTextures[textureUnit] = vk::GetImpl(texture);
Jamie Madill84c662b2018-07-12 15:56:55 -04001150 }
1151
Jamie Madill88fc6da2018-08-30 16:18:36 -04001152 return angle::Result::Continue();
Jamie Madill84c662b2018-07-12 15:56:55 -04001153}
1154
1155const gl::ActiveTextureArray<TextureVk *> &ContextVk::getActiveTextures() const
1156{
1157 return mActiveTextures;
1158}
Jamie Madill5a4c9322018-07-16 11:01:58 -04001159
1160void ContextVk::invalidateDefaultAttribute(size_t attribIndex)
1161{
Jamie Madill88fc6da2018-08-30 16:18:36 -04001162 mDirtyDefaultAttribsMask.set(attribIndex);
1163 mDirtyDefaultAttribs = true;
1164}
1165
1166void ContextVk::invalidateDefaultAttributes(const gl::AttributesMask &dirtyMask)
1167{
1168 if (dirtyMask.any())
1169 {
1170 mDirtyDefaultAttribsMask = dirtyMask;
1171 mDirtyDefaultAttribs = true;
1172 }
Jamie Madill5a4c9322018-07-16 11:01:58 -04001173}
1174
1175angle::Result ContextVk::updateDefaultAttributes()
1176{
Jamie Madill88fc6da2018-08-30 16:18:36 -04001177 ASSERT(mDirtyDefaultAttribsMask.any());
Jamie Madill5a4c9322018-07-16 11:01:58 -04001178
Jamie Madill88fc6da2018-08-30 16:18:36 -04001179 for (size_t attribIndex : mDirtyDefaultAttribsMask)
Jamie Madill5a4c9322018-07-16 11:01:58 -04001180 {
1181 ANGLE_TRY(updateDefaultAttribute(attribIndex))
1182 }
1183
Jamie Madill88fc6da2018-08-30 16:18:36 -04001184 mDirtyDefaultAttribsMask.reset();
Jamie Madill5a4c9322018-07-16 11:01:58 -04001185 return angle::Result::Continue();
1186}
1187
1188angle::Result ContextVk::updateDefaultAttribute(size_t attribIndex)
1189{
1190 vk::DynamicBuffer &defaultBuffer = mDefaultAttribBuffers[attribIndex];
1191
1192 defaultBuffer.releaseRetainedBuffers(mRenderer);
1193
1194 uint8_t *ptr;
1195 VkBuffer bufferHandle = VK_NULL_HANDLE;
Jamie Madill4c310832018-08-29 13:43:17 -04001196 VkDeviceSize offset = 0;
Jamie Madill5a4c9322018-07-16 11:01:58 -04001197 ANGLE_TRY(
1198 defaultBuffer.allocate(this, kDefaultValueSize, &ptr, &bufferHandle, &offset, nullptr));
1199
1200 const gl::State &glState = mState.getState();
1201 const gl::VertexAttribCurrentValueData &defaultValue =
1202 glState.getVertexAttribCurrentValues()[attribIndex];
1203
1204 ASSERT(defaultValue.Type == GL_FLOAT);
1205
1206 memcpy(ptr, defaultValue.FloatValues, kDefaultValueSize);
1207
1208 ANGLE_TRY(defaultBuffer.flush(this));
1209
1210 VertexArrayVk *vertexArrayVk = vk::GetImpl(glState.getVertexArray());
Jamie Madill4c310832018-08-29 13:43:17 -04001211 vertexArrayVk->updateDefaultAttrib(mRenderer, attribIndex, bufferHandle,
1212 static_cast<uint32_t>(offset));
Jamie Madill5a4c9322018-07-16 11:01:58 -04001213 return angle::Result::Continue();
1214}
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001215} // namespace rx