| 1 |
/* |
| 2 |
* Copyright 2011 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 |
#include "src/gpu/ganesh/gl/GrGLProgram.h" |
| 8 |
|
| 9 |
#include "include/core/SkSamplingOptions.h" |
| 10 |
#include "include/gpu/ganesh/GrTypes.h" |
| 11 |
#include "include/gpu/ganesh/gl/GrGLFunctions.h" |
| 12 |
#include "include/gpu/ganesh/gl/GrGLInterface.h" |
| 13 |
#include "src/gpu/Swizzle.h" |
| 14 |
#include "src/gpu/ganesh/GrCaps.h" |
| 15 |
#include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 16 |
#include "src/gpu/ganesh/GrGeometryProcessor.h" |
| 17 |
#include "src/gpu/ganesh/GrPipeline.h" |
| 18 |
#include "src/gpu/ganesh/GrProgramInfo.h" |
| 19 |
#include "src/gpu/ganesh/GrRenderTarget.h" |
| 20 |
#include "src/gpu/ganesh/GrSamplerState.h" |
| 21 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 22 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 23 |
#include "src/gpu/ganesh/GrXferProcessor.h" |
| 24 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 25 |
#include "src/gpu/ganesh/gl/GrGLGpu.h" |
| 26 |
#include "src/gpu/ganesh/gl/GrGLTexture.h" |
| 27 |
#include "src/gpu/ganesh/gl/GrGLUtil.h" |
| 28 |
#include "src/sksl/SkSLCompiler.h" |
| 29 |
|
| 30 |
#include <array> |
| 31 |
#include <functional> |
| 32 |
#include <utility> |
| 33 |
|
| 34 |
class GrTexture; |
| 35 |
|
| 36 |
#define GL_CALL(X) GR_GL_CALL(fGpu->glInterface(), X) |
| 37 |
|
| 38 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 39 |
|
| 40 |
sk_sp<GrGLProgram> GrGLProgram::Make( |
| 41 |
GrGLGpu* gpu, |
| 42 |
const GrGLSLBuiltinUniformHandles& builtinUniforms, |
| 43 |
GrGLuint programID, |
| 44 |
const UniformInfoArray& uniforms, |
| 45 |
const UniformInfoArray& textureSamplers, |
| 46 |
std::unique_ptr<GrGeometryProcessor::ProgramImpl> gpImpl, |
| 47 |
std::unique_ptr<GrXferProcessor::ProgramImpl> xpImpl, |
| 48 |
std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls, |
| 49 |
std::unique_ptr<Attribute[]> attributes, |
| 50 |
int vertexAttributeCnt, |
| 51 |
int instanceAttributeCnt, |
| 52 |
int vertexStride, |
| 53 |
int instanceStride) { |
| 54 |
sk_sp<GrGLProgram> program(new GrGLProgram(gpu, |
| 55 |
builtinUniforms, |
| 56 |
programID, |
| 57 |
uniforms, |
| 58 |
textureSamplers, |
| 59 |
std::move(gpImpl), |
| 60 |
std::move(xpImpl), |
| 61 |
std::move(fpImpls), |
| 62 |
std::move(attributes), |
| 63 |
vertexAttributeCnt, |
| 64 |
instanceAttributeCnt, |
| 65 |
vertexStride, |
| 66 |
instanceStride)); |
| 67 |
// Assign texture units to sampler uniforms one time up front. |
| 68 |
gpu->flushProgram(program); |
| 69 |
program->fProgramDataManager.setSamplerUniforms(textureSamplers, 0); |
| 70 |
return program; |
| 71 |
} |
| 72 |
|
| 73 |
GrGLProgram::GrGLProgram(GrGLGpu* gpu, |
| 74 |
const GrGLSLBuiltinUniformHandles& builtinUniforms, |
| 75 |
GrGLuint programID, |
| 76 |
const UniformInfoArray& uniforms, |
| 77 |
const UniformInfoArray& textureSamplers, |
| 78 |
std::unique_ptr<GrGeometryProcessor::ProgramImpl> gpImpl, |
| 79 |
std::unique_ptr<GrXferProcessor::ProgramImpl> xpImpl, |
| 80 |
std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls, |
| 81 |
std::unique_ptr<Attribute[]> attributes, |
| 82 |
int vertexAttributeCnt, |
| 83 |
int instanceAttributeCnt, |
| 84 |
int vertexStride, |
| 85 |
int instanceStride) |
| 86 |
: fBuiltinUniformHandles(builtinUniforms) |
| 87 |
, fProgramID(programID) |
| 88 |
, fGPImpl(std::move(gpImpl)) |
| 89 |
, fXPImpl(std::move(xpImpl)) |
| 90 |
, fFPImpls(std::move(fpImpls)) |
| 91 |
, fAttributes(std::move(attributes)) |
| 92 |
, fVertexAttributeCnt(vertexAttributeCnt) |
| 93 |
, fInstanceAttributeCnt(instanceAttributeCnt) |
| 94 |
, fVertexStride(vertexStride) |
| 95 |
, fInstanceStride(instanceStride) |
| 96 |
, fGpu(gpu) |
| 97 |
, fProgramDataManager(gpu, uniforms) |
| 98 |
, fNumTextureSamplers(textureSamplers.count()) {} |
| 99 |
|
| 100 |
GrGLProgram::~GrGLProgram() { |
| 101 |
if (fProgramID) { |
| 102 |
GL_CALL(DeleteProgram(fProgramID)); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void GrGLProgram::abandon() { |
| 107 |
fProgramID = 0; |
| 108 |
} |
| 109 |
|
| 110 |
/////////////////////////////////////////////////////////////////////////////// |
| 111 |
|
| 112 |
void GrGLProgram::updateUniforms(const GrRenderTarget* renderTarget, |
| 113 |
const GrProgramInfo& programInfo) { |
| 114 |
this->setRenderTargetState(renderTarget, programInfo.origin(), programInfo.geomProc()); |
| 115 |
|
| 116 |
// we set the uniforms for installed processors in a generic way, but subclasses of GLProgram |
| 117 |
// determine how to set coord transforms |
| 118 |
|
| 119 |
// We must bind to texture units in the same order in which we set the uniforms in |
| 120 |
// GrGLProgramDataManager. That is, we bind textures for processors in this order: |
| 121 |
// primProc, fragProcs, XP. |
| 122 |
fGPImpl->setData(fProgramDataManager, *fGpu->caps()->shaderCaps(), programInfo.geomProc()); |
| 123 |
|
| 124 |
for (int i = 0; i < programInfo.pipeline().numFragmentProcessors(); ++i) { |
| 125 |
const auto& fp = programInfo.pipeline().getFragmentProcessor(i); |
| 126 |
fp.visitWithImpls([&](const GrFragmentProcessor& fp, |
| 127 |
GrFragmentProcessor::ProgramImpl& impl) { |
| 128 |
impl.setData(fProgramDataManager, fp); |
| 129 |
}, *fFPImpls[i]); |
| 130 |
} |
| 131 |
|
| 132 |
programInfo.pipeline().setDstTextureUniforms(fProgramDataManager, &fBuiltinUniformHandles); |
| 133 |
fXPImpl->setData(fProgramDataManager, programInfo.pipeline().getXferProcessor()); |
| 134 |
} |
| 135 |
|
| 136 |
void GrGLProgram::bindTextures(const GrGeometryProcessor& geomProc, |
| 137 |
const GrSurfaceProxy* const geomProcTextures[], |
| 138 |
const GrPipeline& pipeline) { |
| 139 |
// Bind textures from the geometry processor. |
| 140 |
for (int i = 0; i < geomProc.numTextureSamplers(); ++i) { |
| 141 |
SkASSERT(geomProcTextures[i]->asTextureProxy()); |
| 142 |
auto* overrideTexture = static_cast<GrGLTexture*>(geomProcTextures[i]->peekTexture()); |
| 143 |
fGpu->bindTexture(i, geomProc.textureSampler(i).samplerState(), |
| 144 |
geomProc.textureSampler(i).swizzle(), overrideTexture); |
| 145 |
} |
| 146 |
int nextTexSamplerIdx = geomProc.numTextureSamplers(); |
| 147 |
// Bind texture from the destination proxy view. |
| 148 |
GrTexture* dstTexture = pipeline.peekDstTexture(); |
| 149 |
if (dstTexture) { |
| 150 |
fGpu->bindTexture(nextTexSamplerIdx++, GrSamplerState::Filter::kNearest, |
| 151 |
pipeline.dstProxyView().swizzle(), static_cast<GrGLTexture*>(dstTexture)); |
| 152 |
} |
| 153 |
// Bind textures from all of the fragment processors. |
| 154 |
pipeline.visitTextureEffects([&](const GrTextureEffect& te) { |
| 155 |
GrSamplerState samplerState = te.samplerState(); |
| 156 |
skgpu::Swizzle swizzle = te.view().swizzle(); |
| 157 |
auto* texture = static_cast<GrGLTexture*>(te.texture()); |
| 158 |
fGpu->bindTexture(nextTexSamplerIdx++, samplerState, swizzle, texture); |
| 159 |
}); |
| 160 |
|
| 161 |
SkASSERT(nextTexSamplerIdx == fNumTextureSamplers); |
| 162 |
} |
| 163 |
|
| 164 |
void GrGLProgram::setRenderTargetState(const GrRenderTarget* rt, |
| 165 |
GrSurfaceOrigin origin, |
| 166 |
const GrGeometryProcessor& geomProc) { |
| 167 |
// Set RT adjustment and RT flip |
| 168 |
SkISize dimensions = rt->dimensions(); |
| 169 |
if (fRenderTargetState.fRenderTargetOrigin != origin || |
| 170 |
fRenderTargetState.fRenderTargetSize != dimensions) { |
| 171 |
fRenderTargetState.fRenderTargetSize = dimensions; |
| 172 |
fRenderTargetState.fRenderTargetOrigin = origin; |
| 173 |
|
| 174 |
// The client will mark a swap buffer as kBottomLeft when making a SkSurface because |
| 175 |
// GL's framebuffer space has (0, 0) at the bottom left. In NDC (-1, -1) is also the |
| 176 |
// bottom left. However, Skia's device coords has (0, 0) at the top left, so a flip is |
| 177 |
// required when the origin is kBottomLeft. |
| 178 |
bool flip = (origin == kBottomLeft_GrSurfaceOrigin); |
| 179 |
std::array<float, 4> v = SkSL::Compiler::GetRTAdjustVector(dimensions, flip); |
| 180 |
fProgramDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, v.data()); |
| 181 |
if (fBuiltinUniformHandles.fRTFlipUni.isValid()) { |
| 182 |
std::array<float, 2> d = SkSL::Compiler::GetRTFlipVector(dimensions.height(), flip); |
| 183 |
fProgramDataManager.set2fv(fBuiltinUniformHandles.fRTFlipUni, 1, d.data()); |
| 184 |
} |
| 185 |
} |
| 186 |
} |