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