jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/gpu/GrContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/core/SkMipMap.h" |
| 10 | #include "src/gpu/GrContextPriv.h" |
| 11 | #include "src/gpu/GrPipeline.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrTexturePriv.h" |
| 14 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 15 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 16 | #include "src/gpu/glsl/GrGLSLXferProcessor.h" |
| 17 | #include "src/gpu/vk/GrVkBufferView.h" |
| 18 | #include "src/gpu/vk/GrVkCommandBuffer.h" |
| 19 | #include "src/gpu/vk/GrVkDescriptorPool.h" |
| 20 | #include "src/gpu/vk/GrVkDescriptorSet.h" |
| 21 | #include "src/gpu/vk/GrVkGpu.h" |
| 22 | #include "src/gpu/vk/GrVkImageView.h" |
| 23 | #include "src/gpu/vk/GrVkMemory.h" |
| 24 | #include "src/gpu/vk/GrVkPipeline.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/vk/GrVkPipelineState.h" |
| 26 | #include "src/gpu/vk/GrVkSampler.h" |
| 27 | #include "src/gpu/vk/GrVkTexture.h" |
| 28 | #include "src/gpu/vk/GrVkUniformBuffer.h" |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 29 | |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 30 | GrVkPipelineState::GrVkPipelineState( |
| 31 | GrVkGpu* gpu, |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 32 | GrVkPipeline* pipeline, |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 33 | const GrVkDescriptorSetManager::Handle& samplerDSHandle, |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 34 | const GrGLSLBuiltinUniformHandles& builtinUniformHandles, |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 35 | const UniformInfoArray& uniforms, |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 36 | uint32_t uniformSize, |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 37 | const UniformInfoArray& samplers, |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 38 | std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor, |
| 39 | std::unique_ptr<GrGLSLXferProcessor> xferProcessor, |
| 40 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors, |
| 41 | int fragmentProcessorCnt) |
| 42 | : fPipeline(pipeline) |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 43 | , fUniformDescriptorSet(nullptr) |
| 44 | , fSamplerDescriptorSet(nullptr) |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 45 | , fSamplerDSHandle(samplerDSHandle) |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 46 | , fBuiltinUniformHandles(builtinUniformHandles) |
| 47 | , fGeometryProcessor(std::move(geometryProcessor)) |
| 48 | , fXferProcessor(std::move(xferProcessor)) |
| 49 | , fFragmentProcessors(std::move(fragmentProcessors)) |
| 50 | , fFragmentProcessorCnt(fragmentProcessorCnt) |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 51 | , fDataManager(uniforms, uniformSize) { |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 52 | fDescriptorSets[0] = VK_NULL_HANDLE; |
| 53 | fDescriptorSets[1] = VK_NULL_HANDLE; |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 54 | fDescriptorSets[2] = VK_NULL_HANDLE; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 55 | |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 56 | fUniformBuffer.reset(GrVkUniformBuffer::Create(gpu, uniformSize)); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 57 | |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 58 | fNumSamplers = samplers.count(); |
| 59 | |
| 60 | for (int i = 0; i < fNumSamplers; ++i) { |
| 61 | // We store the immutable samplers here and take ownership of the ref from the |
| 62 | // GrVkUnformHandler. |
| 63 | fImmutableSamplers.push_back(samplers[i].fImmutableSampler); |
| 64 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 65 | } |
| 66 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 67 | GrVkPipelineState::~GrVkPipelineState() { |
Ethan Nicholas | 87f340e | 2017-01-03 14:32:01 -0500 | [diff] [blame] | 68 | // Must have freed all GPU resources before this is destroyed |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 69 | SkASSERT(!fPipeline); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 72 | void GrVkPipelineState::freeGPUResources(GrVkGpu* gpu) { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 73 | if (fPipeline) { |
| 74 | fPipeline->unref(gpu); |
| 75 | fPipeline = nullptr; |
| 76 | } |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 77 | |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 78 | if (fUniformBuffer) { |
| 79 | fUniformBuffer->release(gpu); |
| 80 | fUniformBuffer.reset(); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 81 | } |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 82 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 83 | if (fUniformDescriptorSet) { |
| 84 | fUniformDescriptorSet->recycle(const_cast<GrVkGpu*>(gpu)); |
| 85 | fUniformDescriptorSet = nullptr; |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 86 | } |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 87 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 88 | if (fSamplerDescriptorSet) { |
| 89 | fSamplerDescriptorSet->recycle(const_cast<GrVkGpu*>(gpu)); |
| 90 | fSamplerDescriptorSet = nullptr; |
| 91 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 92 | } |
| 93 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 94 | void GrVkPipelineState::abandonGPUResources() { |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame] | 95 | if (fPipeline) { |
| 96 | fPipeline->unrefAndAbandon(); |
| 97 | fPipeline = nullptr; |
| 98 | } |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 99 | |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 100 | if (fUniformBuffer) { |
| 101 | fUniformBuffer->abandon(); |
| 102 | fUniformBuffer.reset(); |
Greg Daniel | adb4bfe | 2018-08-23 16:15:05 -0400 | [diff] [blame] | 103 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 104 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 105 | if (fUniformDescriptorSet) { |
| 106 | fUniformDescriptorSet->unrefAndAbandon(); |
| 107 | fUniformDescriptorSet = nullptr; |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 108 | } |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 109 | |
| 110 | if (fSamplerDescriptorSet) { |
| 111 | fSamplerDescriptorSet->unrefAndAbandon(); |
| 112 | fSamplerDescriptorSet = nullptr; |
| 113 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 116 | void GrVkPipelineState::setAndBindUniforms(GrVkGpu* gpu, |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 117 | const GrRenderTarget* renderTarget, |
| 118 | GrSurfaceOrigin origin, |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 119 | const GrPrimitiveProcessor& primProc, |
| 120 | const GrPipeline& pipeline, |
| 121 | GrVkCommandBuffer* commandBuffer) { |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 122 | this->setRenderTargetState(renderTarget, origin); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 123 | |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 124 | fGeometryProcessor->setData(fDataManager, primProc, |
| 125 | GrFragmentProcessor::CoordTransformIter(pipeline)); |
| 126 | GrFragmentProcessor::Iter iter(pipeline); |
| 127 | GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt); |
| 128 | const GrFragmentProcessor* fp = iter.next(); |
| 129 | GrGLSLFragmentProcessor* glslFP = glslIter.next(); |
| 130 | while (fp && glslFP) { |
| 131 | glslFP->setData(fDataManager, *fp); |
| 132 | fp = iter.next(); |
| 133 | glslFP = glslIter.next(); |
| 134 | } |
| 135 | SkASSERT(!fp && !glslFP); |
| 136 | |
| 137 | { |
| 138 | SkIPoint offset; |
| 139 | GrTexture* dstTexture = pipeline.peekDstTexture(&offset); |
| 140 | |
| 141 | fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset); |
| 142 | } |
| 143 | |
| 144 | // Get new descriptor set |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 145 | if (fUniformBuffer) { |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 146 | int uniformDSIdx = GrVkUniformHandler::kUniformBufferDescSet; |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 147 | if (fDataManager.uploadUniformBuffers(gpu, fUniformBuffer.get()) || |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 148 | !fUniformDescriptorSet) { |
| 149 | if (fUniformDescriptorSet) { |
| 150 | fUniformDescriptorSet->recycle(gpu); |
| 151 | } |
| 152 | fUniformDescriptorSet = gpu->resourceProvider().getUniformDescriptorSet(); |
| 153 | fDescriptorSets[uniformDSIdx] = fUniformDescriptorSet->descriptorSet(); |
| 154 | this->writeUniformBuffers(gpu); |
| 155 | } |
Greg Daniel | eecc687 | 2019-07-29 13:21:37 -0400 | [diff] [blame] | 156 | commandBuffer->bindDescriptorSets(gpu, this, fPipeline->layout(), uniformDSIdx, 1, |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 157 | &fDescriptorSets[uniformDSIdx], 0, nullptr); |
| 158 | if (fUniformDescriptorSet) { |
| 159 | commandBuffer->addRecycledResource(fUniformDescriptorSet); |
| 160 | } |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 161 | if (fUniformBuffer) { |
| 162 | commandBuffer->addRecycledResource(fUniformBuffer->resource()); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void GrVkPipelineState::setAndBindTextures(GrVkGpu* gpu, |
| 168 | const GrPrimitiveProcessor& primProc, |
| 169 | const GrPipeline& pipeline, |
| 170 | const GrTextureProxy* const primProcTextures[], |
| 171 | GrVkCommandBuffer* commandBuffer) { |
| 172 | SkASSERT(primProcTextures || !primProc.numTextureSamplers()); |
| 173 | |
| 174 | struct SamplerBindings { |
| 175 | GrSamplerState fState; |
| 176 | GrVkTexture* fTexture; |
| 177 | }; |
Brian Salomon | 5e81a12 | 2018-08-23 16:46:07 -0400 | [diff] [blame] | 178 | SkAutoSTMalloc<8, SamplerBindings> samplerBindings(fNumSamplers); |
| 179 | int currTextureBinding = 0; |
| 180 | |
| 181 | fGeometryProcessor->setData(fDataManager, primProc, |
| 182 | GrFragmentProcessor::CoordTransformIter(pipeline)); |
| 183 | for (int i = 0; i < primProc.numTextureSamplers(); ++i) { |
| 184 | const auto& sampler = primProc.textureSampler(i); |
| 185 | auto texture = static_cast<GrVkTexture*>(primProcTextures[i]->peekTexture()); |
| 186 | samplerBindings[currTextureBinding++] = {sampler.samplerState(), texture}; |
| 187 | } |
| 188 | |
| 189 | GrFragmentProcessor::Iter iter(pipeline); |
| 190 | GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt); |
| 191 | const GrFragmentProcessor* fp = iter.next(); |
| 192 | GrGLSLFragmentProcessor* glslFP = glslIter.next(); |
| 193 | while (fp && glslFP) { |
| 194 | for (int i = 0; i < fp->numTextureSamplers(); ++i) { |
| 195 | const auto& sampler = fp->textureSampler(i); |
| 196 | samplerBindings[currTextureBinding++] = |
| 197 | {sampler.samplerState(), static_cast<GrVkTexture*>(sampler.peekTexture())}; |
| 198 | } |
| 199 | fp = iter.next(); |
| 200 | glslFP = glslIter.next(); |
| 201 | } |
| 202 | SkASSERT(!fp && !glslFP); |
| 203 | |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 204 | if (GrTextureProxy* dstTextureProxy = pipeline.dstTextureProxy()) { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 205 | samplerBindings[currTextureBinding++] = { |
| 206 | GrSamplerState::ClampNearest(), |
| 207 | static_cast<GrVkTexture*>(dstTextureProxy->peekTexture())}; |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 208 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 209 | |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 210 | // Get new descriptor set |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 211 | SkASSERT(fNumSamplers == currTextureBinding); |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 212 | if (fNumSamplers) { |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 213 | if (fSamplerDescriptorSet) { |
| 214 | fSamplerDescriptorSet->recycle(gpu); |
| 215 | } |
| 216 | fSamplerDescriptorSet = gpu->resourceProvider().getSamplerDescriptorSet(fSamplerDSHandle); |
| 217 | int samplerDSIdx = GrVkUniformHandler::kSamplerDescSet; |
| 218 | fDescriptorSets[samplerDSIdx] = fSamplerDescriptorSet->descriptorSet(); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 219 | for (int i = 0; i < fNumSamplers; ++i) { |
| 220 | const GrSamplerState& state = samplerBindings[i].fState; |
| 221 | GrVkTexture* texture = samplerBindings[i].fTexture; |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 222 | |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 223 | const GrVkImageView* textureView = texture->textureView(); |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 224 | const GrVkSampler* sampler = nullptr; |
| 225 | if (fImmutableSamplers[i]) { |
| 226 | sampler = fImmutableSamplers[i]; |
| 227 | } else { |
| 228 | sampler = gpu->resourceProvider().findOrCreateCompatibleSampler( |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 229 | state, texture->ycbcrConversionInfo()); |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 230 | } |
| 231 | SkASSERT(sampler); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 232 | |
| 233 | VkDescriptorImageInfo imageInfo; |
| 234 | memset(&imageInfo, 0, sizeof(VkDescriptorImageInfo)); |
| 235 | imageInfo.sampler = sampler->sampler(); |
| 236 | imageInfo.imageView = textureView->imageView(); |
| 237 | imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 238 | |
| 239 | VkWriteDescriptorSet writeInfo; |
| 240 | memset(&writeInfo, 0, sizeof(VkWriteDescriptorSet)); |
| 241 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 242 | writeInfo.pNext = nullptr; |
| 243 | writeInfo.dstSet = fDescriptorSets[GrVkUniformHandler::kSamplerDescSet]; |
| 244 | writeInfo.dstBinding = i; |
| 245 | writeInfo.dstArrayElement = 0; |
| 246 | writeInfo.descriptorCount = 1; |
| 247 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 248 | writeInfo.pImageInfo = &imageInfo; |
| 249 | writeInfo.pBufferInfo = nullptr; |
| 250 | writeInfo.pTexelBufferView = nullptr; |
| 251 | |
| 252 | GR_VK_CALL(gpu->vkInterface(), |
| 253 | UpdateDescriptorSets(gpu->device(), 1, &writeInfo, 0, nullptr)); |
| 254 | commandBuffer->addResource(sampler); |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 255 | if (!fImmutableSamplers[i]) { |
| 256 | sampler->unref(gpu); |
| 257 | } |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 258 | commandBuffer->addResource(samplerBindings[i].fTexture->textureView()); |
| 259 | commandBuffer->addResource(samplerBindings[i].fTexture->resource()); |
egdaniel | 7cbffda | 2016-04-08 13:27:53 -0700 | [diff] [blame] | 260 | } |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 261 | |
Greg Daniel | eecc687 | 2019-07-29 13:21:37 -0400 | [diff] [blame] | 262 | commandBuffer->bindDescriptorSets(gpu, this, fPipeline->layout(), samplerDSIdx, 1, |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 263 | &fDescriptorSets[samplerDSIdx], 0, nullptr); |
| 264 | commandBuffer->addRecycledResource(fSamplerDescriptorSet); |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 265 | } |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 268 | void set_uniform_descriptor_writes(VkWriteDescriptorSet* descriptorWrite, |
| 269 | VkDescriptorBufferInfo* bufferInfo, |
| 270 | const GrVkUniformBuffer* buffer, |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 271 | VkDescriptorSet descriptorSet) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 272 | |
| 273 | memset(bufferInfo, 0, sizeof(VkDescriptorBufferInfo)); |
| 274 | bufferInfo->buffer = buffer->buffer(); |
| 275 | bufferInfo->offset = buffer->offset(); |
| 276 | bufferInfo->range = buffer->size(); |
| 277 | |
| 278 | memset(descriptorWrite, 0, sizeof(VkWriteDescriptorSet)); |
| 279 | descriptorWrite->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 280 | descriptorWrite->pNext = nullptr; |
Greg Daniel | 783c436 | 2017-05-04 17:29:09 -0400 | [diff] [blame] | 281 | descriptorWrite->dstSet = descriptorSet; |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 282 | descriptorWrite->dstBinding = GrVkUniformHandler::kUniformBinding; |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 283 | descriptorWrite->dstArrayElement = 0; |
| 284 | descriptorWrite->descriptorCount = 1; |
| 285 | descriptorWrite->descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 286 | descriptorWrite->pImageInfo = nullptr; |
| 287 | descriptorWrite->pBufferInfo = bufferInfo; |
| 288 | descriptorWrite->pTexelBufferView = nullptr; |
| 289 | } |
| 290 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 291 | void GrVkPipelineState::writeUniformBuffers(const GrVkGpu* gpu) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 292 | VkWriteDescriptorSet descriptorWrites[3]; |
| 293 | VkDescriptorBufferInfo bufferInfos[3]; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 294 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 295 | uint32_t writeCount = 0; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 296 | |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 297 | if (fUniformBuffer.get()) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 298 | set_uniform_descriptor_writes(&descriptorWrites[writeCount], |
| 299 | &bufferInfos[writeCount], |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 300 | fUniformBuffer.get(), |
| 301 | fDescriptorSets[GrVkUniformHandler::kUniformBufferDescSet]); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 302 | ++writeCount; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 305 | if (writeCount) { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 306 | GR_VK_CALL(gpu->vkInterface(), UpdateDescriptorSets(gpu->device(), |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 307 | writeCount, |
| 308 | descriptorWrites, |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 309 | 0, nullptr)); |
| 310 | } |
| 311 | } |
| 312 | |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 313 | void GrVkPipelineState::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 314 | |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 315 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 316 | if (fBuiltinUniformHandles.fRTHeightUni.isValid() && |
| 317 | fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) { |
| 318 | fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height())); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // set RT adjustment |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 322 | SkISize size; |
| 323 | size.set(rt->width(), rt->height()); |
| 324 | SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid()); |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 325 | if (fRenderTargetState.fRenderTargetOrigin != origin || |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 326 | fRenderTargetState.fRenderTargetSize != size) { |
| 327 | fRenderTargetState.fRenderTargetSize = size; |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 328 | fRenderTargetState.fRenderTargetOrigin = origin; |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 329 | |
| 330 | float rtAdjustmentVec[4]; |
| 331 | fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 332 | fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec); |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 336 | void GrVkPipelineState::bindPipeline(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer) { |
jvanverth | 992ad36 | 2016-02-26 09:21:02 -0800 | [diff] [blame] | 337 | commandBuffer->bindPipeline(gpu, fPipeline); |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 338 | } |