Skip to content

Commit 4d04b64

Browse files
Mee-guminggo
authored andcommitted
add specific define for metal (#20084)
1 parent f977172 commit 4d04b64

15 files changed

+105
-27
lines changed

cocos/renderer/backend/metal/ProgramMTL.mm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ of this software and associated documentation files (the "Software"), to deal
2727
#include "base/ccMacros.h"
2828

2929
CC_BACKEND_BEGIN
30+
namespace {
31+
const std::string metalSpecificDefine = "#define METAL\n";
32+
}
3033

3134
ProgramMTL::ProgramMTL(const std::string& vertexShader, const std::string& fragmentShader)
3235
: Program(vertexShader, fragmentShader)
3336
{
3437
_vertexShader = static_cast<ShaderModuleMTL*>(ShaderCache::newVertexShaderModule(vertexShader));
35-
_fragmentShader = static_cast<ShaderModuleMTL*>(ShaderCache::newFragmentShaderModule(fragmentShader));
38+
_fragmentShader = static_cast<ShaderModuleMTL*>(ShaderCache::newFragmentShaderModule(std::move(metalSpecificDefine + fragmentShader)));
3639

3740
CC_SAFE_RETAIN(_vertexShader);
3841
CC_SAFE_RETAIN(_fragmentShader);

tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ void ShaderNode::updateUniforms()
202202
_locTime = _programState->getUniformLocation("u_Time");
203203
_locSinTime = _programState->getUniformLocation("u_SinTime");
204204
_locCosTime = _programState->getUniformLocation("u_CosTime");
205+
_locScreenSize = _programState->getUniformLocation("u_screenSize");
206+
207+
const Vec2& frameSize = Director::getInstance()->getOpenGLView()->getFrameSize();
208+
float retinaFactor = Director::getInstance()->getOpenGLView()->getRetinaFactor();
209+
auto screenSizeInPixels = frameSize * retinaFactor;
210+
_programState->setUniform(_locScreenSize, &screenSizeInPixels, sizeof(screenSizeInPixels));
205211
}
206212

207213
/// ShaderMonjori
@@ -598,8 +604,13 @@ bool ShaderRetroEffect::init()
598604
char * fragSource = (char*)fragStr.c_str();
599605

600606
auto p = new backend::ProgramState(positionTextureColor_vert, fragSource);
601-
602607
auto director = Director::getInstance();
608+
const auto& screenSizeLocation = p->getUniformLocation("u_screenSize");
609+
const auto& frameSize = director->getOpenGLView()->getFrameSize();
610+
float retinaFactor = director->getOpenGLView()->getRetinaFactor();
611+
auto screenSizeInPixels = frameSize * retinaFactor;
612+
p->setUniform(screenSizeLocation, &screenSizeInPixels, sizeof(screenSizeInPixels));
613+
603614
auto s = director->getWinSize();
604615

605616
_label = Label::createWithBMFont("fonts/west_england-64.fnt","RETRO EFFECT");

tests/cpp-tests/Classes/ShaderTest/ShaderTest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class ShaderNode : public cocos2d::Node
182182
cocos2d::backend::UniformLocation _locTime;
183183
cocos2d::backend::UniformLocation _locSinTime;
184184
cocos2d::backend::UniformLocation _locCosTime;
185+
cocos2d::backend::UniformLocation _locScreenSize;
185186
};
186187

187188
class ShaderLensFlare : public ShaderTestDemo

tests/cpp-tests/Resources/Shaders/example_Flower.fsh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ precision highp float;
66

77
uniform vec2 center;
88
uniform vec2 resolution;
9-
9+
uniform vec2 u_screenSize;
1010
uniform vec4 u_Time;
1111

1212
//float u( float x ) { return 0.5+0.5*sign(x); }
@@ -16,7 +16,12 @@ float u( float x ) { return (x>0.0)?1.0:0.0; }
1616
void main(void)
1717
{
1818
float time = u_Time[1];
19-
vec2 p = 2.0 * (gl_FragCoord.xy - center.xy) / resolution.xy;
19+
#ifdef METAL
20+
vec2 fragCoord = vec2(gl_FragCoord.x, u_screenSize.y - gl_FragCoord.y);
21+
#else
22+
vec2 fragCoord = gl_FragCoord.xy;
23+
#endif
24+
vec2 p = 2.0 * (fragCoord - center.xy) / resolution.xy;
2025

2126
float a = atan(p.x,p.y);
2227
float r = length(p)*.75;

tests/cpp-tests/Resources/Shaders/example_Heart.fsh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ precision highp float;
66

77
uniform vec2 center;
88
uniform vec2 resolution;
9-
9+
uniform vec2 u_screenSize;
1010
uniform vec4 u_Time;
1111

1212
void main(void)
1313
{
1414
float time = u_Time[1];
15-
vec2 p = 2.0 * (gl_FragCoord.xy - center.xy) / resolution.xy;
15+
#ifdef METAL
16+
vec2 fragCoord = vec2(gl_FragCoord.x, u_screenSize.y - gl_FragCoord.y);
17+
#else
18+
vec2 fragCoord = gl_FragCoord.xy;
19+
#endif
20+
vec2 p = 2.0 * (fragCoord - center.xy) / resolution.xy;
1621

1722
// animate
1823
float tt = mod(time,2.0)/2.0;

tests/cpp-tests/Resources/Shaders/example_HorizontalColor.fsh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ precision lowp float;
66

77
varying vec2 v_texCoord;
88

9+
uniform vec2 u_screenSize;
910
uniform sampler2D u_texture;
1011

1112
void main(void)
1213
{
1314
vec4 optColor;
14-
15-
int y = int( mod(gl_FragCoord.y / 3.0, 10.0 ) );
15+
#ifdef METAL
16+
float fragCoordY = u_screenSize.y - gl_FragCoord.y;
17+
#else
18+
float fragCoordY = gl_FragCoord.y;
19+
#endif
20+
int y = int( mod(fragCoordY / 3.0, 10.0 ) );
1621
if(y == 0) optColor = vec4(1,0,0,1);
1722
else if(y == 1) optColor = vec4(0,1,0,1);
1823
else if(y == 2) optColor = vec4(0,0,1,1);

tests/cpp-tests/Resources/Shaders/example_Julia.fsh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ precision highp float;
66

77
uniform vec2 center;
88
uniform vec2 resolution;
9-
9+
uniform vec2 u_screenSize;
1010
uniform vec4 u_Time;
1111

1212
void main(void)
1313
{
1414
float time = u_Time[1];
15-
vec2 p = 2.0 * (gl_FragCoord.xy - center.xy) / resolution.xy;
15+
#ifdef METAL
16+
vec2 fragCoord = vec2(gl_FragCoord.x, u_screenSize.y - gl_FragCoord.y);
17+
#else
18+
vec2 fragCoord = gl_FragCoord.xy;
19+
#endif
20+
vec2 p = 2.0 * (fragCoord - center.xy) / resolution.xy;
1621
vec2 cc = vec2( cos(.25*time), sin(.25*time*1.423) );
1722

1823
float dmin = 1000.0;

tests/cpp-tests/Resources/Shaders/example_Mandelbrot.fsh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ precision highp float;
66

77
uniform vec2 center;
88
uniform vec2 resolution;
9-
9+
uniform vec2 u_screenSize;
1010
uniform vec4 u_Time;
1111

1212
void main(void)
1313
{
14-
vec2 p = 2.0 * (gl_FragCoord.xy - center.xy) / resolution.xy;
14+
#ifdef METAL
15+
vec2 fragCoord = vec2(gl_FragCoord.x, u_screenSize.y - gl_FragCoord.y);
16+
#else
17+
vec2 fragCoord = gl_FragCoord.xy;
18+
#endif
19+
vec2 p = 2.0 * (fragCoord - center.xy) / resolution.xy;
1520
p.x *= resolution.x/resolution.y;
1621

1722
float zoo = .62+.38*sin(.1*u_Time[1]);

tests/cpp-tests/Resources/Shaders/example_Monjori.fsh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ precision highp float;
77
uniform vec2 center;
88
uniform vec2 resolution;
99
uniform vec4 u_Time;
10+
uniform vec2 u_screenSize;
1011

1112
void main(void)
1213
{
13-
vec2 p = 2.0 * (gl_FragCoord.xy - center.xy) / resolution.xy;
14+
#ifdef METAL
15+
vec2 fragCoord = vec2(gl_FragCoord.x, u_screenSize.y - gl_FragCoord.y);
16+
#else
17+
vec2 fragCoord = gl_FragCoord.xy;
18+
#endif
19+
vec2 p = 2.0 * (fragCoord - center.xy) / resolution.xy;
1420
float a = u_Time[1]*40.0;
1521
float d,e,f,g=1.0/40.0,h,i,r,q;
1622
e=400.0*(p.x*0.5+0.5);

tests/cpp-tests/Resources/Shaders/example_Noisy.fsh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ varying vec2 v_texCoord;
1010
uniform vec2 resolution;
1111
uniform sampler2D u_texture;
1212
uniform vec4 u_Time;
13+
uniform vec2 u_screenSize;
1314

1415
const float intensity = 0.05;
1516
vec3 noise(vec2 uv)
@@ -22,7 +23,12 @@ vec3 noise(vec2 uv)
2223

2324
void main(void)
2425
{
25-
gl_FragColor.xyz = intensity * noise(gl_FragCoord.xy / sin(resolution.xy * u_Time[1] * 0.01)) + (1. - intensity) *
26+
#ifdef METAL
27+
vec2 fragCoord = vec2(gl_FragCoord.x, u_screenSize.y - gl_FragCoord.y);
28+
#else
29+
vec2 fragCoord = gl_FragCoord.xy;
30+
#endif
31+
gl_FragColor.xyz = intensity * noise(fragCoord / sin(resolution.xy * u_Time[1] * 0.01)) + (1. - intensity) *
2632
texture2D(u_texture,v_texCoord.xy).xyz;
2733
gl_FragColor.w = 1.;
2834
}

0 commit comments

Comments
 (0)