I0826 19:48:07.852568 6116 blender_python.cpp:191] Debug flags initialized to: CPU flags: AVX2 : True AVX : True SSE4.1 : True SSE3 : True SSE2 : True BVH layout : BVH8 Split : False CUDA flags: Adaptive Compile : False OptiX flags: CUDA streams : 1 OpenCL flags: Device type : ALL Debug : False Memory limit : 0 I0826 19:48:08.064708 6116 device_opencl.cpp:48] CLEW initialization succeeded. I0826 19:48:08.342893 6116 device_cuda.cpp:56] CUEW initialization failed: Error opening the library GPUShader: linking error: ===== shader string 1 ==== 1 #define COMMON_VIEW_LIB 2 #define DRW_RESOURCE_CHUNK_LEN 512 3 4 /* keep in sync with DRWManager.view_data */ 5 layout(std140) uniform viewBlock 6 { 7 /* Same order as DRWViewportMatrixType */ 8 mat4 ViewProjectionMatrix; 9 mat4 ViewProjectionMatrixInverse; 10 mat4 ViewMatrix; 11 mat4 ViewMatrixInverse; 12 mat4 ProjectionMatrix; 13 mat4 ProjectionMatrixInverse; 14 15 vec4 clipPlanes[6]; 16 17 /* TODO move it elsewhere. */ 18 vec4 CameraTexCoFactors; 19 }; 20 21 #ifdef world_clip_planes_calc_clip_distance 22 # undef world_clip_planes_calc_clip_distance 23 # define world_clip_planes_calc_clip_distance(p) \ 24 _world_clip_planes_calc_clip_distance(p, clipPlanes) 25 #endif 26 27 #ifdef COMMON_GLOBALS_LIB 28 float mul_project_m4_v3_zfac(in vec3 co) 29 { 30 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 31 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 32 } 33 #endif 34 35 /* Not the right place but need to be common to all overlay's. 36 * TODO Split to an overlay lib. */ 37 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 38 { 39 const float div = 1.0 / 255.0; 40 int a = int(mat[0][3]); 41 int b = int(mat[1][3]); 42 int c = int(mat[2][3]); 43 int d = int(mat[3][3]); 44 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 45 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 46 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 47 mat[3][3] = 1.0; 48 return mat; 49 } 50 51 /* Same here, Not the right place but need to be common to all overlay's. 52 * TODO Split to an overlay lib. */ 53 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 54 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 55 { 56 vec2 edge = edge_start - edge_pos; 57 float len = length(edge); 58 if (len > 0.0) { 59 edge /= len; 60 vec2 perp = vec2(-edge.y, edge.x); 61 float dist = dot(perp, frag_co - edge_start); 62 /* Add 0.1 to diffenrentiate with cleared pixels. */ 63 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 1.0); 64 } 65 else { 66 /* Default line if the origin is perfectly aligned with a pixel. */ 67 return vec4(1.0, 0.0, 0.5 + 0.1, 1.0); 68 } 69 } 70 71 uniform int resourceChunk; 72 73 #ifdef GPU_VERTEX_SHADER 74 # ifdef GPU_ARB_shader_draw_parameters 75 # define baseInstance gl_BaseInstanceARB 76 # else /* no ARB_shader_draw_parameters */ 77 uniform int baseInstance; 78 # endif 79 80 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTR) 81 /* When drawing instances of an object at the same position. */ 82 # define instanceId 0 83 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 84 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 85 * the gl_InstanceID is incremented by the 2 bit component of the attribute. 86 * Ignore gl_InstanceID then. */ 87 # define instanceId 0 88 # else 89 # define instanceId gl_InstanceID 90 # endif 91 92 # ifdef UNIFORM_RESOURCE_ID 93 /* This is in the case we want to do a special instance drawcall but still want to have the 94 * right resourceId and all the correct ubo datas. */ 95 uniform int resourceId; 96 # define resource_id resourceId 97 # else 98 # define resource_id (baseInstance + instanceId) 99 # endif 100 101 /* Use this to declare and pass the value if 102 * the fragment shader uses the resource_id. */ 103 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 104 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 105 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 106 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 107 #endif 108 109 /* If used in a fragment / geometry shader, we pass 110 * resource_id as varying. */ 111 #ifdef GPU_GEOMETRY_SHADER 112 # define RESOURCE_ID_VARYING \ 113 flat out int resourceIDFrag; \ 114 flat in int resourceIDGeom[]; 115 116 # define resource_id resourceIDGeom 117 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 118 #endif 119 120 #ifdef GPU_FRAGMENT_SHADER 121 flat in int resourceIDFrag; 122 # define resource_id resourceIDFrag 123 #endif 124 125 /* Breaking this across multiple lines causes issues for some older GLSL compilers. */ 126 /* clang-format off */ 127 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && !defined(INSTANCED_ATTR) 128 /* clang-format on */ 129 struct ObjectMatrices { 130 mat4 drw_modelMatrix; 131 mat4 drw_modelMatrixInverse; 132 }; 133 134 layout(std140) uniform modelBlock 135 { 136 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 137 }; 138 139 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 140 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 141 142 #else /* GPU_INTEL */ 143 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 144 * So for now we just force using the legacy path. */ 145 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 146 * and older amd driver on windows. */ 147 uniform mat4 ModelMatrix; 148 uniform mat4 ModelMatrixInverse; 149 #endif 150 151 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 152 153 /** Transform shortcuts. */ 154 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 155 * will always be decomposed in at least 2 matrix operation. */ 156 157 /** 158 * Some clarification: 159 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 160 * 161 * But since it is slow to multiply matrices we decompose it. Decomposing 162 * inversion and transposition both invert the product order leaving us with 163 * the same original order: 164 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 165 * 166 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 167 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 168 * ViewMatrix * transpose(ModelMatrixInverse) 169 **/ 170 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 171 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 172 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 173 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 174 175 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 176 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 177 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 178 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 179 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 180 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 181 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 182 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 183 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 184 185 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 186 * to make vertex shaders work. even if it's actually dead code. */ 187 #ifdef GPU_INTEL 188 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 189 #else 190 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 191 #endif 192 193 #define DRW_BASE_SELECTED (1 << 1) 194 #define DRW_BASE_FROM_DUPLI (1 << 2) 195 #define DRW_BASE_FROM_SET (1 << 3) 196 #define DRW_BASE_ACTIVE (1 << 4) 197 198 #ifdef GPU_VERTEX_SHADER 199 # define IN_OUT out 200 #else 201 # define IN_OUT in 202 #endif 203 204 IN_OUT ShaderStageInterface 205 { 206 vec3 normal_interp; 207 vec3 color_interp; 208 float alpha_interp; 209 vec2 uv_interp; 210 #ifdef TRANSPARENT_MATERIAL 211 flat float roughness; 212 flat float metallic; 213 #else 214 flat float packed_rough_metal; 215 #endif 216 flat int object_id; 217 }; 218 219 #define EPSILON 0.00001 220 #define M_PI 3.14159265358979323846 221 222 #define CAVITY_BUFFER_RANGE 4.0 223 224 #ifdef WORKBENCH_ENCODE_NORMALS 225 226 # define WB_Normal vec2 227 228 /* From http://aras-p.info/texts/CompactNormalStorage.html 229 * Using Method #4: Spheremap Transform */ 230 vec3 workbench_normal_decode(vec4 enc) 231 { 232 vec2 fenc = enc.xy * 4.0 - 2.0; 233 float f = dot(fenc, fenc); 234 float g = sqrt(1.0 - f / 4.0); 235 vec3 n; 236 n.xy = fenc * g; 237 n.z = 1 - f / 2; 238 return n; 239 } 240 241 /* From http://aras-p.info/texts/CompactNormalStorage.html 242 * Using Method #4: Spheremap Transform */ 243 WB_Normal workbench_normal_encode(bool front_face, vec3 n) 244 { 245 n = normalize(front_face ? n : -n); 246 float p = sqrt(n.z * 8.0 + 8.0); 247 n.xy = clamp(n.xy / p + 0.5, 0.0, 1.0); 248 return n.xy; 249 } 250 251 #else 252 # define WB_Normal vec3 253 /* Well just do nothing... */ 254 # define workbench_normal_encode(f, a) (a) 255 # define workbench_normal_decode(a) (a.xyz) 256 #endif /* WORKBENCH_ENCODE_NORMALS */ 257 258 /* Encoding into the alpha of a RGBA16F texture. (10bit mantissa) */ 259 #define TARGET_BITCOUNT 8u 260 #define METALLIC_BITS 3u /* Metallic channel is less important. */ 261 #define ROUGHNESS_BITS (TARGET_BITCOUNT - METALLIC_BITS) 262 263 /* Encode 2 float into 1 with the desired precision. */ 264 float workbench_float_pair_encode(float v1, float v2) 265 { 266 // const uint v1_mask = ~(0xFFFFFFFFu << ROUGHNESS_BITS); 267 // const uint v2_mask = ~(0xFFFFFFFFu << METALLIC_BITS); 268 /* Same as above because some compiler are dumb af. and think we use mediump int. */ 269 const int v1_mask = 0x1F; 270 const int v2_mask = 0x7; 271 int iv1 = int(v1 * float(v1_mask)); 272 int iv2 = int(v2 * float(v2_mask)) << int(ROUGHNESS_BITS); 273 return float(iv1 | iv2); 274 } 275 276 void workbench_float_pair_decode(float data, out float v1, out float v2) 277 { 278 // const uint v1_mask = ~(0xFFFFFFFFu << ROUGHNESS_BITS); 279 // const uint v2_mask = ~(0xFFFFFFFFu << METALLIC_BITS); 280 /* Same as above because some compiler are dumb af. and think we use mediump int. */ 281 const int v1_mask = 0x1F; 282 const int v2_mask = 0x7; 283 int idata = int(data); 284 v1 = float(idata & v1_mask) * (1.0 / float(v1_mask)); 285 v2 = float(idata >> int(ROUGHNESS_BITS)) * (1.0 / float(v2_mask)); 286 } 287 288 vec3 view_vector_from_screen_uv(vec2 uv, vec4 viewvecs[3], mat4 proj_mat) 289 { 290 if (proj_mat[3][3] == 0.0) { 291 return normalize(viewvecs[0].xyz + vec3(uv, 0.0) * viewvecs[1].xyz); 292 } 293 else { 294 return vec3(0.0, 0.0, 1.0); 295 } 296 } 297 298 vec3 view_position_from_depth(vec2 uvcoords, float depth, vec4 viewvecs[3], mat4 proj_mat) 299 { 300 if (proj_mat[3][3] == 0.0) { 301 /* Perspective */ 302 float d = 2.0 * depth - 1.0; 303 304 float zview = -proj_mat[3][2] / (d + proj_mat[2][2]); 305 306 return zview * (viewvecs[0].xyz + vec3(uvcoords, 0.0) * viewvecs[1].xyz); 307 } 308 else { 309 /* Orthographic */ 310 vec3 offset = vec3(uvcoords, depth); 311 312 return viewvecs[0].xyz + offset * viewvecs[1].xyz; 313 } 314 } 315 316 /* TODO(fclem) deduplicate code. */ 317 bool node_tex_tile_lookup(inout vec3 co, sampler2DArray ima, sampler1DArray map) 318 { 319 vec2 tile_pos = floor(co.xy); 320 321 if (tile_pos.x < 0 || tile_pos.y < 0 || tile_pos.x >= 10) 322 return false; 323 324 float tile = 10.0 * tile_pos.y + tile_pos.x; 325 if (tile >= textureSize(map, 0).x) 326 return false; 327 328 /* Fetch tile information. */ 329 float tile_layer = texelFetch(map, ivec2(tile, 0), 0).x; 330 if (tile_layer < 0.0) 331 return false; 332 333 vec4 tile_info = texelFetch(map, ivec2(tile, 1), 0); 334 335 co = vec3(((co.xy - tile_pos) * tile_info.zw) + tile_info.xy, tile_layer); 336 return true; 337 } 338 339 vec4 workbench_sample_texture(sampler2D image, vec2 coord, bool nearest_sampling) 340 { 341 /* TODO(fclem) We could do the same with sampler objects. 342 * But this is a quick workaround instead of messing with the GPUTexture itself. */ 343 if (nearest_sampling) { 344 /* Use texelFetch for nearest_sampling to reduce glitches. See: T73726 */ 345 vec2 tex_size = vec2(textureSize(image, 0).xy); 346 ivec2 uv = ivec2(floor(coord * tex_size) + 0.5); 347 return texelFetch(image, uv, 0); 348 } 349 else { 350 return texture(image, coord); 351 } 352 } 353 354 vec4 workbench_sample_texture_array(sampler2DArray tile_array, 355 sampler1DArray tile_data, 356 vec2 coord, 357 bool nearest_sampling) 358 { 359 360 vec3 uv = vec3(coord, 0); 361 if (!node_tex_tile_lookup(uv, tile_array, tile_data)) 362 return vec4(1.0, 0.0, 1.0, 1.0); 363 364 /* TODO(fclem) We could do the same with sampler objects. 365 * But this is a quick workaround instead of messing with the GPUTexture itself. */ 366 if (nearest_sampling) { 367 /* Use texelFetch for nearest_sampling to reduce glitches. See: T73726 */ 368 vec3 tex_size = vec3(textureSize(tile_array, 0)); 369 uv.xy = floor(uv.xy * tex_size.xy) + 0.5; 370 return texelFetch(tile_array, ivec3(uv), 0); 371 } 372 else { 373 return texture(tile_array, uv); 374 } 375 } 376 377 uniform sampler2DArray imageTileArray; 378 uniform sampler1DArray imageTileData; 379 uniform sampler2D imageTexture; 380 381 uniform float imageTransparencyCutoff = 0.1; 382 uniform bool imageNearest; 383 uniform bool imagePremult; 384 385 vec3 workbench_image_color(vec2 uvs) 386 { 387 #ifdef V3D_SHADING_TEXTURE_COLOR 388 # ifdef TEXTURE_IMAGE_ARRAY 389 vec4 color = workbench_sample_texture_array(imageTileArray, imageTileData, uvs, imageNearest); 390 # else 391 vec4 color = workbench_sample_texture(imageTexture, uvs, imageNearest); 392 # endif 393 394 /* Unpremultiply if stored multiplied, since straight alpha is expected by shaders. */ 395 if (imagePremult && !(color.a == 0.0 || color.a == 1.0)) { 396 color.rgb /= color.a; 397 } 398 399 # ifdef GPU_FRAGMENT_SHADER 400 if (color.a < imageTransparencyCutoff) { 401 discard; 402 } 403 # endif 404 405 return color.rgb; 406 #else 407 return vec3(1.0); 408 #endif 409 } 410 411 layout(std140) uniform material_block 412 { 413 vec4 mat_data[4096]; 414 }; 415 416 /* If set to -1, the resource handle is used instead. */ 417 uniform int materialIndex; 418 419 void workbench_material_data_get( 420 int handle, out vec3 color, out float alpha, out float roughness, out float metallic) 421 { 422 handle = (materialIndex != -1) ? materialIndex : handle; 423 vec4 data = mat_data[uint(handle) & 0xFFFu]; 424 color = data.rgb; 425 426 uint encoded_data = floatBitsToUint(data.w); 427 alpha = float((encoded_data >> 16u) & 0xFFu) * (1.0 / 255.0); 428 roughness = float((encoded_data >> 8u) & 0xFFu) * (1.0 / 255.0); 429 metallic = float(encoded_data & 0xFFu) * (1.0 / 255.0); 430 } 431 432 #pragma BLENDER_REQUIRE(common_view_lib.glsl) 433 #pragma BLENDER_REQUIRE(workbench_shader_interface_lib.glsl) 434 #pragma BLENDER_REQUIRE(workbench_common_lib.glsl) 435 #pragma BLENDER_REQUIRE(workbench_material_lib.glsl) 436 #pragma BLENDER_REQUIRE(workbench_image_lib.glsl) 437 438 in vec3 pos; 439 in vec3 nor; 440 in vec4 ac; /* active color */ 441 in vec2 au; /* active texture layer */ 442 443 void main() 444 { 445 vec3 world_pos = point_object_to_world(pos); 446 gl_Position = point_world_to_ndc(world_pos); 447 448 #ifdef USE_WORLD_CLIP_PLANES 449 world_clip_planes_calc_clip_distance(world_pos); 450 #endif 451 452 uv_interp = au; 453 454 normal_interp = normalize(normal_object_to_view(nor)); 455 456 #ifdef OPAQUE_MATERIAL 457 float metallic, roughness; 458 #endif 459 workbench_material_data_get(resource_handle, color_interp, alpha_interp, roughness, metallic); 460 461 if (materialIndex == 0) { 462 color_interp = ac.rgb; 463 } 464 465 #ifdef OPAQUE_MATERIAL 466 packed_rough_metal = workbench_float_pair_encode(roughness, metallic); 467 #endif 468 469 object_id = int(uint(resource_handle) & 0xFFFFu) + 1; 470 } Vertex shader(s) failed to link. Vertex link error: The number of attributes used exceeded the maximum. GPUShader: linking error: ===== shader string 1 ==== 1 #define COMMON_VIEW_LIB 2 #define DRW_RESOURCE_CHUNK_LEN 512 3 4 /* keep in sync with DRWManager.view_data */ 5 layout(std140) uniform viewBlock 6 { 7 /* Same order as DRWViewportMatrixType */ 8 mat4 ViewProjectionMatrix; 9 mat4 ViewProjectionMatrixInverse; 10 mat4 ViewMatrix; 11 mat4 ViewMatrixInverse; 12 mat4 ProjectionMatrix; 13 mat4 ProjectionMatrixInverse; 14 15 vec4 clipPlanes[6]; 16 17 /* TODO move it elsewhere. */ 18 vec4 CameraTexCoFactors; 19 }; 20 21 #ifdef world_clip_planes_calc_clip_distance 22 # undef world_clip_planes_calc_clip_distance 23 # define world_clip_planes_calc_clip_distance(p) \ 24 _world_clip_planes_calc_clip_distance(p, clipPlanes) 25 #endif 26 27 #ifdef COMMON_GLOBALS_LIB 28 float mul_project_m4_v3_zfac(in vec3 co) 29 { 30 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 31 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 32 } 33 #endif 34 35 /* Not the right place but need to be common to all overlay's. 36 * TODO Split to an overlay lib. */ 37 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 38 { 39 const float div = 1.0 / 255.0; 40 int a = int(mat[0][3]); 41 int b = int(mat[1][3]); 42 int c = int(mat[2][3]); 43 int d = int(mat[3][3]); 44 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 45 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 46 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 47 mat[3][3] = 1.0; 48 return mat; 49 } 50 51 /* Same here, Not the right place but need to be common to all overlay's. 52 * TODO Split to an overlay lib. */ 53 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 54 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 55 { 56 vec2 edge = edge_start - edge_pos; 57 float len = length(edge); 58 if (len > 0.0) { 59 edge /= len; 60 vec2 perp = vec2(-edge.y, edge.x); 61 float dist = dot(perp, frag_co - edge_start); 62 /* Add 0.1 to diffenrentiate with cleared pixels. */ 63 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 1.0); 64 } 65 else { 66 /* Default line if the origin is perfectly aligned with a pixel. */ 67 return vec4(1.0, 0.0, 0.5 + 0.1, 1.0); 68 } 69 } 70 71 uniform int resourceChunk; 72 73 #ifdef GPU_VERTEX_SHADER 74 # ifdef GPU_ARB_shader_draw_parameters 75 # define baseInstance gl_BaseInstanceARB 76 # else /* no ARB_shader_draw_parameters */ 77 uniform int baseInstance; 78 # endif 79 80 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTR) 81 /* When drawing instances of an object at the same position. */ 82 # define instanceId 0 83 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 84 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 85 * the gl_InstanceID is incremented by the 2 bit component of the attribute. 86 * Ignore gl_InstanceID then. */ 87 # define instanceId 0 88 # else 89 # define instanceId gl_InstanceID 90 # endif 91 92 # ifdef UNIFORM_RESOURCE_ID 93 /* This is in the case we want to do a special instance drawcall but still want to have the 94 * right resourceId and all the correct ubo datas. */ 95 uniform int resourceId; 96 # define resource_id resourceId 97 # else 98 # define resource_id (baseInstance + instanceId) 99 # endif 100 101 /* Use this to declare and pass the value if 102 * the fragment shader uses the resource_id. */ 103 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 104 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 105 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 106 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 107 #endif 108 109 /* If used in a fragment / geometry shader, we pass 110 * resource_id as varying. */ 111 #ifdef GPU_GEOMETRY_SHADER 112 # define RESOURCE_ID_VARYING \ 113 flat out int resourceIDFrag; \ 114 flat in int resourceIDGeom[]; 115 116 # define resource_id resourceIDGeom 117 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 118 #endif 119 120 #ifdef GPU_FRAGMENT_SHADER 121 flat in int resourceIDFrag; 122 # define resource_id resourceIDFrag 123 #endif 124 125 /* Breaking this across multiple lines causes issues for some older GLSL compilers. */ 126 /* clang-format off */ 127 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && !defined(INSTANCED_ATTR) 128 /* clang-format on */ 129 struct ObjectMatrices { 130 mat4 drw_modelMatrix; 131 mat4 drw_modelMatrixInverse; 132 }; 133 134 layout(std140) uniform modelBlock 135 { 136 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 137 }; 138 139 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 140 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 141 142 #else /* GPU_INTEL */ 143 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 144 * So for now we just force using the legacy path. */ 145 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 146 * and older amd driver on windows. */ 147 uniform mat4 ModelMatrix; 148 uniform mat4 ModelMatrixInverse; 149 #endif 150 151 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 152 153 /** Transform shortcuts. */ 154 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 155 * will always be decomposed in at least 2 matrix operation. */ 156 157 /** 158 * Some clarification: 159 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 160 * 161 * But since it is slow to multiply matrices we decompose it. Decomposing 162 * inversion and transposition both invert the product order leaving us with 163 * the same original order: 164 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 165 * 166 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 167 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 168 * ViewMatrix * transpose(ModelMatrixInverse) 169 **/ 170 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 171 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 172 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 173 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 174 175 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 176 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 177 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 178 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 179 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 180 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 181 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 182 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 183 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 184 185 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 186 * to make vertex shaders work. even if it's actually dead code. */ 187 #ifdef GPU_INTEL 188 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 189 #else 190 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 191 #endif 192 193 #define DRW_BASE_SELECTED (1 << 1) 194 #define DRW_BASE_FROM_DUPLI (1 << 2) 195 #define DRW_BASE_FROM_SET (1 << 3) 196 #define DRW_BASE_ACTIVE (1 << 4) 197 198 #ifdef GPU_VERTEX_SHADER 199 # define IN_OUT out 200 #else 201 # define IN_OUT in 202 #endif 203 204 IN_OUT ShaderStageInterface 205 { 206 vec3 normal_interp; 207 vec3 color_interp; 208 float alpha_interp; 209 vec2 uv_interp; 210 #ifdef TRANSPARENT_MATERIAL 211 flat float roughness; 212 flat float metallic; 213 #else 214 flat float packed_rough_metal; 215 #endif 216 flat int object_id; 217 }; 218 219 #define EPSILON 0.00001 220 #define M_PI 3.14159265358979323846 221 222 #define CAVITY_BUFFER_RANGE 4.0 223 224 #ifdef WORKBENCH_ENCODE_NORMALS 225 226 # define WB_Normal vec2 227 228 /* From http://aras-p.info/texts/CompactNormalStorage.html 229 * Using Method #4: Spheremap Transform */ 230 vec3 workbench_normal_decode(vec4 enc) 231 { 232 vec2 fenc = enc.xy * 4.0 - 2.0; 233 float f = dot(fenc, fenc); 234 float g = sqrt(1.0 - f / 4.0); 235 vec3 n; 236 n.xy = fenc * g; 237 n.z = 1 - f / 2; 238 return n; 239 } 240 241 /* From http://aras-p.info/texts/CompactNormalStorage.html 242 * Using Method #4: Spheremap Transform */ 243 WB_Normal workbench_normal_encode(bool front_face, vec3 n) 244 { 245 n = normalize(front_face ? n : -n); 246 float p = sqrt(n.z * 8.0 + 8.0); 247 n.xy = clamp(n.xy / p + 0.5, 0.0, 1.0); 248 return n.xy; 249 } 250 251 #else 252 # define WB_Normal vec3 253 /* Well just do nothing... */ 254 # define workbench_normal_encode(f, a) (a) 255 # define workbench_normal_decode(a) (a.xyz) 256 #endif /* WORKBENCH_ENCODE_NORMALS */ 257 258 /* Encoding into the alpha of a RGBA16F texture. (10bit mantissa) */ 259 #define TARGET_BITCOUNT 8u 260 #define METALLIC_BITS 3u /* Metallic channel is less important. */ 261 #define ROUGHNESS_BITS (TARGET_BITCOUNT - METALLIC_BITS) 262 263 /* Encode 2 float into 1 with the desired precision. */ 264 float workbench_float_pair_encode(float v1, float v2) 265 { 266 // const uint v1_mask = ~(0xFFFFFFFFu << ROUGHNESS_BITS); 267 // const uint v2_mask = ~(0xFFFFFFFFu << METALLIC_BITS); 268 /* Same as above because some compiler are dumb af. and think we use mediump int. */ 269 const int v1_mask = 0x1F; 270 const int v2_mask = 0x7; 271 int iv1 = int(v1 * float(v1_mask)); 272 int iv2 = int(v2 * float(v2_mask)) << int(ROUGHNESS_BITS); 273 return float(iv1 | iv2); 274 } 275 276 void workbench_float_pair_decode(float data, out float v1, out float v2) 277 { 278 // const uint v1_mask = ~(0xFFFFFFFFu << ROUGHNESS_BITS); 279 // const uint v2_mask = ~(0xFFFFFFFFu << METALLIC_BITS); 280 /* Same as above because some compiler are dumb af. and think we use mediump int. */ 281 const int v1_mask = 0x1F; 282 const int v2_mask = 0x7; 283 int idata = int(data); 284 v1 = float(idata & v1_mask) * (1.0 / float(v1_mask)); 285 v2 = float(idata >> int(ROUGHNESS_BITS)) * (1.0 / float(v2_mask)); 286 } 287 288 vec3 view_vector_from_screen_uv(vec2 uv, vec4 viewvecs[3], mat4 proj_mat) 289 { 290 if (proj_mat[3][3] == 0.0) { 291 return normalize(viewvecs[0].xyz + vec3(uv, 0.0) * viewvecs[1].xyz); 292 } 293 else { 294 return vec3(0.0, 0.0, 1.0); 295 } 296 } 297 298 vec3 view_position_from_depth(vec2 uvcoords, float depth, vec4 viewvecs[3], mat4 proj_mat) 299 { 300 if (proj_mat[3][3] == 0.0) { 301 /* Perspective */ 302 float d = 2.0 * depth - 1.0; 303 304 float zview = -proj_mat[3][2] / (d + proj_mat[2][2]); 305 306 return zview * (viewvecs[0].xyz + vec3(uvcoords, 0.0) * viewvecs[1].xyz); 307 } 308 else { 309 /* Orthographic */ 310 vec3 offset = vec3(uvcoords, depth); 311 312 return viewvecs[0].xyz + offset * viewvecs[1].xyz; 313 } 314 } 315 316 /* TODO(fclem) deduplicate code. */ 317 bool node_tex_tile_lookup(inout vec3 co, sampler2DArray ima, sampler1DArray map) 318 { 319 vec2 tile_pos = floor(co.xy); 320 321 if (tile_pos.x < 0 || tile_pos.y < 0 || tile_pos.x >= 10) 322 return false; 323 324 float tile = 10.0 * tile_pos.y + tile_pos.x; 325 if (tile >= textureSize(map, 0).x) 326 return false; 327 328 /* Fetch tile information. */ 329 float tile_layer = texelFetch(map, ivec2(tile, 0), 0).x; 330 if (tile_layer < 0.0) 331 return false; 332 333 vec4 tile_info = texelFetch(map, ivec2(tile, 1), 0); 334 335 co = vec3(((co.xy - tile_pos) * tile_info.zw) + tile_info.xy, tile_layer); 336 return true; 337 } 338 339 vec4 workbench_sample_texture(sampler2D image, vec2 coord, bool nearest_sampling) 340 { 341 /* TODO(fclem) We could do the same with sampler objects. 342 * But this is a quick workaround instead of messing with the GPUTexture itself. */ 343 if (nearest_sampling) { 344 /* Use texelFetch for nearest_sampling to reduce glitches. See: T73726 */ 345 vec2 tex_size = vec2(textureSize(image, 0).xy); 346 ivec2 uv = ivec2(floor(coord * tex_size) + 0.5); 347 return texelFetch(image, uv, 0); 348 } 349 else { 350 return texture(image, coord); 351 } 352 } 353 354 vec4 workbench_sample_texture_array(sampler2DArray tile_array, 355 sampler1DArray tile_data, 356 vec2 coord, 357 bool nearest_sampling) 358 { 359 360 vec3 uv = vec3(coord, 0); 361 if (!node_tex_tile_lookup(uv, tile_array, tile_data)) 362 return vec4(1.0, 0.0, 1.0, 1.0); 363 364 /* TODO(fclem) We could do the same with sampler objects. 365 * But this is a quick workaround instead of messing with the GPUTexture itself. */ 366 if (nearest_sampling) { 367 /* Use texelFetch for nearest_sampling to reduce glitches. See: T73726 */ 368 vec3 tex_size = vec3(textureSize(tile_array, 0)); 369 uv.xy = floor(uv.xy * tex_size.xy) + 0.5; 370 return texelFetch(tile_array, ivec3(uv), 0); 371 } 372 else { 373 return texture(tile_array, uv); 374 } 375 } 376 377 uniform sampler2DArray imageTileArray; 378 uniform sampler1DArray imageTileData; 379 uniform sampler2D imageTexture; 380 381 uniform float imageTransparencyCutoff = 0.1; 382 uniform bool imageNearest; 383 uniform bool imagePremult; 384 385 vec3 workbench_image_color(vec2 uvs) 386 { 387 #ifdef V3D_SHADING_TEXTURE_COLOR 388 # ifdef TEXTURE_IMAGE_ARRAY 389 vec4 color = workbench_sample_texture_array(imageTileArray, imageTileData, uvs, imageNearest); 390 # else 391 vec4 color = workbench_sample_texture(imageTexture, uvs, imageNearest); 392 # endif 393 394 /* Unpremultiply if stored multiplied, since straight alpha is expected by shaders. */ 395 if (imagePremult && !(color.a == 0.0 || color.a == 1.0)) { 396 color.rgb /= color.a; 397 } 398 399 # ifdef GPU_FRAGMENT_SHADER 400 if (color.a < imageTransparencyCutoff) { 401 discard; 402 } 403 # endif 404 405 return color.rgb; 406 #else 407 return vec3(1.0); 408 #endif 409 } 410 411 #pragma BLENDER_REQUIRE(common_view_lib.glsl) 412 #pragma BLENDER_REQUIRE(workbench_shader_interface_lib.glsl) 413 #pragma BLENDER_REQUIRE(workbench_common_lib.glsl) 414 #pragma BLENDER_REQUIRE(workbench_image_lib.glsl) 415 416 layout(location = 0) out vec4 materialData; 417 layout(location = 1) out WB_Normal normalData; 418 layout(location = 2) out uint objectId; 419 420 uniform bool useMatcap = false; 421 422 void main() 423 { 424 normalData = workbench_normal_encode(gl_FrontFacing, normal_interp); 425 426 materialData = vec4(color_interp, packed_rough_metal); 427 428 objectId = uint(object_id); 429 430 if (useMatcap) { 431 /* For matcaps, save front facing in alpha channel. */ 432 materialData.a = float(gl_FrontFacing); 433 } 434 435 #ifdef V3D_SHADING_TEXTURE_COLOR 436 materialData.rgb = workbench_image_color(uv_interp); 437 #endif 438 } Vertex shader(s) failed to link. Vertex link error: The number of attributes used exceeded the maximum. Error : EXCEPTION_ACCESS_VIOLATION Address : 0x000007F6EF908B24 Module : C:\Program Files\Blender Foundation\Blender 2.83\blender.exe