blob: 6ea1f2cbf9c911174455dedadcce8bc67494bdba [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"
Brian Salomon802cb312018-06-08 18:05:20 -040013#include "GrPrimitiveProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070014#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080015#include "glsl/GrGLSLUniformHandler.h"
junov@google.comf93e7172011-03-31 21:26:24 +000016
Brian Salomon1471df92018-06-08 10:49:00 -040017class GrGLSLFragmentProcessor;
18class GrGLSLPrimitiveProcessor;
19class GrGLSLXferProcessor;
egdaniel8dd688b2015-01-22 10:16:09 -080020class GrPipeline;
Brian Salomon1471df92018-06-08 10:49:00 -040021class GrPrimitiveProcessor;
22class GrRenderTargetProxy;
23class GrResourceIOProcessor;
junov@google.comf93e7172011-03-31 21:26:24 +000024
25/**
Brian Salomon802cb312018-06-08 18:05:20 -040026 * This class manages a GPU program and records per-program information. It also records the vertex
27 * and instance attribute layouts that are to be used with the program.
junov@google.comf93e7172011-03-31 21:26:24 +000028 */
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000029class GrGLProgram : public SkRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000030public:
Brian Salomon802cb312018-06-08 18:05:20 -040031 /**
32 * This class has its own Attribute representation as it does not need the name and we don't
33 * want to worry about copying the name string to memory with life time of GrGLProgram.
34 * Additionally, these store the attribute location.
35 */
36 struct Attribute {
37 GrVertexAttribType fType;
38 int fOffset;
39 GrGLint fLocation;
40 GrPrimitiveProcessor::Attribute::InputRate fInputRate;
41 };
42
Brian Salomon1471df92018-06-08 10:49:00 -040043 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
44 using UniformInfoArray = GrGLProgramDataManager::UniformInfoArray;
45 using VaryingInfoArray = GrGLProgramDataManager::VaryingInfoArray;
46
47 GrGLProgram(GrGLGpu*,
48 const GrGLSLBuiltinUniformHandles&,
49 GrGLuint programID,
50 const UniformInfoArray& uniforms,
51 const UniformInfoArray& textureSamplers,
52 const UniformInfoArray& texelBuffers,
53 const VaryingInfoArray&, // used for NVPR only currently
54 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
55 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
56 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
Brian Salomon802cb312018-06-08 18:05:20 -040057 int fragmentProcessorCnt,
58 std::unique_ptr<Attribute[]>,
59 int attributeCnt,
60 int vertexStride,
61 int instanceStride);
kkinnunendddc18a2014-08-03 23:19:46 -070062
joshualittd8dd47b2015-09-11 11:45:01 -070063 ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000064
bsalomon@google.com34cccde2013-01-04 18:34:30 +000065 /**
66 * Call to abandon GL objects owned by this program.
67 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000068 void abandon();
69
bsalomon@google.com271cffc2011-05-20 14:13:56 +000070 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000071 * Gets the GL program ID for this program.
72 */
kkinnunendddc18a2014-08-03 23:19:46 -070073 GrGLuint programID() const { return fProgramID; }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000074
75 /**
joshualittee2af952014-12-30 09:04:15 -080076 * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device
77 * space and to make device space positions have the correct origin for processors that require
78 * them.
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000079 */
joshualittee2af952014-12-30 09:04:15 -080080 struct RenderTargetState {
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000081 SkISize fRenderTargetSize;
82 GrSurfaceOrigin fRenderTargetOrigin;
83
joshualittee2af952014-12-30 09:04:15 -080084 RenderTargetState() { this->invalidate(); }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000085 void invalidate() {
bsalomon@google.com45a412e2013-02-13 16:13:13 +000086 fRenderTargetSize.fWidth = -1;
87 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000088 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
89 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000090
91 /**
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040092 * Gets a float4 that adjusts the position from Skia device coords to GL's normalized device
93 * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000094 * applied as such:
95 * pos.x = dot(v.xy, pos.xz)
robertphillipsef4ba3d2015-09-17 11:21:06 -070096 * pos.y = dot(v.zw, pos.yz)
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000097 */
egdaniel018fb622015-10-28 07:26:40 -070098 void getRTAdjustmentVec(float* destVec) {
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000099 destVec[0] = 2.f / fRenderTargetSize.fWidth;
100 destVec[1] = -1.f;
101 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
102 destVec[2] = -2.f / fRenderTargetSize.fHeight;
103 destVec[3] = 1.f;
104 } else {
105 destVec[2] = 2.f / fRenderTargetSize.fHeight;
106 destVec[3] = -1.f;
107 }
108 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000109 };
110
111 /**
egdaniel7dc4bd02015-10-29 07:57:01 -0700112 * This function uploads uniforms, calls each GrGL*Processor's setData, and retrieves the
cdalton42717652015-06-18 11:54:30 -0700113 * textures that need to be bound on each unit. It is the caller's responsibility to ensure
114 * the program is bound before calling, and to bind the outgoing textures to their respective
115 * units upon return. (Each index in the array corresponds to its matching GL texture unit.)
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000116 */
cdalton74b8d322016-04-11 14:47:28 -0700117 void setData(const GrPrimitiveProcessor&, const GrPipeline&);
bsalomon@google.com91961302011-05-09 18:39:58 +0000118
brianosman33f6b3f2016-06-02 05:49:21 -0700119 /**
120 * This function retrieves the textures that need to be used by each GrGL*Processor, and
121 * ensures that any textures requiring mipmaps have their mipmaps correctly built.
122 */
123 void generateMipmaps(const GrPrimitiveProcessor&, const GrPipeline&);
Brian Salomon802cb312018-06-08 18:05:20 -0400124 int vertexStride() const { return fVertexStride; }
125 int instanceStride() const { return fInstanceStride; }
126
127 int numAttributes() const { return fAttributeCnt; }
128 const Attribute& attribute(int i) const { return fAttributes[i]; }
brianosman33f6b3f2016-06-02 05:49:21 -0700129
Brian Salomon1471df92018-06-08 10:49:00 -0400130private:
cdalton74b8d322016-04-11 14:47:28 -0700131 // A helper to loop over effects, set the transforms (via subclass) and bind textures
Greg Danielbc5d4d72017-05-05 10:28:42 -0400132 void setFragmentData(const GrPrimitiveProcessor&, const GrPipeline&, int* nextTexSamplerIdx,
Brian Salomon559f5562017-11-15 14:28:33 -0500133 int* nextTexelBufferIdx);
joshualitt47bb3822014-10-07 16:43:25 -0700134
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000135 // Helper for setData() that sets the view matrix and loads the render target height uniform
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400136 void setRenderTargetState(const GrPrimitiveProcessor&, const GrRenderTargetProxy*);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000137
cdalton74b8d322016-04-11 14:47:28 -0700138 // Helper for setData() that binds textures and texel buffers to the appropriate texture units
Brian Osman2b23c4b2018-06-01 12:25:08 -0400139 void bindTextures(const GrResourceIOProcessor&, int* nextSamplerIdx, int* nextTexelBufferIdx);
cdalton74b8d322016-04-11 14:47:28 -0700140
brianosman33f6b3f2016-06-02 05:49:21 -0700141 // Helper for generateMipmaps() that ensures mipmaps are up to date
Brian Osman2b23c4b2018-06-01 12:25:08 -0400142 void generateMipmaps(const GrResourceIOProcessor&);
brianosman33f6b3f2016-06-02 05:49:21 -0700143
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000144 // these reflect the current values of uniforms (GL uniform values travel with program)
joshualittee2af952014-12-30 09:04:15 -0800145 RenderTargetState fRenderTargetState;
Brian Salomon1471df92018-06-08 10:49:00 -0400146 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
joshualitt47bb3822014-10-07 16:43:25 -0700147 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000148
joshualitt47bb3822014-10-07 16:43:25 -0700149 // the installed effects
Brian Salomon802cb312018-06-08 18:05:20 -0400150 std::unique_ptr<GrGLSLPrimitiveProcessor> fPrimitiveProcessor;
Ben Wagner145dbcd2016-11-03 14:40:50 -0400151 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400152 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
153 int fFragmentProcessorCnt;
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000154
Brian Salomon802cb312018-06-08 18:05:20 -0400155 std::unique_ptr<Attribute[]> fAttributes;
156 int fAttributeCnt;
157 int fVertexStride;
158 int fInstanceStride;
159
bsalomon861e1032014-12-16 07:33:49 -0800160 GrGLGpu* fGpu;
joshualitt47bb3822014-10-07 16:43:25 -0700161 GrGLProgramDataManager fProgramDataManager;
junov@google.comf93e7172011-03-31 21:26:24 +0000162
Greg Danielbc5d4d72017-05-05 10:28:42 -0400163 int fNumTextureSamplers;
164 int fNumTexelBuffers;
Greg Danielbc5d4d72017-05-05 10:28:42 -0400165
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000166 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000167};
168
junov@google.comf93e7172011-03-31 21:26:24 +0000169#endif