Skip to content

Commit 5d00619

Browse files
newnonminggo
authored andcommitted
V3 multisampling support (cocos2d#18632)
* Multisampling support * fix opengl initialization with multisampling * fix merge conflict * reverted default attributes
1 parent 5ddf8fb commit 5d00619

File tree

27 files changed

+68
-41
lines changed

27 files changed

+68
-41
lines changed

cocos/platform/CCGLView.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace {
9191
}
9292

9393
//default context attributions are set as follows
94-
GLContextAttrs GLView::_glContextAttrs = {8, 8, 8, 8, 24, 8};
94+
GLContextAttrs GLView::_glContextAttrs = {8, 8, 8, 8, 24, 8, 0};
9595

9696
void GLView::setGLContextAttrs(GLContextAttrs& glContextAttrs)
9797
{

cocos/platform/CCGLView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ struct GLContextAttrs
8282
int alphaBits;
8383
int depthBits;
8484
int stencilBits;
85+
int multisamplingCount;
8586
};
8687

8788
NS_CC_BEGIN

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ private class Cocos2dxEGLConfigChooser implements GLSurfaceView.EGLConfigChooser
347347
private int[] mConfigAttributes;
348348
private final int EGL_OPENGL_ES2_BIT = 0x04;
349349
private final int EGL_OPENGL_ES3_BIT = 0x40;
350-
public Cocos2dxEGLConfigChooser(int redSize, int greenSize, int blueSize, int alphaSize, int depthSize, int stencilSize)
350+
public Cocos2dxEGLConfigChooser(int redSize, int greenSize, int blueSize, int alphaSize, int depthSize, int stencilSize, int multisamplingCount)
351351
{
352-
mConfigAttributes = new int[] {redSize, greenSize, blueSize, alphaSize, depthSize, stencilSize};
352+
mConfigAttributes = new int[] {redSize, greenSize, blueSize, alphaSize, depthSize, stencilSize, multisamplingCount};
353353
}
354354
public Cocos2dxEGLConfigChooser(int[] attributes)
355355
{
@@ -368,17 +368,34 @@ public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display)
368368
EGL10.EGL_ALPHA_SIZE, mConfigAttributes[3],
369369
EGL10.EGL_DEPTH_SIZE, mConfigAttributes[4],
370370
EGL10.EGL_STENCIL_SIZE, mConfigAttributes[5],
371+
EGL10.EGL_SAMPLE_BUFFERS, (mConfigAttributes[6] > 0) ? 1 : 0,
372+
EGL10.EGL_SAMPLES, mConfigAttributes[6],
371373
EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
372374
EGL10.EGL_NONE
373375
},
374376
{
375-
// GL ES 2 with user set
377+
// GL ES 2 with user set 16 bit depth buffer
376378
EGL10.EGL_RED_SIZE, mConfigAttributes[0],
377379
EGL10.EGL_GREEN_SIZE, mConfigAttributes[1],
378380
EGL10.EGL_BLUE_SIZE, mConfigAttributes[2],
379381
EGL10.EGL_ALPHA_SIZE, mConfigAttributes[3],
380382
EGL10.EGL_DEPTH_SIZE, mConfigAttributes[4] >= 24 ? 16 : mConfigAttributes[4],
381383
EGL10.EGL_STENCIL_SIZE, mConfigAttributes[5],
384+
EGL10.EGL_SAMPLE_BUFFERS, (mConfigAttributes[6] > 0) ? 1 : 0,
385+
EGL10.EGL_SAMPLES, mConfigAttributes[6],
386+
EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
387+
EGL10.EGL_NONE
388+
},
389+
{
390+
// GL ES 2 with user set 16 bit depth buffer without multisampling
391+
EGL10.EGL_RED_SIZE, mConfigAttributes[0],
392+
EGL10.EGL_GREEN_SIZE, mConfigAttributes[1],
393+
EGL10.EGL_BLUE_SIZE, mConfigAttributes[2],
394+
EGL10.EGL_ALPHA_SIZE, mConfigAttributes[3],
395+
EGL10.EGL_DEPTH_SIZE, mConfigAttributes[4] >= 24 ? 16 : mConfigAttributes[4],
396+
EGL10.EGL_STENCIL_SIZE, mConfigAttributes[5],
397+
EGL10.EGL_SAMPLE_BUFFERS, 0,
398+
EGL10.EGL_SAMPLES, 0,
382399
EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
383400
EGL10.EGL_NONE
384401
},

cocos/platform/android/javaactivity-android.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ JNIEXPORT jintArray Java_org_cocos2dx_lib_Cocos2dxActivity_getGLContextAttrs(JNI
114114
cocos2d::Application::getInstance()->initGLContextAttrs();
115115
GLContextAttrs _glContextAttrs = GLView::getGLContextAttrs();
116116

117-
int tmp[6] = {_glContextAttrs.redBits, _glContextAttrs.greenBits, _glContextAttrs.blueBits,
118-
_glContextAttrs.alphaBits, _glContextAttrs.depthBits, _glContextAttrs.stencilBits};
117+
int tmp[7] = {_glContextAttrs.redBits, _glContextAttrs.greenBits, _glContextAttrs.blueBits,
118+
_glContextAttrs.alphaBits, _glContextAttrs.depthBits, _glContextAttrs.stencilBits, _glContextAttrs.multisamplingCount};
119119

120120

121-
jintArray glContextAttrsJava = env->NewIntArray(6);
122-
env->SetIntArrayRegion(glContextAttrsJava, 0, 6, tmp);
121+
jintArray glContextAttrsJava = env->NewIntArray(7);
122+
env->SetIntArrayRegion(glContextAttrsJava, 0, 7, tmp);
123123

124124
return glContextAttrsJava;
125125
}

cocos/platform/desktop/CCGLViewImpl-desktop.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float fram
288288
glfwWindowHint(GLFW_ALPHA_BITS,_glContextAttrs.alphaBits);
289289
glfwWindowHint(GLFW_DEPTH_BITS,_glContextAttrs.depthBits);
290290
glfwWindowHint(GLFW_STENCIL_BITS,_glContextAttrs.stencilBits);
291+
292+
glfwWindowHint(GLFW_SAMPLES, _glContextAttrs.multisamplingCount);
291293

292294
int neededWidth = rect.size.width * _frameZoomFactor;
293295
int neededHeight = rect.size.height * _frameZoomFactor;
@@ -360,6 +362,9 @@ bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float fram
360362

361363
// Enable point size by default.
362364
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
365+
366+
if(_glContextAttrs.multisamplingCount > 0)
367+
glEnable(GL_MULTISAMPLE);
363368

364369
// // GLFW v3.2 no longer emits "onGLFWWindowSizeFunCallback" at creation time. Force default viewport:
365370
// setViewPortInPoints(0, 0, neededWidth, neededHeight);

cocos/platform/ios/CCGLViewImpl-ios.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class CC_DLL GLViewImpl : public GLView
5656
static void convertAttrs();
5757
static void* _pixelFormat;
5858
static int _depthFormat;
59+
static int _multisamplingCount;
5960

6061
/** sets the content scale factor */
6162
virtual bool setContentScaleFactor(float contentScaleFactor) override;

cocos/platform/ios/CCGLViewImpl-ios.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ of this software and associated documentation files (the "Software"), to deal
3838

3939
void* GLViewImpl::_pixelFormat = kEAGLColorFormatRGB565;
4040
int GLViewImpl::_depthFormat = GL_DEPTH_COMPONENT16;
41+
int GLViewImpl::_multisamplingCount = 0;
4142

4243
GLViewImpl* GLViewImpl::createWithEAGLView(void *eaglview)
4344
{
@@ -106,6 +107,8 @@ of this software and associated documentation files (the "Software"), to deal
106107
{
107108
CCASSERT(0, "Unsupported format for depth and stencil buffers. Using default");
108109
}
110+
111+
_multisamplingCount = _glContextAttrs.multisamplingCount;
109112
}
110113

111114
GLViewImpl::GLViewImpl()

templates/cpp-template-default/Classes/AppDelegate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ AppDelegate::~AppDelegate()
4040
// it will affect all platforms
4141
void AppDelegate::initGLContextAttrs()
4242
{
43-
// set OpenGL context attributes: red,green,blue,alpha,depth,stencil
44-
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
43+
// set OpenGL context attributes: red,green,blue,alpha,depth,stencil,multisamplesCount
44+
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8, 0};
4545

4646
GLView::setGLContextAttrs(glContextAttrs);
4747
}

templates/cpp-template-default/proj.ios_mac/ios/RootViewController.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ - (void)loadView {
4848
depthFormat: cocos2d::GLViewImpl::_depthFormat
4949
preserveBackbuffer: NO
5050
sharegroup: nil
51-
multiSampling: NO
52-
numberOfSamples: 0 ];
51+
multiSampling: cocos2d::GLViewImpl::_multisamling
52+
numberOfSamples: cocos2d::GLViewImpl::_samples ];
5353

5454
// Enable or disable multiple touches
5555
[eaglView setMultipleTouchEnabled:NO];

templates/js-template-default/frameworks/runtime-src/Classes/AppDelegate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ AppDelegate::~AppDelegate()
7676

7777
void AppDelegate::initGLContextAttrs()
7878
{
79-
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
79+
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8, 0};
8080

8181
GLView::setGLContextAttrs(glContextAttrs);
8282
}

0 commit comments

Comments
 (0)