joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 8 | #include "GrGLProgramBuilder.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 9 | |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 10 | #include "GrAutoLocaleSetter.h" |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 11 | #include "GrContext.h" |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 13 | #include "GrCoordTransform.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 14 | #include "GrGLProgramBuilder.h" |
egdaniel | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 15 | #include "GrProgramDesc.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 16 | #include "GrShaderCaps.h" |
bsalomon | 7f9b2e4 | 2016-01-12 13:29:26 -0800 | [diff] [blame] | 17 | #include "GrSwizzle.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 18 | #include "SkAutoMalloc.h" |
Derek Sollenberger | 488f0d6 | 2017-03-03 15:48:33 -0500 | [diff] [blame] | 19 | #include "SkATrace.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 20 | #include "SkTraceEvent.h" |
joshualitt | e7afc2d | 2015-09-11 10:44:13 -0700 | [diff] [blame] | 21 | #include "gl/GrGLGpu.h" |
joshualitt | e7afc2d | 2015-09-11 10:44:13 -0700 | [diff] [blame] | 22 | #include "gl/GrGLProgram.h" |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 23 | #include "gl/builders/GrGLShaderStringBuilder.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 24 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 25 | #include "glsl/GrGLSLGeometryProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 26 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 27 | #include "glsl/GrGLSLXferProcessor.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 28 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 29 | #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X) |
| 30 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(this->gpu()->glInterface(), R, X) |
| 31 | |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 32 | GrGLProgram* GrGLProgramBuilder::CreateProgram(GrRenderTarget* renderTarget, GrSurfaceOrigin origin, |
| 33 | const GrPrimitiveProcessor& primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 34 | const GrTextureProxy* const primProcProxies[], |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 35 | const GrPipeline& pipeline, |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 36 | GrProgramDesc* desc, |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 37 | GrGLGpu* gpu) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 38 | SkASSERT(!pipeline.isBad()); |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 39 | |
Derek Sollenberger | 488f0d6 | 2017-03-03 15:48:33 -0500 | [diff] [blame] | 40 | ATRACE_ANDROID_FRAMEWORK("Shader Compile"); |
bsalomon | 3318ee7 | 2015-03-16 11:56:29 -0700 | [diff] [blame] | 41 | GrAutoLocaleSetter als("C"); |
| 42 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 43 | // create a builder. This will be handed off to effects so they can use it to add |
| 44 | // uniforms, varyings, textures, etc |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 45 | GrGLProgramBuilder builder(gpu, renderTarget, origin, |
| 46 | pipeline, primProc, primProcProxies, desc); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 47 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 48 | auto persistentCache = gpu->getContext()->priv().getPersistentCache(); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 49 | if (persistentCache) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 50 | sk_sp<SkData> key = SkData::MakeWithoutCopy(desc->asKey(), desc->keyLength()); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 51 | builder.fCached = persistentCache->load(*key); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 52 | // the eventual end goal is to completely skip emitAndInstallProcs on a cache hit, but it's |
| 53 | // doing necessary setup in addition to generating the SkSL code. Currently we are only able |
| 54 | // to skip the SkSL->GLSL step on a cache hit. |
| 55 | } |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 56 | if (!builder.emitAndInstallProcs()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 57 | return nullptr; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 58 | } |
egdaniel | c1e7101 | 2016-01-20 07:53:51 -0800 | [diff] [blame] | 59 | return builder.finalize(); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 60 | } |
| 61 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 62 | ///////////////////////////////////////////////////////////////////////////// |
| 63 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 64 | GrGLProgramBuilder::GrGLProgramBuilder(GrGLGpu* gpu, |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 65 | GrRenderTarget* renderTarget, |
| 66 | GrSurfaceOrigin origin, |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 67 | const GrPipeline& pipeline, |
| 68 | const GrPrimitiveProcessor& primProc, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 69 | const GrTextureProxy* const primProcProxies[], |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 70 | GrProgramDesc* desc) |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 71 | : INHERITED(renderTarget, origin, primProc, primProcProxies, pipeline, desc) |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 72 | , fGpu(gpu) |
| 73 | , fVaryingHandler(this) |
| 74 | , fUniformHandler(this) |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 75 | , fVertexAttributeCnt(0) |
| 76 | , fInstanceAttributeCnt(0) |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 77 | , fVertexStride(0) |
| 78 | , fInstanceStride(0) {} |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 79 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 80 | const GrCaps* GrGLProgramBuilder::caps() const { |
| 81 | return fGpu->caps(); |
| 82 | } |
| 83 | |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 84 | bool GrGLProgramBuilder::compileAndAttachShaders(const char* glsl, |
| 85 | int length, |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 86 | GrGLuint programId, |
| 87 | GrGLenum type, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 88 | SkTDArray<GrGLuint>* shaderIds, |
| 89 | const SkSL::Program::Settings& settings, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 90 | const SkSL::Program::Inputs& inputs) { |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 91 | GrGLGpu* gpu = this->gpu(); |
| 92 | GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(), |
| 93 | programId, |
| 94 | type, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 95 | glsl, |
| 96 | length, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 97 | gpu->stats(), |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 98 | settings); |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 99 | if (!shaderId) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | *shaderIds->append() = shaderId; |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 104 | if (inputs.fFlipY) { |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 105 | GrProgramDesc* d = this->desc(); |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 106 | d->setSurfaceOriginKey(GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(this->origin())); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 107 | } |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 112 | void GrGLProgramBuilder::computeCountsAndStrides(GrGLuint programID, |
| 113 | const GrPrimitiveProcessor& primProc, |
| 114 | bool bindAttribLocations) { |
| 115 | fVertexAttributeCnt = primProc.numVertexAttributes(); |
| 116 | fInstanceAttributeCnt = primProc.numInstanceAttributes(); |
| 117 | fAttributes.reset( |
| 118 | new GrGLProgram::Attribute[fVertexAttributeCnt + fInstanceAttributeCnt]); |
| 119 | auto addAttr = [&](int i, const auto& a, size_t* stride) { |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 120 | fAttributes[i].fCPUType = a.cpuType(); |
| 121 | fAttributes[i].fGPUType = a.gpuType(); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 122 | fAttributes[i].fOffset = *stride; |
| 123 | *stride += a.sizeAlign4(); |
| 124 | fAttributes[i].fLocation = i; |
| 125 | if (bindAttribLocations) { |
| 126 | GL_CALL(BindAttribLocation(programID, i, a.name())); |
| 127 | } |
| 128 | }; |
| 129 | fVertexStride = 0; |
| 130 | int i = 0; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 131 | for (const auto& attr : primProc.vertexAttributes()) { |
| 132 | addAttr(i++, attr, &fVertexStride); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 133 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 134 | SkASSERT(fVertexStride == primProc.vertexStride()); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 135 | fInstanceStride = 0; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 136 | for (const auto& attr : primProc.instanceAttributes()) { |
| 137 | addAttr(i++, attr, &fInstanceStride); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 138 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 139 | SkASSERT(fInstanceStride == primProc.instanceStride()); |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 142 | void GrGLProgramBuilder::addInputVars(const SkSL::Program::Inputs& inputs) { |
| 143 | if (inputs.fRTWidth) { |
| 144 | this->addRTWidthUniform(SKSL_RTWIDTH_NAME); |
| 145 | } |
| 146 | if (inputs.fRTHeight) { |
| 147 | this->addRTHeightUniform(SKSL_RTHEIGHT_NAME); |
| 148 | } |
| 149 | } |
| 150 | |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 151 | void GrGLProgramBuilder::storeShaderInCache(const SkSL::Program::Inputs& inputs, GrGLuint programID, |
| 152 | const SkSL::String& glsl) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 153 | if (!this->gpu()->getContext()->priv().getPersistentCache()) { |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | sk_sp<SkData> key = SkData::MakeWithoutCopy(desc()->asKey(), desc()->keyLength()); |
| 157 | if (fGpu->glCaps().programBinarySupport()) { |
| 158 | // binary cache |
| 159 | GrGLsizei length = 0; |
| 160 | GL_CALL(GetProgramiv(programID, GL_PROGRAM_BINARY_LENGTH, &length)); |
| 161 | if (length > 0) { |
| 162 | GrGLenum binaryFormat; |
| 163 | std::unique_ptr<char[]> binary(new char[length]); |
| 164 | GL_CALL(GetProgramBinary(programID, length, &length, &binaryFormat, binary.get())); |
| 165 | size_t dataLength = sizeof(inputs) + sizeof(binaryFormat) + length; |
| 166 | std::unique_ptr<uint8_t[]> data(new uint8_t[dataLength]); |
| 167 | size_t offset = 0; |
| 168 | memcpy(data.get() + offset, &inputs, sizeof(inputs)); |
| 169 | offset += sizeof(inputs); |
| 170 | memcpy(data.get() + offset, &binaryFormat, sizeof(binaryFormat)); |
| 171 | offset += sizeof(binaryFormat); |
| 172 | memcpy(data.get() + offset, binary.get(), length); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 173 | this->gpu()->getContext()->priv().getPersistentCache()->store( |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 174 | *key, *SkData::MakeWithoutCopy(data.get(), dataLength)); |
| 175 | } |
| 176 | } else { |
| 177 | // source cache |
| 178 | size_t dataLength = sizeof(inputs) + glsl.length(); |
| 179 | std::unique_ptr<uint8_t[]> data(new uint8_t[dataLength]); |
| 180 | size_t offset = 0; |
| 181 | memcpy(data.get() + offset, &inputs, sizeof(inputs)); |
| 182 | offset += sizeof(inputs); |
| 183 | memcpy(data.get() + offset, glsl.data(), glsl.length()); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 184 | this->gpu()->getContext()->priv().getPersistentCache()->store( |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 185 | *key, *SkData::MakeWithoutCopy(data.get(), dataLength)); |
| 186 | } |
| 187 | } |
| 188 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 189 | GrGLProgram* GrGLProgramBuilder::finalize() { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 190 | TRACE_EVENT0("skia", TRACE_FUNC); |
Ryan Macnak | 38a10ad | 2017-07-10 10:36:34 -0700 | [diff] [blame] | 191 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 192 | // verify we can get a program id |
| 193 | GrGLuint programID; |
| 194 | GL_CALL_RET(programID, CreateProgram()); |
| 195 | if (0 == programID) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 196 | return nullptr; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Ethan Nicholas | 06d55fb | 2017-11-08 09:48:50 -0500 | [diff] [blame] | 199 | if (this->gpu()->glCaps().programBinarySupport() && |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 200 | this->gpu()->getContext()->priv().getPersistentCache()) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 201 | GL_CALL(ProgramParameteri(programID, GR_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GR_GL_TRUE)); |
| 202 | } |
| 203 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 204 | this->finalizeShaders(); |
| 205 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 206 | // compile shaders and bind attributes / uniforms |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 207 | const GrPrimitiveProcessor& primProc = this->primitiveProcessor(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 208 | SkSL::Program::Settings settings; |
| 209 | settings.fCaps = this->gpu()->glCaps().shaderCaps(); |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 210 | settings.fFlipY = this->origin() != kTopLeft_GrSurfaceOrigin; |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 211 | settings.fSharpenTextures = |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 212 | this->gpu()->getContext()->priv().options().fSharpenMipmappedTextures; |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 213 | settings.fFragColorIsInOut = this->fragColorIsInOut(); |
| 214 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 215 | SkSL::Program::Inputs inputs; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 216 | SkTDArray<GrGLuint> shadersToDelete; |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 217 | // Calling GetProgramiv is expensive in Chromium. Assume success in release builds. |
| 218 | bool checkLinked = kChromium_GrGLDriver != fGpu->ctxInfo().driver(); |
| 219 | #ifdef SK_DEBUG |
| 220 | checkLinked = true; |
| 221 | #endif |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 222 | bool cached = fCached.get() != nullptr; |
| 223 | SkSL::String glsl; |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 224 | if (cached) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 225 | const uint8_t* bytes = fCached->bytes(); |
| 226 | size_t offset = 0; |
| 227 | memcpy(&inputs, bytes + offset, sizeof(inputs)); |
| 228 | offset += sizeof(inputs); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 229 | if (fGpu->glCaps().programBinarySupport()) { |
| 230 | // binary cache hit, just hand the binary to GL |
| 231 | int binaryFormat; |
| 232 | memcpy(&binaryFormat, bytes + offset, sizeof(binaryFormat)); |
| 233 | offset += sizeof(binaryFormat); |
| 234 | GrGLClearErr(this->gpu()->glInterface()); |
| 235 | GR_GL_CALL_NOERRCHECK(this->gpu()->glInterface(), |
| 236 | ProgramBinary(programID, binaryFormat, (void*) (bytes + offset), |
| 237 | fCached->size() - offset)); |
| 238 | if (GR_GL_GET_ERROR(this->gpu()->glInterface()) == GR_GL_NO_ERROR) { |
| 239 | if (checkLinked) { |
| 240 | cached = this->checkLinkStatus(programID); |
| 241 | } |
| 242 | if (cached) { |
| 243 | this->addInputVars(inputs); |
| 244 | this->computeCountsAndStrides(programID, primProc, false); |
| 245 | } |
| 246 | } else { |
| 247 | cached = false; |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 248 | } |
Ethan Nicholas | 98ad5b7 | 2018-03-13 09:53:02 -0400 | [diff] [blame] | 249 | } else { |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 250 | // source cache hit, we don't need to compile the SkSL->GLSL |
| 251 | glsl = SkSL::String(((const char*) bytes) + offset, fCached->size() - offset); |
Ethan Nicholas | 98ad5b7 | 2018-03-13 09:53:02 -0400 | [diff] [blame] | 252 | } |
| 253 | } |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 254 | if (!cached || !fGpu->glCaps().programBinarySupport()) { |
| 255 | // either a cache miss, or we can't store binaries in the cache |
| 256 | if (glsl == "" || true) { |
| 257 | // don't have cached GLSL, need to compile SkSL->GLSL |
| 258 | if (fFS.fForceHighPrecision) { |
| 259 | settings.fForceHighPrecision = true; |
| 260 | } |
| 261 | std::unique_ptr<SkSL::Program> fs = GrSkSLtoGLSL(gpu()->glContext(), |
| 262 | GR_GL_FRAGMENT_SHADER, |
| 263 | fFS.fCompilerStrings.begin(), |
| 264 | fFS.fCompilerStringLengths.begin(), |
| 265 | fFS.fCompilerStrings.count(), |
| 266 | settings, |
| 267 | &glsl); |
| 268 | if (!fs) { |
| 269 | this->cleanupProgram(programID, shadersToDelete); |
| 270 | return nullptr; |
| 271 | } |
| 272 | inputs = fs->fInputs; |
| 273 | } else { |
| 274 | // we've pulled GLSL and inputs from the cache, but still need to do some setup |
| 275 | this->addInputVars(inputs); |
| 276 | this->computeCountsAndStrides(programID, primProc, false); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 277 | } |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 278 | this->addInputVars(inputs); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 279 | if (!this->compileAndAttachShaders(glsl.c_str(), glsl.size(), programID, |
| 280 | GR_GL_FRAGMENT_SHADER, &shadersToDelete, settings, |
| 281 | inputs)) { |
| 282 | this->cleanupProgram(programID, shadersToDelete); |
| 283 | return nullptr; |
| 284 | } |
| 285 | |
| 286 | std::unique_ptr<SkSL::Program> vs = GrSkSLtoGLSL(gpu()->glContext(), |
| 287 | GR_GL_VERTEX_SHADER, |
| 288 | fVS.fCompilerStrings.begin(), |
| 289 | fVS.fCompilerStringLengths.begin(), |
| 290 | fVS.fCompilerStrings.count(), |
| 291 | settings, |
| 292 | &glsl); |
Brian Salomon | 7c93472 | 2018-06-01 14:42:53 -0400 | [diff] [blame] | 293 | if (!vs || !this->compileAndAttachShaders(glsl.c_str(), glsl.size(), programID, |
| 294 | GR_GL_VERTEX_SHADER, &shadersToDelete, settings, |
| 295 | inputs)) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 296 | this->cleanupProgram(programID, shadersToDelete); |
| 297 | return nullptr; |
| 298 | } |
| 299 | |
| 300 | // NVPR actually requires a vertex shader to compile |
| 301 | bool useNvpr = primProc.isPathRendering(); |
| 302 | if (!useNvpr) { |
Ethan Nicholas | ad77dce | 2018-07-11 13:59:03 -0400 | [diff] [blame] | 303 | this->computeCountsAndStrides(programID, primProc, true); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if (primProc.willUseGeoShader()) { |
| 307 | std::unique_ptr<SkSL::Program> gs; |
| 308 | gs = GrSkSLtoGLSL(gpu()->glContext(), |
| 309 | GR_GL_GEOMETRY_SHADER, |
| 310 | fGS.fCompilerStrings.begin(), |
| 311 | fGS.fCompilerStringLengths.begin(), |
| 312 | fGS.fCompilerStrings.count(), |
| 313 | settings, |
| 314 | &glsl); |
Brian Salomon | 7c93472 | 2018-06-01 14:42:53 -0400 | [diff] [blame] | 315 | if (!gs || !this->compileAndAttachShaders(glsl.c_str(), glsl.size(), programID, |
| 316 | GR_GL_GEOMETRY_SHADER, &shadersToDelete, |
| 317 | settings, inputs)) { |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 318 | this->cleanupProgram(programID, shadersToDelete); |
| 319 | return nullptr; |
| 320 | } |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 321 | } |
| 322 | this->bindProgramResourceLocations(programID); |
| 323 | |
| 324 | GL_CALL(LinkProgram(programID)); |
Ethan Nicholas | 5a0338c | 2019-01-02 11:48:56 -0500 | [diff] [blame] | 325 | if (checkLinked) { |
| 326 | if (!this->checkLinkStatus(programID)) { |
| 327 | GL_CALL(DeleteProgram(programID)); |
| 328 | SkDebugf("VS:\n"); |
| 329 | GrGLPrintShader(fGpu->glContext(), |
| 330 | GR_GL_VERTEX_SHADER, |
| 331 | fVS.fCompilerStrings.begin(), |
| 332 | fVS.fCompilerStringLengths.begin(), |
| 333 | fVS.fCompilerStrings.count(), |
| 334 | settings); |
| 335 | if (primProc.willUseGeoShader()) { |
| 336 | SkDebugf("\nGS:\n"); |
| 337 | GrGLPrintShader(fGpu->glContext(), |
| 338 | GR_GL_GEOMETRY_SHADER, |
| 339 | fGS.fCompilerStrings.begin(), |
| 340 | fGS.fCompilerStringLengths.begin(), |
| 341 | fGS.fCompilerStrings.count(), settings); |
| 342 | } |
| 343 | SkDebugf("\nFS:\n"); |
| 344 | GrGLPrintShader(fGpu->glContext(), |
| 345 | GR_GL_FRAGMENT_SHADER, |
| 346 | fFS.fCompilerStrings.begin(), |
| 347 | fFS.fCompilerStringLengths.begin(), |
| 348 | fFS.fCompilerStrings.count(), |
| 349 | settings); |
| 350 | return nullptr; |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 351 | } |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 352 | } |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 353 | } |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 354 | this->resolveProgramResourceLocations(programID); |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 355 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 356 | this->cleanupShaders(shadersToDelete); |
Ethan Nicholas | 8d05883 | 2019-01-07 10:49:58 -0500 | [diff] [blame] | 357 | if (!cached) { |
| 358 | this->storeShaderInCache(inputs, programID, glsl); |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 359 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 360 | return this->createProgram(programID); |
| 361 | } |
| 362 | |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 363 | void GrGLProgramBuilder::bindProgramResourceLocations(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 364 | fUniformHandler.bindUniformLocations(programID, fGpu->glCaps()); |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 365 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 366 | const GrGLCaps& caps = this->gpu()->glCaps(); |
| 367 | if (fFS.hasCustomColorOutput() && caps.bindFragDataLocationSupport()) { |
| 368 | GL_CALL(BindFragDataLocation(programID, 0, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 369 | GrGLSLFragmentShaderBuilder::DeclaredColorOutputName())); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 370 | } |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 371 | if (fFS.hasSecondaryOutput() && caps.shaderCaps()->mustDeclareFragmentShaderOutput()) { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 372 | GL_CALL(BindFragDataLocationIndexed(programID, 0, 1, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 373 | GrGLSLFragmentShaderBuilder::DeclaredSecondaryColorOutputName())); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 374 | } |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 375 | |
| 376 | // handle NVPR separable varyings |
| 377 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
| 378 | !fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
| 379 | return; |
| 380 | } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 381 | int count = fVaryingHandler.fPathProcVaryingInfos.count(); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 382 | for (int i = 0; i < count; ++i) { |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 383 | GL_CALL(BindFragmentInputLocation(programID, i, |
| 384 | fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str())); |
| 385 | fVaryingHandler.fPathProcVaryingInfos[i].fLocation = i; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 386 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | bool GrGLProgramBuilder::checkLinkStatus(GrGLuint programID) { |
| 390 | GrGLint linked = GR_GL_INIT_ZERO; |
| 391 | GL_CALL(GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
| 392 | if (!linked) { |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 393 | SkDebugf("Program linking failed.\n"); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 394 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 395 | GL_CALL(GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 396 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 397 | if (infoLen > 0) { |
| 398 | // retrieve length even though we don't need it to workaround |
| 399 | // bug in chrome cmd buffer param validation. |
| 400 | GrGLsizei length = GR_GL_INIT_ZERO; |
| 401 | GL_CALL(GetProgramInfoLog(programID, |
| 402 | infoLen+1, |
| 403 | &length, |
| 404 | (char*)log.get())); |
kkinnunen | 297aaf9 | 2015-02-19 06:32:12 -0800 | [diff] [blame] | 405 | SkDebugf("%s", (char*)log.get()); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 406 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 407 | } |
| 408 | return SkToBool(linked); |
| 409 | } |
| 410 | |
kkinnunen | 7aedda5 | 2015-06-29 23:01:28 -0700 | [diff] [blame] | 411 | void GrGLProgramBuilder::resolveProgramResourceLocations(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 412 | fUniformHandler.getUniformLocations(programID, fGpu->glCaps()); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 413 | |
| 414 | // handle NVPR separable varyings |
| 415 | if (!fGpu->glCaps().shaderCaps()->pathRenderingSupport() || |
kkinnunen | 7bdb05d | 2016-01-25 00:47:23 -0800 | [diff] [blame] | 416 | fGpu->glPathRendering()->shouldBindFragmentInputs()) { |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 417 | return; |
| 418 | } |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 419 | int count = fVaryingHandler.fPathProcVaryingInfos.count(); |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 420 | for (int i = 0; i < count; ++i) { |
| 421 | GrGLint location; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 422 | GL_CALL_RET(location, GetProgramResourceLocation( |
| 423 | programID, |
| 424 | GR_GL_FRAGMENT_INPUT, |
| 425 | fVaryingHandler.fPathProcVaryingInfos[i].fVariable.c_str())); |
| 426 | fVaryingHandler.fPathProcVaryingInfos[i].fLocation = location; |
joshualitt | d8dd47b | 2015-09-11 11:45:01 -0700 | [diff] [blame] | 427 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 428 | } |
| 429 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 430 | void GrGLProgramBuilder::cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs) { |
| 431 | GL_CALL(DeleteProgram(programID)); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 432 | this->cleanupShaders(shaderIDs); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 433 | } |
| 434 | void GrGLProgramBuilder::cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs) { |
| 435 | for (int i = 0; i < shaderIDs.count(); ++i) { |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 436 | GL_CALL(DeleteShader(shaderIDs[i])); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | GrGLProgram* GrGLProgramBuilder::createProgram(GrGLuint programID) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 441 | return new GrGLProgram(fGpu, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 442 | fUniformHandles, |
| 443 | programID, |
| 444 | fUniformHandler.fUniforms, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 445 | fUniformHandler.fSamplers, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 446 | fVaryingHandler.fPathProcVaryingInfos, |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 447 | std::move(fGeometryProcessor), |
| 448 | std::move(fXferProcessor), |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 449 | std::move(fFragmentProcessors), |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 450 | fFragmentProcessorCnt, |
| 451 | std::move(fAttributes), |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 452 | fVertexAttributeCnt, |
| 453 | fInstanceAttributeCnt, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 454 | fVertexStride, |
| 455 | fInstanceStride); |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 456 | } |