blob: 590eff641aa1c9cf02a31b8b26b168a9b20dfd2d [file] [log] [blame]
Timothy Liang057c3902018-08-08 10:48:45 -04001/*
2 * Copyright 2018 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
8#include "GrMtlPipelineState.h"
9
Timothy Liang6ed63962018-08-10 09:49:44 -040010#include "GrContext.h"
11#include "GrContextPriv.h"
12#include "GrPipeline.h"
13#include "GrRenderTarget.h"
Ethan Nicholas01063512018-10-08 16:58:25 -040014#include "GrRenderTargetPriv.h"
Timothy Liang6ed63962018-08-10 09:49:44 -040015#include "GrTexturePriv.h"
Timothy Liang057c3902018-08-08 10:48:45 -040016#include "GrMtlBuffer.h"
17#include "GrMtlGpu.h"
Timothy Liang6ed63962018-08-10 09:49:44 -040018#include "GrMtlSampler.h"
19#include "GrMtlTexture.h"
20#include "glsl/GrGLSLFragmentProcessor.h"
21#include "glsl/GrGLSLGeometryProcessor.h"
22#include "glsl/GrGLSLXferProcessor.h"
Timothy Liang057c3902018-08-08 10:48:45 -040023
Timothy Liang6ed63962018-08-10 09:49:44 -040024GrMtlPipelineState::SamplerBindings::SamplerBindings(const GrSamplerState& state,
25 GrTexture* texture,
Timothy Liang6ed63962018-08-10 09:49:44 -040026 GrMtlGpu* gpu)
Greg Daniel0f70be82018-10-08 17:35:08 +000027 : fTexture(static_cast<GrMtlTexture*>(texture)->mtlTexture()) {
Timothy Liang6ed63962018-08-10 09:49:44 -040028 // TODO: use resource provider to get sampler.
29 std::unique_ptr<GrMtlSampler> sampler(
30 GrMtlSampler::Create(gpu, state, texture->texturePriv().maxMipMapLevel()));
31 fSampler = sampler->mtlSamplerState();
32}
33
34GrMtlPipelineState::GrMtlPipelineState(
35 GrMtlGpu* gpu,
36 id<MTLRenderPipelineState> pipelineState,
37 MTLPixelFormat pixelFormat,
38 const GrGLSLBuiltinUniformHandles& builtinUniformHandles,
39 const UniformInfoArray& uniforms,
40 GrMtlBuffer* geometryUniformBuffer,
41 GrMtlBuffer* fragmentUniformBuffer,
42 uint32_t numSamplers,
43 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
44 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
45 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
46 int fragmentProcessorCnt)
Timothy Liang057c3902018-08-08 10:48:45 -040047 : fGpu(gpu)
48 , fPipelineState(pipelineState)
49 , fPixelFormat(pixelFormat)
Timothy Liang6ed63962018-08-10 09:49:44 -040050 , fBuiltinUniformHandles(builtinUniformHandles)
Timothy Liang057c3902018-08-08 10:48:45 -040051 , fGeometryUniformBuffer(geometryUniformBuffer)
Timothy Liang6ed63962018-08-10 09:49:44 -040052 , fFragmentUniformBuffer(fragmentUniformBuffer)
53 , fNumSamplers(numSamplers)
54 , fGeometryProcessor(std::move(geometryProcessor))
55 , fXferProcessor(std::move(xferProcessor))
56 , fFragmentProcessors(std::move(fragmentProcessors))
57 , fFragmentProcessorCnt(fragmentProcessorCnt)
58 , fDataManager(uniforms, geometryUniformBuffer->sizeInBytes(),
59 fragmentUniformBuffer->sizeInBytes()) {
Timothy Liang057c3902018-08-08 10:48:45 -040060 (void) fPixelFormat; // Suppress unused-var warning.
61}
Timothy Liang6ed63962018-08-10 09:49:44 -040062
63void GrMtlPipelineState::setData(const GrPrimitiveProcessor& primProc,
64 const GrPipeline& pipeline,
65 const GrTextureProxy* const primProcTextures[]) {
66 SkASSERT(primProcTextures || !primProc.numTextureSamplers());
67
68 this->setRenderTargetState(pipeline.proxy());
69 fGeometryProcessor->setData(fDataManager, primProc,
70 GrFragmentProcessor::CoordTransformIter(pipeline));
71 fSamplerBindings.reset();
72 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
73 const auto& sampler = primProc.textureSampler(i);
74 auto texture = static_cast<GrMtlTexture*>(primProcTextures[i]->peekTexture());
Greg Daniel0f70be82018-10-08 17:35:08 +000075 fSamplerBindings.emplace_back(sampler.samplerState(), texture, fGpu);
Timothy Liang6ed63962018-08-10 09:49:44 -040076 }
77
78 GrFragmentProcessor::Iter iter(pipeline);
79 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
80 const GrFragmentProcessor* fp = iter.next();
81 GrGLSLFragmentProcessor* glslFP = glslIter.next();
82 while (fp && glslFP) {
Michael Ludwigd3a357d2018-09-27 17:31:08 -040083 glslFP->setData(fDataManager, *fp);
Timothy Liang6ed63962018-08-10 09:49:44 -040084 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
85 const auto& sampler = fp->textureSampler(i);
Greg Daniel0f70be82018-10-08 17:35:08 +000086 fSamplerBindings.emplace_back(sampler.samplerState(), sampler.peekTexture(), fGpu);
Timothy Liang6ed63962018-08-10 09:49:44 -040087 }
88 fp = iter.next();
89 glslFP = glslIter.next();
90 }
91 SkASSERT(!fp && !glslFP);
92
93 {
94 SkIPoint offset;
95 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
96
97 fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset);
98 }
99
100 if (GrTextureProxy* dstTextureProxy = pipeline.dstTextureProxy()) {
101 fSamplerBindings.emplace_back(GrSamplerState::ClampNearest(),
102 dstTextureProxy->peekTexture(),
Timothy Liang6ed63962018-08-10 09:49:44 -0400103 fGpu);
104 }
105
106 SkASSERT(fNumSamplers == fSamplerBindings.count());
107 if (fGeometryUniformBuffer || fFragmentUniformBuffer) {
108 fDataManager.uploadUniformBuffers(fGpu, fGeometryUniformBuffer.get(),
109 fFragmentUniformBuffer.get());
110 }
Ethan Nicholas01063512018-10-08 16:58:25 -0400111
112 if (pipeline.isStencilEnabled()) {
113 GrRenderTarget* rt = pipeline.renderTarget();
114 SkASSERT(rt->renderTargetPriv().getStencilAttachment());
115 fStencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(),
116 rt->renderTargetPriv().numStencilBits());
117 }
Timothy Liang6ed63962018-08-10 09:49:44 -0400118}
119
120void GrMtlPipelineState::bind(id<MTLRenderCommandEncoder> renderCmdEncoder) {
121 if (fGeometryUniformBuffer) {
122 [renderCmdEncoder setVertexBuffer: fGeometryUniformBuffer->mtlBuffer()
123 offset: 0
124 atIndex: GrMtlUniformHandler::kGeometryBinding];
125 }
126 if (fFragmentUniformBuffer) {
127 [renderCmdEncoder setFragmentBuffer: fFragmentUniformBuffer->mtlBuffer()
128 offset: 0
129 atIndex: GrMtlUniformHandler::kFragBinding];
130 }
131 SkASSERT(fNumSamplers == fSamplerBindings.count());
132 for (int index = 0; index < fNumSamplers; ++index) {
Greg Daniel0f70be82018-10-08 17:35:08 +0000133 [renderCmdEncoder setFragmentTexture: fSamplerBindings[index].fTexture
134 atIndex: index];
135 [renderCmdEncoder setFragmentSamplerState: fSamplerBindings[index].fSampler
136 atIndex: index];
Timothy Liang6ed63962018-08-10 09:49:44 -0400137 }
138}
139
140void GrMtlPipelineState::setRenderTargetState(const GrRenderTargetProxy* proxy) {
141 GrRenderTarget* rt = proxy->peekRenderTarget();
142
143 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
Greg Daniele6ab9982018-08-22 13:56:32 +0000144 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
145 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
146 fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
Timothy Liang6ed63962018-08-10 09:49:44 -0400147 }
148
149 // set RT adjustment
150 SkISize size;
151 size.set(rt->width(), rt->height());
152 SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
153 if (fRenderTargetState.fRenderTargetOrigin != proxy->origin() ||
154 fRenderTargetState.fRenderTargetSize != size) {
155 fRenderTargetState.fRenderTargetSize = size;
156 fRenderTargetState.fRenderTargetOrigin = proxy->origin();
157
158 float rtAdjustmentVec[4];
159 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
160 fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
161 }
162}
Timothy Liangde0be802018-08-10 13:48:08 -0400163
164static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
165 switch (coeff) {
166 case kConstC_GrBlendCoeff:
167 case kIConstC_GrBlendCoeff:
168 case kConstA_GrBlendCoeff:
169 case kIConstA_GrBlendCoeff:
170 return true;
171 default:
172 return false;
173 }
174}
175
176void GrMtlPipelineState::setBlendConstants(id<MTLRenderCommandEncoder> renderCmdEncoder,
177 GrPixelConfig config,
178 const GrXferProcessor& xferProcessor) {
179 if (!renderCmdEncoder) {
180 return;
181 }
182
183 GrXferProcessor::BlendInfo blendInfo;
184 xferProcessor.getBlendInfo(&blendInfo);
185 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
186 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
187 if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
188 float floatColors[4];
189 // Swizzle the blend to match what the shader will output.
190 const GrSwizzle& swizzle = fGpu->caps()->shaderCaps()->configOutputSwizzle(config);
191 GrColor blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
192 GrColorToRGBAFloat(blendConst, floatColors);
193
194 [renderCmdEncoder setBlendColorRed: floatColors[0]
195 green: floatColors[1]
196 blue: floatColors[2]
197 alpha: floatColors[3]];
198 }
199}
Ethan Nicholas01063512018-10-08 16:58:25 -0400200
201MTLStencilOperation skia_stencil_op_to_mtl(GrStencilOp op) {
202 switch (op) {
203 case GrStencilOp::kKeep:
204 return MTLStencilOperationKeep;
205 case GrStencilOp::kZero:
206 return MTLStencilOperationZero;
207 case GrStencilOp::kReplace:
208 return MTLStencilOperationReplace;
209 case GrStencilOp::kInvert:
210 return MTLStencilOperationInvert;
211 case GrStencilOp::kIncWrap:
212 return MTLStencilOperationIncrementWrap;
213 case GrStencilOp::kDecWrap:
214 return MTLStencilOperationDecrementWrap;
215 case GrStencilOp::kIncClamp:
216 return MTLStencilOperationIncrementClamp;
217 case GrStencilOp::kDecClamp:
218 return MTLStencilOperationDecrementClamp;
219 }
220}
221
222MTLStencilDescriptor* skia_stencil_to_mtl(GrStencilSettings::Face face) {
223 MTLStencilDescriptor* result = [[MTLStencilDescriptor alloc] init];
224 switch (face.fTest) {
225 case GrStencilTest::kAlways:
226 result.stencilCompareFunction = MTLCompareFunctionAlways;
227 break;
228 case GrStencilTest::kNever:
229 result.stencilCompareFunction = MTLCompareFunctionNever;
230 break;
231 case GrStencilTest::kGreater:
232 result.stencilCompareFunction = MTLCompareFunctionGreater;
233 break;
234 case GrStencilTest::kGEqual:
235 result.stencilCompareFunction = MTLCompareFunctionGreaterEqual;
236 break;
237 case GrStencilTest::kLess:
238 result.stencilCompareFunction = MTLCompareFunctionLess;
239 break;
240 case GrStencilTest::kLEqual:
241 result.stencilCompareFunction = MTLCompareFunctionLessEqual;
242 break;
243 case GrStencilTest::kEqual:
244 result.stencilCompareFunction = MTLCompareFunctionEqual;
245 break;
246 case GrStencilTest::kNotEqual:
247 result.stencilCompareFunction = MTLCompareFunctionNotEqual;
248 break;
249 }
250 result.readMask = face.fTestMask;
251 result.writeMask = face.fWriteMask;
252 result.depthStencilPassOperation = skia_stencil_op_to_mtl(face.fPassOp);
253 result.stencilFailureOperation = skia_stencil_op_to_mtl(face.fFailOp);
254 return result;
255}
256
257void GrMtlPipelineState::setDepthStencilState(id<MTLRenderCommandEncoder> renderCmdEncoder) {
258 if (fStencil.isDisabled()) {
259 MTLDepthStencilDescriptor* desc = [[MTLDepthStencilDescriptor alloc] init];
260 id<MTLDepthStencilState> state = [fGpu->device() newDepthStencilStateWithDescriptor:desc];
261 [renderCmdEncoder setDepthStencilState:state];
262 }
263 else {
264 MTLDepthStencilDescriptor* desc = [[MTLDepthStencilDescriptor alloc] init];
265 desc.frontFaceStencil = skia_stencil_to_mtl(fStencil.front());
266 if (fStencil.isTwoSided()) {
267 desc.backFaceStencil = skia_stencil_to_mtl(fStencil.back());
268 [renderCmdEncoder setStencilFrontReferenceValue:fStencil.front().fRef
269 backReferenceValue:fStencil.back().fRef];
270 }
271 else {
272 desc.backFaceStencil = desc.frontFaceStencil;
273 [renderCmdEncoder setStencilReferenceValue:fStencil.front().fRef];
274 }
275 id<MTLDepthStencilState> state = [fGpu->device() newDepthStencilStateWithDescriptor:desc];
276 [renderCmdEncoder setDepthStencilState:state];
277 }
278}