blob: 78cf484198d4b301256e1bc8036585876009b9ce [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
kkinnunen7510b222014-07-30 00:04:16 -070012#include "GrGLProgramDataManager.h"
egdaniel018fb622015-10-28 07:26:40 -070013#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080014#include "glsl/GrGLSLUniformHandler.h"
junov@google.comf93e7172011-03-31 21:26:24 +000015
Brian Salomon1471df92018-06-08 10:49:00 -040016class GrGLSLFragmentProcessor;
17class GrGLSLPrimitiveProcessor;
18class GrGLSLXferProcessor;
egdaniel8dd688b2015-01-22 10:16:09 -080019class GrPipeline;
Brian Salomon1471df92018-06-08 10:49:00 -040020class GrPrimitiveProcessor;
Robert Phillipsd0fe8752019-01-31 14:13:59 -050021class GrRenderTarget;
Brian Salomon7eae3e02018-08-07 14:02:38 +000022class GrTextureProxy;
junov@google.comf93e7172011-03-31 21:26:24 +000023
24/**
Brian Salomon802cb312018-06-08 18:05:20 -040025 * This class manages a GPU program and records per-program information. It also records the vertex
26 * and instance attribute layouts that are to be used with the program.
junov@google.comf93e7172011-03-31 21:26:24 +000027 */
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000028class GrGLProgram : public SkRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000029public:
Brian Salomon802cb312018-06-08 18:05:20 -040030 /**
31 * This class has its own Attribute representation as it does not need the name and we don't
32 * want to worry about copying the name string to memory with life time of GrGLProgram.
33 * Additionally, these store the attribute location.
34 */
35 struct Attribute {
Brian Osman4a3f5c82018-09-18 16:16:38 -040036 GrVertexAttribType fCPUType;
37 GrSLType fGPUType;
Brian Salomon92be2f72018-06-19 14:33:47 -040038 size_t fOffset;
Brian Salomon802cb312018-06-08 18:05:20 -040039 GrGLint fLocation;
Brian Salomon802cb312018-06-08 18:05:20 -040040 };
41
Brian Salomon1471df92018-06-08 10:49:00 -040042 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
43 using UniformInfoArray = GrGLProgramDataManager::UniformInfoArray;
44 using VaryingInfoArray = GrGLProgramDataManager::VaryingInfoArray;
45
Brian Salomon92be2f72018-06-19 14:33:47 -040046 /**
47 * The attribute array consists of vertexAttributeCnt + instanceAttributeCnt elements with
48 * the vertex attributes preceding the instance attributes.
49 */
Brian Salomon1471df92018-06-08 10:49:00 -040050 GrGLProgram(GrGLGpu*,
51 const GrGLSLBuiltinUniformHandles&,
52 GrGLuint programID,
53 const UniformInfoArray& uniforms,
54 const UniformInfoArray& textureSamplers,
Brian Salomon1471df92018-06-08 10:49:00 -040055 const VaryingInfoArray&, // used for NVPR only currently
56 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
57 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
58 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
Brian Salomon802cb312018-06-08 18:05:20 -040059 int fragmentProcessorCnt,
60 std::unique_ptr<Attribute[]>,
Brian Salomon92be2f72018-06-19 14:33:47 -040061 int vertexAttributeCnt,
62 int instanceAttributeCnt,
Brian Salomon802cb312018-06-08 18:05:20 -040063 int vertexStride,
64 int instanceStride);
kkinnunendddc18a2014-08-03 23:19:46 -070065
joshualittd8dd47b2015-09-11 11:45:01 -070066 ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000067
bsalomon@google.com34cccde2013-01-04 18:34:30 +000068 /**
69 * Call to abandon GL objects owned by this program.
70 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000071 void abandon();
72
bsalomon@google.com271cffc2011-05-20 14:13:56 +000073 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000074 * Gets the GL program ID for this program.
75 */
kkinnunendddc18a2014-08-03 23:19:46 -070076 GrGLuint programID() const { return fProgramID; }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000077
78 /**
joshualittee2af952014-12-30 09:04:15 -080079 * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device
80 * space and to make device space positions have the correct origin for processors that require
81 * them.
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000082 */
joshualittee2af952014-12-30 09:04:15 -080083 struct RenderTargetState {
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000084 SkISize fRenderTargetSize;
85 GrSurfaceOrigin fRenderTargetOrigin;
86
joshualittee2af952014-12-30 09:04:15 -080087 RenderTargetState() { this->invalidate(); }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000088 void invalidate() {
bsalomon@google.com45a412e2013-02-13 16:13:13 +000089 fRenderTargetSize.fWidth = -1;
90 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000091 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
92 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000093
94 /**
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040095 * Gets a float4 that adjusts the position from Skia device coords to GL's normalized device
96 * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000097 * applied as such:
98 * pos.x = dot(v.xy, pos.xz)
robertphillipsef4ba3d2015-09-17 11:21:06 -070099 * pos.y = dot(v.zw, pos.yz)
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000100 */
egdaniel018fb622015-10-28 07:26:40 -0700101 void getRTAdjustmentVec(float* destVec) {
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000102 destVec[0] = 2.f / fRenderTargetSize.fWidth;
103 destVec[1] = -1.f;
104 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
105 destVec[2] = -2.f / fRenderTargetSize.fHeight;
106 destVec[3] = 1.f;
107 } else {
108 destVec[2] = 2.f / fRenderTargetSize.fHeight;
109 destVec[3] = -1.f;
110 }
111 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000112 };
113
114 /**
Brian Salomon7eae3e02018-08-07 14:02:38 +0000115 * This function uploads uniforms, calls each GrGLSL*Processor's setData. It binds all fragment
Brian Salomonf7232642018-09-19 08:58:08 -0400116 * processor textures. Primitive process textures can be bound using this function or by
117 * calling updatePrimitiveProcessorTextureBindings.
Brian Salomon7eae3e02018-08-07 14:02:38 +0000118 *
119 * It is the caller's responsibility to ensure the program is bound before calling.
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000120 */
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500121 void updateUniformsAndTextureBindings(const GrRenderTarget*, GrSurfaceOrigin,
122 const GrPrimitiveProcessor&, const GrPipeline&,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000123 const GrTextureProxy* const primitiveProcessorTextures[]);
bsalomon@google.com91961302011-05-09 18:39:58 +0000124
Brian Salomonf7232642018-09-19 08:58:08 -0400125 void updatePrimitiveProcessorTextureBindings(const GrPrimitiveProcessor&,
126 const GrTextureProxy* const[]);
127
Brian Salomon802cb312018-06-08 18:05:20 -0400128 int vertexStride() const { return fVertexStride; }
129 int instanceStride() const { return fInstanceStride; }
130
Brian Salomon92be2f72018-06-19 14:33:47 -0400131 int numVertexAttributes() const { return fVertexAttributeCnt; }
132 const Attribute& vertexAttribute(int i) const {
133 SkASSERT(i >= 0 && i < fVertexAttributeCnt);
134 return fAttributes[i];
135 }
136
137 int numInstanceAttributes() const { return fInstanceAttributeCnt; }
138 const Attribute& instanceAttribute(int i) const {
139 SkASSERT(i >= 0 && i < fInstanceAttributeCnt);
140 return fAttributes[i + fVertexAttributeCnt];
141 }
brianosman33f6b3f2016-06-02 05:49:21 -0700142
Brian Salomon1471df92018-06-08 10:49:00 -0400143private:
cdalton74b8d322016-04-11 14:47:28 -0700144 // A helper to loop over effects, set the transforms (via subclass) and bind textures
Brian Salomone782f842018-07-31 13:53:11 -0400145 void setFragmentData(const GrPipeline&, int* nextTexSamplerIdx);
joshualitt47bb3822014-10-07 16:43:25 -0700146
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000147 // Helper for setData() that sets the view matrix and loads the render target height uniform
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500148 void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin, const GrPrimitiveProcessor&);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000149
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000150 // these reflect the current values of uniforms (GL uniform values travel with program)
joshualittee2af952014-12-30 09:04:15 -0800151 RenderTargetState fRenderTargetState;
Brian Salomon1471df92018-06-08 10:49:00 -0400152 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
joshualitt47bb3822014-10-07 16:43:25 -0700153 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000154
joshualitt47bb3822014-10-07 16:43:25 -0700155 // the installed effects
Brian Salomon802cb312018-06-08 18:05:20 -0400156 std::unique_ptr<GrGLSLPrimitiveProcessor> fPrimitiveProcessor;
Ben Wagner145dbcd2016-11-03 14:40:50 -0400157 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400158 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
159 int fFragmentProcessorCnt;
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000160
Brian Salomon802cb312018-06-08 18:05:20 -0400161 std::unique_ptr<Attribute[]> fAttributes;
Brian Salomon92be2f72018-06-19 14:33:47 -0400162 int fVertexAttributeCnt;
163 int fInstanceAttributeCnt;
Brian Salomon802cb312018-06-08 18:05:20 -0400164 int fVertexStride;
165 int fInstanceStride;
166
bsalomon861e1032014-12-16 07:33:49 -0800167 GrGLGpu* fGpu;
joshualitt47bb3822014-10-07 16:43:25 -0700168 GrGLProgramDataManager fProgramDataManager;
junov@google.comf93e7172011-03-31 21:26:24 +0000169
Greg Danielbc5d4d72017-05-05 10:28:42 -0400170 int fNumTextureSamplers;
Greg Danielbc5d4d72017-05-05 10:28:42 -0400171
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000172 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000173};
174
junov@google.comf93e7172011-03-31 21:26:24 +0000175#endif