blob: 96ff10f05dcfc52da41c660d9bd853c74650654c [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
junov@google.comf93e7172011-03-31 21:26:24 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
junov@google.comf93e7172011-03-31 21:26:24 +00009#ifndef GrGLProgram_DEFINED
10#define GrGLProgram_DEFINED
11
robertphillips@google.com6177e692013-02-28 20:16:25 +000012#include "GrGLContext.h"
egdaniel5d8f69f2016-09-07 07:24:12 -070013#include "GrProgramDesc.h"
bsalomon@google.com890e3b52012-06-01 19:01:37 +000014#include "GrGLTexture.h"
kkinnunen7510b222014-07-30 00:04:16 -070015#include "GrGLProgramDataManager.h"
egdaniel018fb622015-10-28 07:26:40 -070016#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080017#include "glsl/GrGLSLUniformHandler.h"
junov@google.comf93e7172011-03-31 21:26:24 +000018
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000019#include "SkString.h"
Scroggo97c88c22011-05-11 14:05:25 +000020
joshualittd8dd47b2015-09-11 11:45:01 -070021#include "builders/GrGLProgramBuilder.h"
22
joshualitt47bb3822014-10-07 16:43:25 -070023class GrGLInstalledProcessors;
joshualitt30ba4362014-08-21 20:18:45 -070024class GrGLProgramBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -080025class GrPipeline;
junov@google.comf93e7172011-03-31 21:26:24 +000026
27/**
28 * This class manages a GPU program and records per-program information.
29 * We can specify the attribute locations so that they are constant
30 * across our shaders. But the driver determines the uniform locations
31 * at link time. We don't need to remember the sampler uniform location
32 * because we will bind a texture slot to it and never change it
33 * Uniforms are program-local so we can't rely on fHWState to hold the
34 * previous uniform state after a program change.
35 */
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000036class GrGLProgram : public SkRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000037public:
egdaniel7ea439b2015-12-03 09:20:44 -080038 typedef GrGLSLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles;
kkinnunendddc18a2014-08-03 23:19:46 -070039
joshualittd8dd47b2015-09-11 11:45:01 -070040 ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000041
bsalomon@google.com34cccde2013-01-04 18:34:30 +000042 /**
43 * Call to abandon GL objects owned by this program.
44 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000045 void abandon();
46
joshualitt79f8fae2014-10-28 17:59:26 -070047 const GrProgramDesc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000048
bsalomon@google.com271cffc2011-05-20 14:13:56 +000049 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000050 * Gets the GL program ID for this program.
51 */
kkinnunendddc18a2014-08-03 23:19:46 -070052 GrGLuint programID() const { return fProgramID; }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000053
54 /**
joshualittee2af952014-12-30 09:04:15 -080055 * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device
56 * space and to make device space positions have the correct origin for processors that require
57 * them.
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000058 */
joshualittee2af952014-12-30 09:04:15 -080059 struct RenderTargetState {
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000060 SkISize fRenderTargetSize;
61 GrSurfaceOrigin fRenderTargetOrigin;
62
joshualittee2af952014-12-30 09:04:15 -080063 RenderTargetState() { this->invalidate(); }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000064 void invalidate() {
bsalomon@google.com45a412e2013-02-13 16:13:13 +000065 fRenderTargetSize.fWidth = -1;
66 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000067 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
68 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000069
70 /**
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040071 * Gets a float4 that adjusts the position from Skia device coords to GL's normalized device
72 * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000073 * applied as such:
74 * pos.x = dot(v.xy, pos.xz)
robertphillipsef4ba3d2015-09-17 11:21:06 -070075 * pos.y = dot(v.zw, pos.yz)
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000076 */
egdaniel018fb622015-10-28 07:26:40 -070077 void getRTAdjustmentVec(float* destVec) {
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000078 destVec[0] = 2.f / fRenderTargetSize.fWidth;
79 destVec[1] = -1.f;
80 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
81 destVec[2] = -2.f / fRenderTargetSize.fHeight;
82 destVec[3] = 1.f;
83 } else {
84 destVec[2] = 2.f / fRenderTargetSize.fHeight;
85 destVec[3] = -1.f;
86 }
87 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000088 };
89
90 /**
egdaniel7dc4bd02015-10-29 07:57:01 -070091 * This function uploads uniforms, calls each GrGL*Processor's setData, and retrieves the
cdalton42717652015-06-18 11:54:30 -070092 * textures that need to be bound on each unit. It is the caller's responsibility to ensure
93 * the program is bound before calling, and to bind the outgoing textures to their respective
94 * units upon return. (Each index in the array corresponds to its matching GL texture unit.)
bsalomon@google.com4285acc2012-10-22 14:11:24 +000095 */
cdalton74b8d322016-04-11 14:47:28 -070096 void setData(const GrPrimitiveProcessor&, const GrPipeline&);
bsalomon@google.com91961302011-05-09 18:39:58 +000097
brianosman33f6b3f2016-06-02 05:49:21 -070098 /**
99 * This function retrieves the textures that need to be used by each GrGL*Processor, and
100 * ensures that any textures requiring mipmaps have their mipmaps correctly built.
101 */
102 void generateMipmaps(const GrPrimitiveProcessor&, const GrPipeline&);
103
joshualitt47bb3822014-10-07 16:43:25 -0700104protected:
Brian Salomonf9f45122016-11-29 11:59:17 -0500105 using UniformHandle = GrGLSLProgramDataManager::UniformHandle ;
106 using UniformInfoArray = GrGLProgramDataManager::UniformInfoArray;
107 using VaryingInfoArray = GrGLProgramDataManager::VaryingInfoArray;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000108
bsalomon861e1032014-12-16 07:33:49 -0800109 GrGLProgram(GrGLGpu*,
joshualitt79f8fae2014-10-28 17:59:26 -0700110 const GrProgramDesc&,
joshualitt47bb3822014-10-07 16:43:25 -0700111 const BuiltinUniformHandles&,
112 GrGLuint programID,
Brian Salomon101b8442016-11-18 11:58:54 -0500113 const UniformInfoArray& uniforms,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400114 const UniformInfoArray& textureSamplers,
115 const UniformInfoArray& texelBuffers,
Brian Salomonf9f45122016-11-29 11:59:17 -0500116 const UniformInfoArray& imageStorages,
egdaniel0eafe792015-11-20 14:01:22 -0800117 const VaryingInfoArray&, // used for NVPR only currently
Robert Phillips369e8b72017-08-01 16:13:04 -0400118 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
119 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
egdaniel09aa1fc2016-04-20 07:09:46 -0700120 const GrGLSLFragProcs& fragmentProcessors);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000121
cdalton74b8d322016-04-11 14:47:28 -0700122 // A helper to loop over effects, set the transforms (via subclass) and bind textures
Greg Danielbc5d4d72017-05-05 10:28:42 -0400123 void setFragmentData(const GrPrimitiveProcessor&, const GrPipeline&, int* nextTexSamplerIdx,
124 int* nextTexelBufferIdx, int* nextImageStorageIdx);
joshualitt47bb3822014-10-07 16:43:25 -0700125
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000126 // Helper for setData() that sets the view matrix and loads the render target height uniform
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400127 void setRenderTargetState(const GrPrimitiveProcessor&, const GrRenderTargetProxy*);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000128
cdalton74b8d322016-04-11 14:47:28 -0700129 // Helper for setData() that binds textures and texel buffers to the appropriate texture units
Greg Danielbc5d4d72017-05-05 10:28:42 -0400130 void bindTextures(const GrResourceIOProcessor&, bool allowSRGBInputs, int* nextSamplerIdx,
131 int* nextTexelBufferIdx, int* nextImageStorageIdx);
cdalton74b8d322016-04-11 14:47:28 -0700132
brianosman33f6b3f2016-06-02 05:49:21 -0700133 // Helper for generateMipmaps() that ensures mipmaps are up to date
Brian Salomonab015ef2017-04-04 10:15:51 -0400134 void generateMipmaps(const GrResourceIOProcessor&, bool allowSRGBInputs);
brianosman33f6b3f2016-06-02 05:49:21 -0700135
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000136 // these reflect the current values of uniforms (GL uniform values travel with program)
joshualittee2af952014-12-30 09:04:15 -0800137 RenderTargetState fRenderTargetState;
joshualitt47bb3822014-10-07 16:43:25 -0700138 BuiltinUniformHandles fBuiltinUniformHandles;
139 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000140
joshualitt47bb3822014-10-07 16:43:25 -0700141 // the installed effects
Ben Wagner145dbcd2016-11-03 14:40:50 -0400142 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
143 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
egdanielfa896322016-01-13 12:19:30 -0800144 GrGLSLFragProcs fFragmentProcessors;
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000145
joshualitt79f8fae2014-10-28 17:59:26 -0700146 GrProgramDesc fDesc;
bsalomon861e1032014-12-16 07:33:49 -0800147 GrGLGpu* fGpu;
joshualitt47bb3822014-10-07 16:43:25 -0700148 GrGLProgramDataManager fProgramDataManager;
junov@google.comf93e7172011-03-31 21:26:24 +0000149
Greg Danielbc5d4d72017-05-05 10:28:42 -0400150 int fNumTextureSamplers;
151 int fNumTexelBuffers;
152 int fNumImageStorages;
153
joshualitt47bb3822014-10-07 16:43:25 -0700154 friend class GrGLProgramBuilder;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000155
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000156 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000157};
158
junov@google.comf93e7172011-03-31 21:26:24 +0000159#endif