iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
TestCustomShaderControl.h
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
13#include "IControl.h"
14#if defined IGRAPHICS_NANOVG
15
21#include "IGraphicsNanoVG.h"
22
23using namespace iplug;
24using namespace igraphics;
25
29{
30public:
31 TestCustomShaderControl(const IRECT& bounds, int paramIdx)
32 : IKnobControlBase(bounds, paramIdx)
33 {
34 SetTooltip("TestCustomShaderControl");
35 }
36
37#ifdef IGRAPHICS_GL
39 {
40 if (mFBO)
41 nvgDeleteFramebuffer(mFBO);
42 }
43
44 void Draw(IGraphics& g) override
45 {
46 NVGcontext* vg = static_cast<NVGcontext*>(g.GetDrawContext());
47 int w = static_cast<int>(mRECT.W() * g.GetDrawScale());
48 int h = static_cast<int>(mRECT.H() * g.GetDrawScale());
49
50 if (invalidateFBO)
51 {
52 if (mFBO)
53 nvgDeleteFramebuffer(mFBO);
54
55 mFBO = nvgCreateFramebuffer(vg, w, h, 0);
56
57 invalidateFBO = false;
58 }
59
60 g.DrawDottedRect(COLOR_BLACK, mRECT);
61 g.FillRect(mMouseIsOver ? COLOR_TRANSLUCENT : COLOR_TRANSPARENT, mRECT);
62
63 nvgEndFrame(vg);
64 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &mInitialFBO);
65
66 nvgBindFramebuffer(mFBO);
67 nvgBeginFrame(vg, static_cast<float>(w), static_cast<float>(h), static_cast<float>(g.GetScreenScale()));
68 GLint vp[4];
69 glGetIntegerv(GL_VIEWPORT, vp);
70
71 glViewport(0, 0, w, h);
72
73 glScissor(0, 0, w, h);
74 glClearColor(0.f, 0.f, 0.f, 0.f);
75 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
76
77 // code from emscripten tests
78
79 auto compileShader = [](GLenum shaderType, const char *src) {
80 GLuint shader = glCreateShader(shaderType);
81 glShaderSource(shader, 1, &src, NULL);
82 glCompileShader(shader);
83
84 GLint isCompiled = 0;
85 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
86 if (!isCompiled)
87 {
88 GLint maxLength = 0;
89 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
90 char *buf = (char*)malloc(maxLength+1);
91 glGetShaderInfoLog(shader, maxLength, &maxLength, buf);
92 printf("%s\n", buf);
93 free(buf);
94 return GLuint(0);
95 }
96
97 return shader;
98 };
99
100 auto createProgram = [](GLuint vertexShader, GLuint fragmentShader) {
101 GLuint program = glCreateProgram();
102 glAttachShader(program, vertexShader);
103 glAttachShader(program, fragmentShader);
104 glBindAttribLocation(program, 0, "apos");
105 glBindAttribLocation(program, 1, "acolor");
106 glLinkProgram(program);
107 return program;
108 };
109
110 static bool sLoggedGLSLVersion = false;
111 if (!sLoggedGLSLVersion) {
112 DBGMSG("Supported GLSL version is %s.\n", (char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
113 sLoggedGLSLVersion = true;
114 }
115
116 #ifdef IGRAPHICS_GL2
117 static const char vs_str[] =
118 "attribute vec4 apos;\n"
119 "attribute vec4 acolor;\n"
120 "varying vec4 color;\n"
121 "void main() {\n"
122 " color = acolor;\n"
123 " gl_Position = apos;\n"
124 "}";
125 #else
126 static const char vs_str[] =
127#ifdef IGRAPHICS_GLES3
128 "#version 300 es\n"
129 "precision highp float;\n"
130 "in vec4 apos;\n"
131 "in vec4 acolor;\n"
132 "out vec4 color;\n"
133#elif defined(IGRAPHICS_GLES2)
134 "precision highp float;\n"
135 "attribute vec4 apos;\n"
136 "attribute vec4 acolor;\n"
137 "varying vec4 color;\n"
138#else
139 "#version 330 core\n"
140 "in vec4 apos;\n"
141 "in vec4 acolor;\n"
142 "out vec4 color;\n"
143#endif
144 "void main() {\n"
145 " color = acolor;\n"
146 " gl_Position = apos;\n"
147 "}";
148 #endif
149 GLuint vs = compileShader(GL_VERTEX_SHADER, vs_str);
150
151 #ifdef IGRAPHICS_GL2
152 static const char fs_str[] =
153 "varying vec4 color;\n"
154 "uniform vec4 color2;\n"
155 "void main() {\n"
156 " gl_FragColor = color;\n"
157 "}";
158 #else
159 static const char fs_str[] =
160#ifdef IGRAPHICS_GLES3
161 "#version 300 es\n"
162 "precision mediump float;\n"
163 "in vec4 color;\n"
164 "out vec4 FragColor;\n"
165 "uniform vec4 color2;\n"
166 "void main() {\n"
167 " FragColor = color;\n"
168 "}\n"
169#elif defined(IGRAPHICS_GLES2)
170 "precision mediump float;\n"
171 "varying vec4 color;\n"
172 "uniform vec4 color2;\n"
173 "void main() {\n"
174 " gl_FragColor = color;\n"
175 "}\n"
176#else
177 "#version 330 core\n"
178 "in vec4 color;\n"
179 "out vec4 FragColor;\n"
180 "uniform vec4 color2;\n"
181 "void main() {\n"
182 " FragColor = color;\n"
183 "}\n"
184#endif
185 ;
186 #endif
187 GLuint fs = compileShader(GL_FRAGMENT_SHADER, fs_str);
188
189 GLuint program = createProgram(vs, fs);
190 glUseProgram(program);
191
192 static const float posAndColor[] = {
193 // x, y, r, g, b
194 -0.6f, -0.6f, 1.0f, 0.0f, 0.0f,
195 0.6f, -0.6f, 0.0f, 1.0f, 0.0f,
196 0.f, 0.6f, 0.0f, 0.0f, 1.0f,
197 };
198
199 #if !defined(IGRAPHICS_GL2) && !defined(IGRAPHICS_GLES2)
200 GLuint vao;
201 glGenVertexArrays(1, &vao);
202 glBindVertexArray(vao);
203 #endif
204
205 GLuint vbo;
206 glGenBuffers(1, &vbo);
207 glBindBuffer(GL_ARRAY_BUFFER, vbo);
208 glBufferData(GL_ARRAY_BUFFER, sizeof(posAndColor), posAndColor, GL_STATIC_DRAW);
209
210 #ifdef IGRAPHICS_GL2
211 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);
212 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
213 glEnableVertexAttribArray(0);
214 glEnableVertexAttribArray(1);
215 #else
216 GLint posAttrib = glGetAttribLocation(program, "apos");
217 GLint colorAttrib = glGetAttribLocation(program, "acolor");
218 glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);
219 glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
220 glEnableVertexAttribArray(posAttrib);
221 glEnableVertexAttribArray(colorAttrib);
222 #endif
223
224 glDrawArrays(GL_TRIANGLES, 0, 3);
225
226 glViewport(vp[0], vp[1], vp[2], vp[3]);
227
228 nvgEndFrame(vg);
229 glBindFramebuffer(GL_FRAMEBUFFER, mInitialFBO);
230 nvgBeginFrame(vg, static_cast<float>(g.WindowWidth()),
231 static_cast<float>(g.WindowHeight()),
232 static_cast<float>(g.GetScreenScale()));
233
234 APIBitmap apibmp {mFBO->image, w, h, 1, 1.};
235 IBitmap bmp {&apibmp, 1, false};
236
237 g.DrawFittedBitmap(bmp, mRECT);
238 }
239
240 void OnResize() override
241 {
242 invalidateFBO = true;
243 }
244
245 void OnRescale() override
246 {
247 invalidateFBO = true;
248 }
249#elif defined IGRAPHICS_METAL
251
252 void CleanUp();
253
254 void Draw(IGraphics& g) override;
255#endif
256
257private:
258 NVGframebuffer* mFBO = nullptr;
259
260#ifdef IGRAPHICS_METAL
261 void* mRenderPassDescriptor = nullptr;
262 void* mRenderPipeline = nullptr;
263#else
264 int mInitialFBO = 0;
265#endif
266 bool invalidateFBO = true;
267};
268
269#else
272{
273public:
274 TestCustomShaderControl(const IRECT& bounds, int paramIdx)
275 : IControl(bounds)
276 {
277 SetTooltip("TestCustomShaderControl");
278 }
279
280 void Draw(IGraphics& g) override
281 {
282 g.DrawText(mText, "UNSUPPORTED", mRECT);
283 }
284};
285#endif
This file contains the base IControl implementation, along with some base classes for specific types ...
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
User-facing bitmap abstraction that you use to manage bitmap data, independant of draw class/platform...
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
virtual void OnResize()
Called when IControl is constructed or resized using SetRect().
Definition: IControl.h:153
bool mMouseIsOver
if mGraphics::mHandleMouseOver = true, this will be true when the mouse is over control.
Definition: IControl.h:565
virtual void OnRescale()
Implement to do something when graphics is scaled globally (e.g.
Definition: IControl.h:150
IControl * SetTooltip(const char *str)
Set a tooltip for the control.
Definition: IControl.h:221
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
virtual void DrawFittedBitmap(const IBitmap &bitmap, const IRECT &bounds, const IBlend *pBlend=0)
Draw a bitmap (raster) image to the graphics context, scaling the image to fit the bounds.
Definition: IGraphics.cpp:2792
void DrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend=0)
Draw some text to the graphics context in a specific rectangle.
Definition: IGraphics.cpp:683
virtual void * GetDrawContext()=0
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
virtual void DrawDottedRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f, float dashLen=2.f)
Draw a dotted rectangle to the graphics context.
Definition: IGraphics.cpp:2557
int WindowWidth() const
Gets the width of the graphics context including draw scaling.
Definition: IGraphics.h:1106
virtual void FillRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill a rectangular region of the graphics context with a color.
Definition: IGraphics.cpp:2587
float GetScreenScale() const
Gets the screen/display scaling factor, e.g.
Definition: IGraphics.h:1122
float GetDrawScale() const
Gets the graphics context scaling factor.
Definition: IGraphics.h:1118
int WindowHeight() const
Gets the height of the graphics context including draw scaling.
Definition: IGraphics.h:1110
A base class for knob/dial controls, to handle mouse action and Sender.
Definition: IControl.h:1373
Control to test IGraphicsNanoVG with Metal Shaders.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
Used to manage a rectangular area, independent of draw class/platform.
float W() const
float H() const