blob: a43e4f4294e649719ad413df6878cb3da33422bd [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;
21class GrRenderTargetProxy;
22class GrResourceIOProcessor;
junov@google.comf93e7172011-03-31 21:26:24 +000023
24/**
25 * This class manages a GPU program and records per-program information.
26 * We can specify the attribute locations so that they are constant
27 * across our shaders. But the driver determines the uniform locations
28 * at link time. We don't need to remember the sampler uniform location
29 * because we will bind a texture slot to it and never change it
30 * Uniforms are program-local so we can't rely on fHWState to hold the
31 * previous uniform state after a program change.
32 */
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000033class GrGLProgram : public SkRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000034public:
Brian Salomon1471df92018-06-08 10:49:00 -040035 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
36 using UniformInfoArray = GrGLProgramDataManager::UniformInfoArray;
37 using VaryingInfoArray = GrGLProgramDataManager::VaryingInfoArray;
38
39 GrGLProgram(GrGLGpu*,
40 const GrGLSLBuiltinUniformHandles&,
41 GrGLuint programID,
42 const UniformInfoArray& uniforms,
43 const UniformInfoArray& textureSamplers,
44 const UniformInfoArray& texelBuffers,
45 const VaryingInfoArray&, // used for NVPR only currently
46 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
47 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
48 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
49 int fragmentProcessorCnt);
kkinnunendddc18a2014-08-03 23:19:46 -070050
joshualittd8dd47b2015-09-11 11:45:01 -070051 ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000052
bsalomon@google.com34cccde2013-01-04 18:34:30 +000053 /**
54 * Call to abandon GL objects owned by this program.
55 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000056 void abandon();
57
bsalomon@google.com271cffc2011-05-20 14:13:56 +000058 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000059 * Gets the GL program ID for this program.
60 */
kkinnunendddc18a2014-08-03 23:19:46 -070061 GrGLuint programID() const { return fProgramID; }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000062
63 /**
joshualittee2af952014-12-30 09:04:15 -080064 * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device
65 * space and to make device space positions have the correct origin for processors that require
66 * them.
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000067 */
joshualittee2af952014-12-30 09:04:15 -080068 struct RenderTargetState {
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000069 SkISize fRenderTargetSize;
70 GrSurfaceOrigin fRenderTargetOrigin;
71
joshualittee2af952014-12-30 09:04:15 -080072 RenderTargetState() { this->invalidate(); }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000073 void invalidate() {
bsalomon@google.com45a412e2013-02-13 16:13:13 +000074 fRenderTargetSize.fWidth = -1;
75 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000076 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
77 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000078
79 /**
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040080 * Gets a float4 that adjusts the position from Skia device coords to GL's normalized device
81 * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000082 * applied as such:
83 * pos.x = dot(v.xy, pos.xz)
robertphillipsef4ba3d2015-09-17 11:21:06 -070084 * pos.y = dot(v.zw, pos.yz)
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000085 */
egdaniel018fb622015-10-28 07:26:40 -070086 void getRTAdjustmentVec(float* destVec) {
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000087 destVec[0] = 2.f / fRenderTargetSize.fWidth;
88 destVec[1] = -1.f;
89 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
90 destVec[2] = -2.f / fRenderTargetSize.fHeight;
91 destVec[3] = 1.f;
92 } else {
93 destVec[2] = 2.f / fRenderTargetSize.fHeight;
94 destVec[3] = -1.f;
95 }
96 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000097 };
98
99 /**
egdaniel7dc4bd02015-10-29 07:57:01 -0700100 * This function uploads uniforms, calls each GrGL*Processor's setData, and retrieves the
cdalton42717652015-06-18 11:54:30 -0700101 * textures that need to be bound on each unit. It is the caller's responsibility to ensure
102 * the program is bound before calling, and to bind the outgoing textures to their respective
103 * units upon return. (Each index in the array corresponds to its matching GL texture unit.)
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000104 */
cdalton74b8d322016-04-11 14:47:28 -0700105 void setData(const GrPrimitiveProcessor&, const GrPipeline&);
bsalomon@google.com91961302011-05-09 18:39:58 +0000106
brianosman33f6b3f2016-06-02 05:49:21 -0700107 /**
108 * This function retrieves the textures that need to be used by each GrGL*Processor, and
109 * ensures that any textures requiring mipmaps have their mipmaps correctly built.
110 */
111 void generateMipmaps(const GrPrimitiveProcessor&, const GrPipeline&);
112
Brian Salomon1471df92018-06-08 10:49:00 -0400113private:
cdalton74b8d322016-04-11 14:47:28 -0700114 // A helper to loop over effects, set the transforms (via subclass) and bind textures
Greg Danielbc5d4d72017-05-05 10:28:42 -0400115 void setFragmentData(const GrPrimitiveProcessor&, const GrPipeline&, int* nextTexSamplerIdx,
Brian Salomon559f5562017-11-15 14:28:33 -0500116 int* nextTexelBufferIdx);
joshualitt47bb3822014-10-07 16:43:25 -0700117
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000118 // Helper for setData() that sets the view matrix and loads the render target height uniform
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400119 void setRenderTargetState(const GrPrimitiveProcessor&, const GrRenderTargetProxy*);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000120
cdalton74b8d322016-04-11 14:47:28 -0700121 // Helper for setData() that binds textures and texel buffers to the appropriate texture units
Brian Osman2b23c4b2018-06-01 12:25:08 -0400122 void bindTextures(const GrResourceIOProcessor&, int* nextSamplerIdx, int* nextTexelBufferIdx);
cdalton74b8d322016-04-11 14:47:28 -0700123
brianosman33f6b3f2016-06-02 05:49:21 -0700124 // Helper for generateMipmaps() that ensures mipmaps are up to date
Brian Osman2b23c4b2018-06-01 12:25:08 -0400125 void generateMipmaps(const GrResourceIOProcessor&);
brianosman33f6b3f2016-06-02 05:49:21 -0700126
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000127 // these reflect the current values of uniforms (GL uniform values travel with program)
joshualittee2af952014-12-30 09:04:15 -0800128 RenderTargetState fRenderTargetState;
Brian Salomon1471df92018-06-08 10:49:00 -0400129 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
joshualitt47bb3822014-10-07 16:43:25 -0700130 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000131
joshualitt47bb3822014-10-07 16:43:25 -0700132 // the installed effects
Ben Wagner145dbcd2016-11-03 14:40:50 -0400133 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
134 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400135 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
136 int fFragmentProcessorCnt;
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000137
bsalomon861e1032014-12-16 07:33:49 -0800138 GrGLGpu* fGpu;
joshualitt47bb3822014-10-07 16:43:25 -0700139 GrGLProgramDataManager fProgramDataManager;
junov@google.comf93e7172011-03-31 21:26:24 +0000140
Greg Danielbc5d4d72017-05-05 10:28:42 -0400141 int fNumTextureSamplers;
142 int fNumTexelBuffers;
Greg Danielbc5d4d72017-05-05 10:28:42 -0400143
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000144 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000145};
146
junov@google.comf93e7172011-03-31 21:26:24 +0000147#endif