mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
* Fix #10538: Android+WebGL PS struct variables has mediump precision * update compiled shaders * review modification * update compiled shaders
This commit is contained in:
@@ -448,7 +448,7 @@ export const effects = [
|
||||
"shaders": [
|
||||
{
|
||||
"name": "standard|standard-vs|standard-fs",
|
||||
"hash": 1626719900,
|
||||
"hash": 1747308226,
|
||||
"builtins": {
|
||||
"statistics": { "CC_EFFECT_USED_VERTEX_UNIFORM_VECTORS": 221, "CC_EFFECT_USED_FRAGMENT_UNIFORM_VECTORS": 64 },
|
||||
"globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }, { "name": "CCCamera", "defines": [] }, { "name": "CCShadow", "defines": [] }], "samplerTextures": [{ "name": "cc_shadowMap", "defines": ["CC_RECEIVE_SHADOW"] }, { "name": "cc_spotLightingMap", "defines": ["CC_RECEIVE_SHADOW"] }, { "name": "cc_environment", "defines": ["CC_USE_IBL"] }, { "name": "cc_diffuseMap", "defines": ["CC_USE_IBL", "CC_USE_DIFFUSEMAP"] }], "buffers": [], "images": [] },
|
||||
@@ -611,7 +611,7 @@ export const effects = [
|
||||
"shaders": [
|
||||
{
|
||||
"name": "terrain|terrain-vs|terrain-fs",
|
||||
"hash": 2255044546,
|
||||
"hash": 3818018672,
|
||||
"builtins": {
|
||||
"statistics": { "CC_EFFECT_USED_VERTEX_UNIFORM_VECTORS": 69, "CC_EFFECT_USED_FRAGMENT_UNIFORM_VECTORS": 60 },
|
||||
"globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }, { "name": "CCCamera", "defines": [] }, { "name": "CCShadow", "defines": [] }], "samplerTextures": [{ "name": "cc_shadowMap", "defines": ["CC_RECEIVE_SHADOW"] }, { "name": "cc_spotLightingMap", "defines": ["CC_RECEIVE_SHADOW"] }, { "name": "cc_environment", "defines": ["CC_USE_IBL"] }, { "name": "cc_diffuseMap", "defines": ["CC_USE_IBL", "CC_USE_DIFFUSEMAP"] }], "buffers": [], "images": [] },
|
||||
@@ -956,7 +956,7 @@ export const effects = [
|
||||
"shaders": [
|
||||
{
|
||||
"name": "deferred-lighting|lighting-vs|lighting-fs",
|
||||
"hash": 1506731478,
|
||||
"hash": 1741755773,
|
||||
"builtins": {
|
||||
"statistics": { "CC_EFFECT_USED_VERTEX_UNIFORM_VECTORS": 39, "CC_EFFECT_USED_FRAGMENT_UNIFORM_VECTORS": 58 },
|
||||
"globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }, { "name": "CCCamera", "defines": [] }, { "name": "CCShadow", "defines": [] }], "samplerTextures": [{ "name": "cc_shadowMap", "defines": ["CC_RECEIVE_SHADOW"] }, { "name": "cc_spotLightingMap", "defines": ["CC_RECEIVE_SHADOW"] }, { "name": "cc_environment", "defines": ["CC_USE_IBL"] }, { "name": "cc_diffuseMap", "defines": ["CC_USE_IBL", "CC_USE_DIFFUSEMAP"] }], "buffers": [], "images": [] },
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -29,6 +29,7 @@
|
||||
*/
|
||||
|
||||
import { ccclass, displayOrder, serializable, type } from 'cc.decorator';
|
||||
import { systemInfo } from 'pal/system-info';
|
||||
import { sceneCulling, validPunctualLightsCulling } from './scene-culling';
|
||||
import { Asset } from '../assets/asset';
|
||||
import { AccessType, Attribute, Buffer, BufferInfo, BufferUsageBit, ClearFlagBit, ClearFlags, ColorAttachment, CommandBuffer,
|
||||
@@ -47,6 +48,8 @@ import { PipelineUBO } from './pipeline-ubo';
|
||||
import { RenderFlow } from './render-flow';
|
||||
import { IPipelineEvent, PipelineEventProcessor, PipelineEventType } from './pipeline-event';
|
||||
import { decideProfilerCamera } from './pipeline-funcs';
|
||||
import { OS } from '../../../pal/system-info/enum-type';
|
||||
import { macro } from '../platform/macro';
|
||||
|
||||
/**
|
||||
* @en Render pipeline information descriptor
|
||||
@@ -639,6 +642,8 @@ export abstract class RenderPipeline extends Asset implements IPipelineEvent {
|
||||
str += `#define CC_DEVICE_MAX_VERTEX_UNIFORM_VECTORS ${this.device.capabilities.maxVertexUniformVectors}\n`;
|
||||
str += `#define CC_DEVICE_MAX_FRAGMENT_UNIFORM_VECTORS ${this.device.capabilities.maxFragmentUniformVectors}\n`;
|
||||
str += `#define CC_DEVICE_CAN_BENEFIT_FROM_INPUT_ATTACHMENT ${this.device.hasFeature(Feature.INPUT_ATTACHMENT_BENEFIT) ? 1 : 0}\n`;
|
||||
str += `#define CC_PLATFORM_ANDROID_AND_WEBGL ${systemInfo.os === OS.ANDROID && systemInfo.isBrowser ? 1 : 0}\n`;
|
||||
str += `#define CC_ENABLE_WEBGL_HIGHP_STRUCT_VALUES ${macro.ENABLE_WEBGL_HIGHP_STRUCT_VALUES ? 1 : 0}\n`;
|
||||
this._constantMacros = str;
|
||||
}
|
||||
|
||||
|
||||
@@ -1053,6 +1053,22 @@ const macro = {
|
||||
* @default 20
|
||||
*/
|
||||
MAX_LABEL_CANVAS_POOL_SIZE: 20,
|
||||
|
||||
/**
|
||||
* @en
|
||||
* Boolean that indicates if enable highp precision data in structure with fragment shader.
|
||||
* Enable this option will make the variables defined by the HIGHP_VALUE_STRUCT_DEFINE macro in the shader more accurate, such as position.
|
||||
* Enable this option can avoid some distorted lighting effects. That depends on whether your game has abnormal lighting effects on this platform.
|
||||
* There will be a slight performance loss if enable this option, but the impact is not significant.
|
||||
* Only affect WebGL backend
|
||||
* @zh
|
||||
* 用于设置是否在片元着色器中使用结构体的时候,允许其中的数据使用highp精度
|
||||
* 将这个选项设置为 true 会让shader中使用HIGHP_VALUE_STRUCT_DEFINE宏定义的变量精度更高,比如位置信息等,避免出现一些失真的光照效果。是否开启这个选项很大程度上取决于你的游戏在此平台上是否出现了异常的表现。
|
||||
* 开启后会有轻微的性能损失,但影响不大。
|
||||
* 仅影响 WebGL 后端
|
||||
* @default false
|
||||
*/
|
||||
ENABLE_WEBGL_HIGHP_STRUCT_VALUES: false,
|
||||
};
|
||||
|
||||
legacyCC.macro = macro;
|
||||
|
||||
@@ -22,6 +22,9 @@
|
||||
#pragma define CC_EFFECT_USED_VERTEX_UNIFORM_VECTORS default(128)
|
||||
#pragma define CC_EFFECT_USED_FRAGMENT_UNIFORM_VECTORS default(128)
|
||||
#pragma define CC_DEVICE_CAN_BENEFIT_FROM_INPUT_ATTACHMENT default(0)
|
||||
#pragma define CC_PLATFORM_ANDROID_AND_WEBGL default(0)
|
||||
#pragma define CC_ENABLE_WEBGL_HIGHP_STRUCT_VALUES default(0)
|
||||
|
||||
// use cluster shading or not
|
||||
#pragma define CC_ENABLE_CLUSTERED_LIGHT_CULLING default(0)
|
||||
// When sampling RTs, Y-flip is needed if the screen space sign Y is positive (pointing upwards)
|
||||
|
||||
@@ -9,3 +9,72 @@ vec4 packDepthToRGBA (float depth) {
|
||||
|
||||
#define unpackRGBAToDepth(color)
|
||||
dot(color, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 16581375.0))
|
||||
|
||||
|
||||
// highp <--> 2 * mediump
|
||||
#define DEFINE_PACK_HIGHP_FUNC(type)
|
||||
highp type unpackHighpData (type mainPart, type modPart) {\
|
||||
highp type data = mainPart;\
|
||||
return data + modPart;\
|
||||
}\
|
||||
void packHighpData (out type mainPart, out type modPart, highp type data) {\
|
||||
mainPart = fract(data);\
|
||||
modPart = data - mainPart;\
|
||||
}\
|
||||
highp type unpackHighpData (type mainPart, type modPart, const float modValue) {\
|
||||
highp type data = mainPart * modValue;\
|
||||
return data + modPart * modValue;\
|
||||
}\
|
||||
void packHighpData (out type mainPart, out type modPart, highp type data, const float modValue) {\
|
||||
highp type divide = data / modValue;\
|
||||
mainPart = floor(divide);\
|
||||
modPart = (data - mainPart * modValue) / modValue;\
|
||||
}
|
||||
|
||||
DEFINE_PACK_HIGHP_FUNC(float)
|
||||
DEFINE_PACK_HIGHP_FUNC(vec2)
|
||||
DEFINE_PACK_HIGHP_FUNC(vec3)
|
||||
DEFINE_PACK_HIGHP_FUNC(vec4)
|
||||
|
||||
// Use these macros to define and assign high precision values in fs structs, otherwise will loss precisions with android + webgl platform
|
||||
#define HIGHP_VALUE_STRUCT_DEFINE(type, name) \
|
||||
#if CC_PLATFORM_ANDROID_AND_WEBGL && CC_ENABLE_WEBGL_HIGHP_STRUCT_VALUES\
|
||||
type name, name##_fract_part;\
|
||||
#else\
|
||||
type name;\
|
||||
#endif\
|
||||
#pragma // empty pragma trick to get rid of trailing semicolons at effect compile time
|
||||
|
||||
// used for large range such as position
|
||||
#define HIGHP_VALUE_FROM_STRUCT_DEFINED(value, defined) \
|
||||
#if CC_PLATFORM_ANDROID_AND_WEBGL && CC_ENABLE_WEBGL_HIGHP_STRUCT_VALUES\
|
||||
value = unpackHighpData(defined, defined##_fract_part);\
|
||||
#else\
|
||||
value = defined;\
|
||||
#endif\
|
||||
#pragma // empty pragma trick to get rid of trailing semicolons at effect compile time
|
||||
|
||||
#define HIGHP_VALUE_TO_STRUCT_DEFINED(value, defined) \
|
||||
#if CC_PLATFORM_ANDROID_AND_WEBGL && CC_ENABLE_WEBGL_HIGHP_STRUCT_VALUES\
|
||||
packHighpData(defined, defined##_fract_part, value);\
|
||||
#else\
|
||||
defined = value;\
|
||||
#endif\
|
||||
#pragma // empty pragma trick to get rid of trailing semicolons at effect compile time
|
||||
|
||||
// used for small range such as -1-1 normalized
|
||||
#define HIGHP_VALUE_FROM_STRUCT_DEFINED_SMALL_RANGE(value, defined) \
|
||||
#if CC_PLATFORM_ANDROID_AND_WEBGL && CC_ENABLE_WEBGL_HIGHP_STRUCT_VALUES\
|
||||
value = unpackHighpData(defined, defined##_fract_part, 1000.0);\
|
||||
#else\
|
||||
value = defined;\
|
||||
#endif\
|
||||
#pragma // empty pragma trick to get rid of trailing semicolons at effect compile time
|
||||
|
||||
#define HIGHP_VALUE_TO_STRUCT_DEFINED_SMALL_RANGE(value, defined) \
|
||||
#if CC_PLATFORM_ANDROID_AND_WEBGL && CC_ENABLE_WEBGL_HIGHP_STRUCT_VALUES\
|
||||
packHighpData(defined, defined##_fract_part, value, 1000.0);\
|
||||
#else\
|
||||
defined = value;\
|
||||
#endif\
|
||||
#pragma // empty pragma trick to get rid of trailing semicolons at effect compile time
|
||||
|
||||
@@ -89,13 +89,16 @@ vec4 CCClusterShadingAdditive (StandardSurface s, vec4 shadowPos) {
|
||||
vec3 specular = mix(vec3(0.04), s.albedo.rgb, s.metallic);
|
||||
vec3 diffuseContrib = diffuse / PI;
|
||||
|
||||
vec3 position;
|
||||
HIGHP_VALUE_FROM_STRUCT_DEFINED(position, s.position);
|
||||
|
||||
vec3 N = normalize(s.normal);
|
||||
vec3 V = normalize(cc_cameraPos.xyz - s.position);
|
||||
vec3 V = normalize(cc_cameraPos.xyz - position);
|
||||
float NV = max(abs(dot(N, V)), 0.001);
|
||||
specular = BRDFApprox(specular, s.roughness, NV);
|
||||
vec3 finalColor = vec3(0.0);
|
||||
|
||||
uint cluster = getClusterIndex(gl_FragCoord, vec4(s.position, 1.0));
|
||||
uint cluster = getClusterIndex(gl_FragCoord, vec4(position, 1.0));
|
||||
LightGrid grid = getLightGrid(cluster);
|
||||
uint numLights = grid.ccLights;
|
||||
|
||||
@@ -103,7 +106,7 @@ vec4 CCClusterShadingAdditive (StandardSurface s, vec4 shadowPos) {
|
||||
if (i >= numLights) break;
|
||||
uint lightIndex = getGridLightIndex(grid.offset, i);
|
||||
CCLight light = getCCLight(lightIndex);
|
||||
vec3 SLU = light.cc_lightPos.xyz - s.position;
|
||||
vec3 SLU = light.cc_lightPos.xyz - position;
|
||||
vec3 SL = normalize(SLU);
|
||||
vec3 SH = normalize(SL + V);
|
||||
float SNL = max(dot(N, SL), 0.001);
|
||||
@@ -131,7 +134,7 @@ vec4 CCClusterShadingAdditive (StandardSurface s, vec4 shadowPos) {
|
||||
float shadow = 1.0;
|
||||
#if CC_RECEIVE_SHADOW
|
||||
if (light.cc_lightPos.w > 0.0) {
|
||||
CC_SPOT_SHADOW_FACTOR_BASE(shadow, shadowPos, s.position, light.cc_lightPos, N);
|
||||
CC_SPOT_SHADOW_FACTOR_BASE(shadow, shadowPos, position, light.cc_lightPos, N);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -4,13 +4,15 @@
|
||||
|
||||
#if CC_ENABLE_CLUSTERED_LIGHT_CULLING == 0
|
||||
vec4 CCStandardShadingAdditive (StandardSurface s, vec4 shadowPos) {
|
||||
vec3 position;
|
||||
HIGHP_VALUE_FROM_STRUCT_DEFINED(position, s.position);
|
||||
// Calculate diffuse & specular
|
||||
vec3 diffuse = s.albedo.rgb * (1.0 - s.metallic);
|
||||
vec3 specular = mix(vec3(0.04), s.albedo.rgb, s.metallic);
|
||||
vec3 diffuseContrib = diffuse / PI;
|
||||
|
||||
vec3 N = normalize(s.normal);
|
||||
vec3 V = normalize(cc_cameraPos.xyz - s.position);
|
||||
vec3 V = normalize(cc_cameraPos.xyz - position);
|
||||
float NV = max(abs(dot(N, V)), 0.0);
|
||||
specular = BRDFApprox(specular, s.roughness, NV);
|
||||
vec3 finalColor = vec3(0.0);
|
||||
@@ -19,7 +21,7 @@ vec4 CCStandardShadingAdditive (StandardSurface s, vec4 shadowPos) {
|
||||
|
||||
for (int i = 0; i < LIGHTS_PER_PASS; i++) {
|
||||
if (i >= numLights) break;
|
||||
vec3 SLU = cc_lightPos[i].xyz - s.position;
|
||||
vec3 SLU = cc_lightPos[i].xyz - position;
|
||||
vec3 SL = normalize(SLU);
|
||||
vec3 SH = normalize(SL + V);
|
||||
float SNL = max(dot(N, SL), 0.0);
|
||||
@@ -47,7 +49,7 @@ vec4 CCStandardShadingAdditive (StandardSurface s, vec4 shadowPos) {
|
||||
float shadow = 1.0;
|
||||
#if CC_RECEIVE_SHADOW
|
||||
if (cc_lightPos[i].w > 0.0 && cc_lightSizeRangeAngle[i].w > 0.0) {
|
||||
CC_SPOT_SHADOW_FACTOR_BASE(shadow, shadowPos, s.position, cc_lightPos[i], N);
|
||||
CC_SPOT_SHADOW_FACTOR_BASE(shadow, shadowPos, position, cc_lightPos[i], N);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ struct StandardSurface {
|
||||
// albedo
|
||||
vec4 albedo;
|
||||
// these two need to be in the same coordinate system
|
||||
vec3 position;
|
||||
HIGHP_VALUE_STRUCT_DEFINE(vec3, position);
|
||||
vec3 normal;
|
||||
// emissive
|
||||
vec3 emissive;
|
||||
@@ -89,8 +89,11 @@ vec4 CCStandardShadingBase (StandardSurface s, vec4 shadowPos) {
|
||||
|
||||
vec3 specular = mix(vec3(0.04), s.albedo.rgb, s.metallic);
|
||||
|
||||
vec3 position;
|
||||
HIGHP_VALUE_FROM_STRUCT_DEFINED(position, s.position);
|
||||
|
||||
vec3 N = normalize(s.normal);
|
||||
vec3 V = normalize(cc_cameraPos.xyz - s.position);
|
||||
vec3 V = normalize(cc_cameraPos.xyz - position);
|
||||
|
||||
float NV = max(abs(dot(N, V)), 0.0);
|
||||
specular = BRDFApprox(specular, s.roughness, NV);
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
|
||||
#include <cc-global>
|
||||
#include <common>
|
||||
#include <packing>
|
||||
|
||||
struct ToonSurface {
|
||||
vec4 baseColor;
|
||||
vec4 specular;
|
||||
// these two need to be in the same coordinate system
|
||||
vec3 position;
|
||||
HIGHP_VALUE_STRUCT_DEFINE(vec3, position);
|
||||
vec3 normal;
|
||||
// shading params
|
||||
vec3 shade1;
|
||||
@@ -23,13 +24,16 @@ struct ToonSurface {
|
||||
#if CC_FORWARD_ADD
|
||||
#include <lighting>
|
||||
vec4 CCToonShading (ToonSurface s) {
|
||||
vec3 V = normalize(cc_cameraPos.xyz - s.position);
|
||||
vec3 position;
|
||||
HIGHP_VALUE_FROM_STRUCT_DEFINED(position, s.position);
|
||||
|
||||
vec3 V = normalize(cc_cameraPos.xyz - position);
|
||||
vec3 N = normalize(s.normal);
|
||||
float specularWeight = 1.0 - pow(s.specular.a, 5.0);
|
||||
vec3 finalColor = vec3(0.0);
|
||||
|
||||
for (int i = 0; i < LIGHTS_PER_PASS; i++) {
|
||||
vec3 SLU = cc_lightPos[i].xyz - s.position;
|
||||
vec3 SLU = cc_lightPos[i].xyz - position;
|
||||
vec3 SL = normalize(SLU);
|
||||
vec3 SH = normalize(SL + V);
|
||||
float SNL = 0.5 * dot(N, SL) + 0.5;
|
||||
@@ -68,7 +72,7 @@ struct ToonSurface {
|
||||
#endif
|
||||
|
||||
vec4 CCToonShading (ToonSurface s) {
|
||||
vec3 V = normalize(cc_cameraPos.xyz - s.position);
|
||||
vec3 V = normalize(cc_cameraPos.xyz - position);
|
||||
vec3 N = normalize(s.normal);
|
||||
|
||||
vec3 L = normalize(-cc_mainLitDir.xyz);
|
||||
|
||||
@@ -47,7 +47,9 @@
|
||||
void main () { \
|
||||
StandardSurface s; surf(s); \
|
||||
fragColor0 = s.albedo; \
|
||||
fragColor1 = vec4(s.position, s.roughness); \
|
||||
vec3 position; \
|
||||
HIGHP_VALUE_FROM_STRUCT_DEFINED(position, s.position); \
|
||||
fragColor1 = vec4(position, s.roughness); \
|
||||
fragColor2 = vec4(s.normal, s.metallic); \
|
||||
fragColor3 = vec4(s.emissive, s.occlusion); \
|
||||
} \
|
||||
|
||||
@@ -196,7 +196,7 @@ CCProgram standard-fs %{
|
||||
nmmp.z * normalize(s.normal);
|
||||
#endif
|
||||
|
||||
s.position = v_position;
|
||||
HIGHP_VALUE_TO_STRUCT_DEFINED(v_position, s.position);
|
||||
|
||||
vec4 pbr = pbrParams;
|
||||
#if USE_PBR_MAP
|
||||
|
||||
@@ -250,7 +250,7 @@ CCProgram standard-fs %{
|
||||
nmmp.z * normalize(s.normal);
|
||||
#endif
|
||||
|
||||
s.position = v_position;
|
||||
HIGHP_VALUE_TO_STRUCT_DEFINED(v_position, s.position);
|
||||
|
||||
vec4 pbr = pbrParams;
|
||||
#if USE_PBR_MAP
|
||||
|
||||
@@ -176,7 +176,7 @@ CCProgram terrain-fs %{
|
||||
baseColor = texture(detailMap0, uv0);
|
||||
#endif
|
||||
|
||||
s.position = v_position;
|
||||
HIGHP_VALUE_TO_STRUCT_DEFINED(v_position, s.position);
|
||||
#if USE_NORMALMAP
|
||||
vec4 baseNormal = vec4(0, 0, 0, 0);
|
||||
#if LAYERS == 1
|
||||
|
||||
@@ -216,7 +216,7 @@ CCProgram toon-fs %{
|
||||
nmmp.z * normalize(s.normal);
|
||||
#endif
|
||||
|
||||
s.position = v_position;
|
||||
HIGHP_VALUE_TO_STRUCT_DEFINED(v_position, s.position);
|
||||
|
||||
s.specular = specular;
|
||||
#if USE_SPECULAR_MAP
|
||||
|
||||
@@ -94,7 +94,8 @@ CCProgram lighting-fs %{
|
||||
#endif
|
||||
|
||||
s.albedo = albedoMap;
|
||||
s.position = positionMap.xyz;
|
||||
vec3 position = positionMap.xyz;
|
||||
HIGHP_VALUE_TO_STRUCT_DEFINED(position, s.position);
|
||||
s.roughness = positionMap.w;
|
||||
s.normal = normalMap.xyz;
|
||||
s.metallic = normalMap.w;
|
||||
@@ -102,10 +103,10 @@ CCProgram lighting-fs %{
|
||||
s.occlusion = emissiveMap.w;
|
||||
// fixme: default value is 0, and give black result
|
||||
float fogFactor;
|
||||
CC_TRANSFER_FOG_BASE(vec4(s.position, 1), fogFactor);
|
||||
CC_TRANSFER_FOG_BASE(vec4(position, 1), fogFactor);
|
||||
|
||||
vec4 shadowPos;
|
||||
CC_TRANSFER_SHADOW_BASE(vec4(s.position, 1), shadowPos);
|
||||
CC_TRANSFER_SHADOW_BASE(vec4(position, 1), shadowPos);
|
||||
|
||||
vec4 color = CCStandardShadingBase(s, shadowPos) +
|
||||
#if CC_ENABLE_CLUSTERED_LIGHT_CULLING == 1
|
||||
|
||||
Reference in New Issue
Block a user