Skip to content

Commit ec55d20

Browse files
committed
parse smooth(level) in the pre-processor and construct sketchQuality
method
1 parent eda88f3 commit ec55d20

File tree

5 files changed

+135
-20
lines changed

5 files changed

+135
-20
lines changed

core/src/processing/core/PApplet.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,8 +818,9 @@ public SketchSurfaceViewGL(Context context, int wide, int high, boolean is3D) {
818818
// Tells the default EGLContextFactory and EGLConfigChooser to create an GLES2 context.
819819
setEGLContextClientVersion(2);
820820

821-
if (PGLES.ENABLE_MULTISAMPLING) {
822-
setEGLConfigChooser(((PGLES)g3.pgl).getConfigChooser());
821+
int quality = sketchQuality();
822+
if (1 < quality) {
823+
setEGLConfigChooser(((PGLES)g3.pgl).getConfigChooser(quality));
823824
}
824825

825826
// The renderer can be set only once.

core/src/processing/opengl/PGLES.java

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,18 @@ public class PGLES extends PGL {
7171
MAX_CAPS_JOINS_LENGTH = 1000;
7272
}
7373

74-
public static final boolean ENABLE_MULTISAMPLING = false;
75-
7674
// Some EGL constants needed to initialize a GLES2 context.
7775
protected static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
7876
protected static final int EGL_OPENGL_ES2_BIT = 0x0004;
7977

8078
// Coverage multisampling identifiers for nVidia Tegra2
8179
protected static final int EGL_COVERAGE_BUFFERS_NV = 0x30E0;
8280
protected static final int EGL_COVERAGE_SAMPLES_NV = 0x30E1;
81+
protected static final int GL_COVERAGE_BUFFER_BIT_NV = 0x8000;
82+
83+
protected static boolean usingMultisampling = false;
84+
protected static boolean usingCoverageMultisampling = false;
85+
protected static int multisampleCount = 1;
8386

8487
///////////////////////////////////////////////////////////
8588

@@ -174,15 +177,15 @@ public AndroidContextFactory getContextFactory() {
174177
}
175178

176179

177-
public AndroidConfigChooser getConfigChooser() {
178-
configChooser = new AndroidConfigChooser(5, 6, 5, 4, 16, 1);
180+
public AndroidConfigChooser getConfigChooser(int samples) {
181+
configChooser = new AndroidConfigChooser(5, 6, 5, 4, 16, 1, samples);
179182
return configChooser;
180183
}
181184

182185

183186
public AndroidConfigChooser getConfigChooser(int r, int g, int b, int a,
184-
int d, int s) {
185-
configChooser = new AndroidConfigChooser(r, g, b, a, d, s);
187+
int d, int s, int samples) {
188+
configChooser = new AndroidConfigChooser(r, g, b, a, d, s, samples);
186189
return configChooser;
187190
}
188191

@@ -259,6 +262,26 @@ protected class AndroidConfigChooser implements EGLConfigChooser {
259262
public int stencilBits;
260263
public int[] tempValue = new int[1];
261264

265+
public int numSamples;
266+
267+
/*
268+
The GLES2 extensions supported are:
269+
GL_OES_rgb8_rgba8 GL_OES_depth24 GL_OES_vertex_half_float
270+
GL_OES_texture_float GL_OES_texture_half_float
271+
GL_OES_element_index_uint GL_OES_mapbuffer
272+
GL_OES_fragment_precision_high GL_OES_compressed_ETC1_RGB8_texture
273+
GL_OES_EGL_image GL_OES_required_internalformat GL_OES_depth_texture
274+
GL_OES_get_program_binary GL_OES_packed_depth_stencil
275+
GL_OES_standard_derivatives GL_OES_vertex_array_object GL_OES_egl_sync
276+
GL_EXT_multi_draw_arrays GL_EXT_texture_format_BGRA8888
277+
GL_EXT_discard_framebuffer GL_EXT_shader_texture_lod
278+
GL_IMG_shader_binary GL_IMG_texture_compression_pvrtc
279+
GL_IMG_texture_stream2 GL_IMG_texture_npot
280+
GL_IMG_texture_format_BGRA8888 GL_IMG_read_format
281+
GL_IMG_program_binary GL_IMG_multisampled_render_to_texture
282+
*/
283+
284+
/*
262285
// The attributes we want in the frame buffer configuration for Processing.
263286
// For more details on other attributes, see:
264287
// http://www.khronos.org/opengles/documentation/opengles1_0/html/eglChooseConfig.html
@@ -307,23 +330,57 @@ protected class AndroidConfigChooser implements EGLConfigChooser {
307330
EGL10.EGL_SAMPLE_BUFFERS, 1,
308331
EGL10.EGL_SAMPLES, 2,
309332
EGL10.EGL_NONE };
333+
*/
334+
335+
protected int[] attribsNoMSAA = {
336+
EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
337+
EGL10.EGL_SAMPLE_BUFFERS, 0,
338+
EGL10.EGL_NONE };
310339

311-
public AndroidConfigChooser(int r, int g, int b, int a, int d, int s) {
312-
redTarget = r;
313-
greenTarget = g;
314-
blueTarget = b;
315-
alphaTarget = a;
316-
depthTarget = d;
317-
stencilTarget = s;
340+
public AndroidConfigChooser(int rbits, int gbits, int bbits, int abits,
341+
int dbits, int sbits, int samples) {
342+
redTarget = rbits;
343+
greenTarget = gbits;
344+
blueTarget = bbits;
345+
alphaTarget = abits;
346+
depthTarget = dbits;
347+
stencilTarget = sbits;
348+
numSamples = samples;
318349
}
319350

320351
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
321-
EGLConfig[] configs = chooseConfigWithAttribs(egl, display, configAttribsGL_MSAA);
322-
if (configs == null) {
323-
chooseConfigWithAttribs(egl, display, configAttribsGL_CovMSAA);
352+
EGLConfig[] configs = null;
353+
if (1 < numSamples) {
354+
int[] attribs = new int[] {
355+
EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
356+
EGL10.EGL_SAMPLE_BUFFERS, 1,
357+
EGL10.EGL_SAMPLES, numSamples,
358+
EGL10.EGL_NONE };
359+
configs = chooseConfigWithAttribs(egl, display, attribs);
324360
if (configs == null) {
325-
chooseConfigWithAttribs(egl, display, configAttribsGL_NoMSAA);
361+
// No normal multisampling config was found. Try to create a
362+
// coverage multisampling configuration, for the nVidia Tegra2.
363+
// See the EGL_NV_coverage_sample documentation.
364+
int[] attribsCov = {
365+
EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
366+
EGL_COVERAGE_BUFFERS_NV, 1,
367+
EGL_COVERAGE_SAMPLES_NV, numSamples,
368+
EGL10.EGL_NONE };
369+
configs = chooseConfigWithAttribs(egl, display, attribsCov);
370+
if (configs == null) {
371+
configs = chooseConfigWithAttribs(egl, display, attribsNoMSAA);
372+
} else {
373+
usingMultisampling = true;
374+
usingCoverageMultisampling = true;
375+
multisampleCount = numSamples;
376+
}
377+
} else {
378+
usingMultisampling = true;
379+
usingCoverageMultisampling = false;
380+
multisampleCount = numSamples;
326381
}
382+
} else {
383+
configs = chooseConfigWithAttribs(egl, display, attribsNoMSAA);
327384
}
328385

329386
if (configs == null) {
@@ -1619,6 +1676,9 @@ public void stencilMaskSeparate(int face, int mask) {
16191676

16201677
@Override
16211678
public void clear(int buf) {
1679+
if (usingMultisampling && usingCoverageMultisampling) {
1680+
buf |= GL_COVERAGE_BUFFER_BIT_NV;
1681+
}
16221682
GLES20.glClear(buf);
16231683
}
16241684

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3376,7 +3376,7 @@ public void smooth() {
33763376

33773377
@Override
33783378
public void smooth(int level) {
3379-
if (smoothDisabled) return;
3379+
if (smoothDisabled || PGL.MAX_SAMPLES == -1) return;
33803380

33813381
smooth = true;
33823382

src/processing/mode/android/AndroidBuild.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public File createProject() throws IOException, SketchException {
140140
// }
141141
// On Android, this init will throw a SketchException if there's a problem with size()
142142
preproc.initSketchSize(sketch.getMainProgram());
143+
preproc.initSketchSmooth(sketch.getMainProgram());
143144
sketchClassName = preprocess(srcFolder, manifest.getPackageName(), preproc, false);
144145
if (sketchClassName != null) {
145146
File tempManifest = new File(tmpFolder, "AndroidManifest.xml");

src/processing/mode/android/AndroidPreprocessor.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.List;
2828

2929
import processing.app.*;
30+
import processing.core.PApplet;
3031
import processing.mode.java.preproc.PdePreprocessor;
3132
import processing.mode.java.preproc.PreprocessorResult;
3233
import antlr.RecognitionException;
@@ -36,7 +37,13 @@
3637
public class AndroidPreprocessor extends PdePreprocessor {
3738
Sketch sketch;
3839
String packageName;
40+
41+
protected String smoothStatement;
42+
protected String sketchQuality;
3943

44+
45+
public static final String SMOOTH_REGEX =
46+
"(?:^|\\s|;)smooth\\s*\\(\\s*([^\\s,]+)\\s*\\)\\s*\\;";
4047

4148
public AndroidPreprocessor(final Sketch sketch,
4249
final String packageName) throws IOException {
@@ -59,6 +66,48 @@ public String[] initSketchSize(String code) throws SketchException {
5966
sketchRenderer = info[3];
6067
return info;
6168
}
69+
70+
71+
public String[] initSketchSmooth(String code) throws SketchException {
72+
String[] info = parseSketchSmooth(code, true);
73+
if (info == null) {
74+
System.err.println("More about the size() command on Android can be");
75+
System.err.println("found here: http://wiki.processing.org/w/Android");
76+
throw new SketchException("Could not parse the size() command.");
77+
}
78+
smoothStatement = info[0];
79+
sketchQuality = info[1];
80+
return info;
81+
}
82+
83+
84+
static public String[] parseSketchSmooth(String code, boolean fussy) {
85+
String[] matches = PApplet.match(scrubComments(code), SMOOTH_REGEX);
86+
87+
if (matches != null) {
88+
boolean badSmooth = false;
89+
90+
if (PApplet.parseInt(matches[1], -1) == -1) {
91+
badSmooth = true;
92+
}
93+
94+
if (badSmooth && fussy) {
95+
// found a reference to smooth, but it didn't seem to contain numbers
96+
final String message =
97+
"The smooth level of this applet could not automatically\n" +
98+
"be determined from your code. Use only a numeric\n" +
99+
"value (not variables) for the smooth() command.\n" +
100+
"See the smooth() reference for an explanation.";
101+
Base.showWarning("Could not find smooth level", message, null);
102+
// new Exception().printStackTrace(System.out);
103+
return null;
104+
}
105+
106+
return matches;
107+
}
108+
return new String[] { null, null }; // not an error, just empty
109+
}
110+
62111

63112
/*
64113
protected boolean parseSketchSize() {
@@ -172,6 +221,10 @@ protected void writeFooter(PrintWriter out, String className) {
172221
out.println(indent + "public String sketchRenderer() { return " + sketchRenderer + "; }");
173222
}
174223

224+
if (sketchQuality != null) {
225+
out.println(indent + "public int sketchQuality() { return " + sketchQuality + "; }");
226+
}
227+
175228
// close off the class definition
176229
out.println("}");
177230
}

0 commit comments

Comments
 (0)