Switching to fully guarded memory allocator. Blender 2.82 (sub 4) Build: 2019-12-06 19:18:33 Windows Release argv[0] = blender argv[1] = --debug argv[2] = --debug-cycles argv[3] = --python-expr argv[4] = import bpy; bpy.ops.wm.sysinfo(filepath=r'C:\Users\ADMINI~1\AppData\Local\Temp\blender\debug_logs\blender_system_info.txt') Read prefs: C:\Users\Administrator\AppData\Roaming\Blender Foundation\Blender\2.82\config\userpref.blend read file C:\Users\Administrator\AppData\Roaming\Blender Foundation\Blender\2.82\config\startup.blend Version 281 sub 14 date 2019-10-08 17:22 hash 48778d86a6d0 AL lib: (EE) UpdateDeviceParams: Failed to set 48000hz, got 44100hz instead found bundled python: c:\blender-2.82-5e96b860a372-windows64\2.82\python I1206 14:40:00.224670 3180 blender_python.cpp:184] 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 I1206 14:40:01.484673 3180 device_opencl.cpp:48] CLEW initialization succeeded. I1206 14:40:01.504673 3180 device_cuda.cpp:2582] CUEW initialization succeeded I1206 14:40:01.504673 3180 device_cuda.cpp:2584] Found precompiled kernels GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define SHADOW_PASS 12 #define DOUBLE_MANIFOLD ===== shader string 6 ==== 13 #define COMMON_VIEW_LIB 14 #define DRW_RESOURCE_CHUNK_LEN 512 15 16 /* keep in sync with DRWManager.view_data */ 17 layout(std140) uniform viewBlock 18 { 19 /* Same order as DRWViewportMatrixType */ 20 mat4 ViewProjectionMatrix; 21 mat4 ViewProjectionMatrixInverse; 22 mat4 ViewMatrix; 23 mat4 ViewMatrixInverse; 24 mat4 ProjectionMatrix; 25 mat4 ProjectionMatrixInverse; 26 27 vec4 clipPlanes[6]; 28 29 /* TODO move it elsewhere. */ 30 vec4 CameraTexCoFactors; 31 }; 32 33 #ifdef world_clip_planes_calc_clip_distance 34 # undef world_clip_planes_calc_clip_distance 35 # define world_clip_planes_calc_clip_distance(p) \ 36 _world_clip_planes_calc_clip_distance(p, clipPlanes) 37 #endif 38 39 #ifdef COMMON_GLOBALS_LIB 40 float mul_project_m4_v3_zfac(in vec3 co) 41 { 42 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 43 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 44 } 45 #endif 46 47 /* Not the right place but need to be common to all overlay's. 48 * TODO Split to an overlay lib. */ 49 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 50 { 51 const float div = 1.0 / 255.0; 52 int a = int(mat[0][3]); 53 int b = int(mat[1][3]); 54 int c = int(mat[2][3]); 55 int d = int(mat[3][3]); 56 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 57 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 58 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 59 mat[3][3] = 1.0; 60 return mat; 61 } 62 63 /* Same here, Not the right place but need to be common to all overlay's. 64 * TODO Split to an overlay lib. */ 65 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 66 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 67 { 68 vec2 edge = edge_start - edge_pos; 69 float len = length(edge); 70 if (len > 0.0) { 71 edge /= len; 72 vec2 perp = vec2(-edge.y, edge.x); 73 float dist = dot(perp, frag_co - edge_start); 74 /* Add 0.1 to diffenrentiate with cleared pixels. */ 75 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 76 } 77 else { 78 /* Default line if the origin is perfectly aligned with a pixel. */ 79 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 80 } 81 } 82 83 uniform int resourceChunk; 84 85 #ifdef GPU_VERTEX_SHADER 86 # ifdef GL_ARB_shader_draw_parameters 87 # define baseInstance gl_BaseInstanceARB 88 # else /* no ARB_shader_draw_parameters */ 89 uniform int baseInstance; 90 # endif 91 92 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 93 /* When drawing instances of an object at the same position. */ 94 # define instanceId 0 95 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 96 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 97 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 98 * Ignore gl_InstanceID then. */ 99 # define instanceId 0 100 # else 101 # define instanceId gl_InstanceID 102 # endif 103 104 # define resource_id (baseInstance + instanceId) 105 106 /* Use this to declare and pass the value if 107 * the fragment shader uses the resource_id. */ 108 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 109 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 110 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 111 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 112 #endif 113 114 /* If used in a fragment / geometry shader, we pass 115 * resource_id as varying. */ 116 #ifdef GPU_GEOMETRY_SHADER 117 # define RESOURCE_ID_VARYING \ 118 flat out int resourceIDFrag; \ 119 flat in int resourceIDGeom[]; 120 121 # define resource_id resourceIDGeom 122 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 123 #endif 124 125 #ifdef GPU_FRAGMENT_SHADER 126 flat in int resourceIDFrag; 127 # define resource_id resourceIDFrag 128 #endif 129 130 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 131 !defined(INSTANCED_ATTRIB) 132 struct ObjectMatrices { 133 mat4 drw_modelMatrix; 134 mat4 drw_modelMatrixInverse; 135 }; 136 137 layout(std140) uniform modelBlock 138 { 139 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 140 }; 141 142 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 143 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 144 145 #else /* GPU_INTEL */ 146 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 147 * So for now we just force using the legacy path. */ 148 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 149 * and older amd driver on windows. */ 150 uniform mat4 ModelMatrix; 151 uniform mat4 ModelMatrixInverse; 152 #endif 153 154 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 155 156 /** Transform shortcuts. */ 157 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 158 * will always be decomposed in at least 2 matrix operation. */ 159 160 /** 161 * Some clarification: 162 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 163 * 164 * But since it is slow to multiply matrices we decompose it. Decomposing 165 * inversion and transposition both invert the product order leaving us with 166 * the same original order: 167 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 168 * 169 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 170 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 171 * ViewMatrix * transpose(ModelMatrixInverse) 172 **/ 173 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 174 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 175 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 176 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 177 178 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 179 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 180 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 181 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 182 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 183 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 184 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 185 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 186 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 187 188 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 189 * to make vertex shaders work. even if it's actually dead code. */ 190 #ifdef GPU_INTEL 191 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 192 #else 193 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 194 #endif 195 196 #define DRW_BASE_SELECTED (1 << 1) 197 #define DRW_BASE_FROM_DUPLI (1 << 2) 198 #define DRW_BASE_FROM_SET (1 << 3) 199 #define DRW_BASE_ACTIVE (1 << 4) 200 #define INFINITE 1000.0 201 202 uniform vec3 lightDirection = vec3(0.57, 0.57, -0.57); 203 uniform float lightDistance = 1e4; 204 205 in vec3 pos; 206 207 out VertexData 208 { 209 vec3 pos; /* local position */ 210 vec4 frontPosition; /* final ndc position */ 211 vec4 backPosition; 212 } 213 vData; 214 215 void main() 216 { 217 vData.pos = pos; 218 vData.frontPosition = point_object_to_ndc(pos); 219 vData.backPosition = point_object_to_ndc(pos + lightDirection * lightDistance); 220 } 0(130) : error C0105: Syntax error in #if 0(130) : error C0105: Syntax error in #if 0(131) : error C0000: syntax error, unexpected '!' at token "!" 0(135) : error C0000: syntax error, unexpected '}' at token "}" 0(139) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define SHADOW_PASS ===== shader string 6 ==== 12 #define COMMON_VIEW_LIB 13 #define DRW_RESOURCE_CHUNK_LEN 512 14 15 /* keep in sync with DRWManager.view_data */ 16 layout(std140) uniform viewBlock 17 { 18 /* Same order as DRWViewportMatrixType */ 19 mat4 ViewProjectionMatrix; 20 mat4 ViewProjectionMatrixInverse; 21 mat4 ViewMatrix; 22 mat4 ViewMatrixInverse; 23 mat4 ProjectionMatrix; 24 mat4 ProjectionMatrixInverse; 25 26 vec4 clipPlanes[6]; 27 28 /* TODO move it elsewhere. */ 29 vec4 CameraTexCoFactors; 30 }; 31 32 #ifdef world_clip_planes_calc_clip_distance 33 # undef world_clip_planes_calc_clip_distance 34 # define world_clip_planes_calc_clip_distance(p) \ 35 _world_clip_planes_calc_clip_distance(p, clipPlanes) 36 #endif 37 38 #ifdef COMMON_GLOBALS_LIB 39 float mul_project_m4_v3_zfac(in vec3 co) 40 { 41 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 42 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 43 } 44 #endif 45 46 /* Not the right place but need to be common to all overlay's. 47 * TODO Split to an overlay lib. */ 48 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 49 { 50 const float div = 1.0 / 255.0; 51 int a = int(mat[0][3]); 52 int b = int(mat[1][3]); 53 int c = int(mat[2][3]); 54 int d = int(mat[3][3]); 55 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 56 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 57 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 58 mat[3][3] = 1.0; 59 return mat; 60 } 61 62 /* Same here, Not the right place but need to be common to all overlay's. 63 * TODO Split to an overlay lib. */ 64 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 65 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 66 { 67 vec2 edge = edge_start - edge_pos; 68 float len = length(edge); 69 if (len > 0.0) { 70 edge /= len; 71 vec2 perp = vec2(-edge.y, edge.x); 72 float dist = dot(perp, frag_co - edge_start); 73 /* Add 0.1 to diffenrentiate with cleared pixels. */ 74 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 75 } 76 else { 77 /* Default line if the origin is perfectly aligned with a pixel. */ 78 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 79 } 80 } 81 82 uniform int resourceChunk; 83 84 #ifdef GPU_VERTEX_SHADER 85 # ifdef GL_ARB_shader_draw_parameters 86 # define baseInstance gl_BaseInstanceARB 87 # else /* no ARB_shader_draw_parameters */ 88 uniform int baseInstance; 89 # endif 90 91 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 92 /* When drawing instances of an object at the same position. */ 93 # define instanceId 0 94 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 95 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 96 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 97 * Ignore gl_InstanceID then. */ 98 # define instanceId 0 99 # else 100 # define instanceId gl_InstanceID 101 # endif 102 103 # define resource_id (baseInstance + instanceId) 104 105 /* Use this to declare and pass the value if 106 * the fragment shader uses the resource_id. */ 107 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 108 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 109 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 110 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 111 #endif 112 113 /* If used in a fragment / geometry shader, we pass 114 * resource_id as varying. */ 115 #ifdef GPU_GEOMETRY_SHADER 116 # define RESOURCE_ID_VARYING \ 117 flat out int resourceIDFrag; \ 118 flat in int resourceIDGeom[]; 119 120 # define resource_id resourceIDGeom 121 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 122 #endif 123 124 #ifdef GPU_FRAGMENT_SHADER 125 flat in int resourceIDFrag; 126 # define resource_id resourceIDFrag 127 #endif 128 129 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 130 !defined(INSTANCED_ATTRIB) 131 struct ObjectMatrices { 132 mat4 drw_modelMatrix; 133 mat4 drw_modelMatrixInverse; 134 }; 135 136 layout(std140) uniform modelBlock 137 { 138 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 139 }; 140 141 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 142 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 143 144 #else /* GPU_INTEL */ 145 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 146 * So for now we just force using the legacy path. */ 147 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 148 * and older amd driver on windows. */ 149 uniform mat4 ModelMatrix; 150 uniform mat4 ModelMatrixInverse; 151 #endif 152 153 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 154 155 /** Transform shortcuts. */ 156 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 157 * will always be decomposed in at least 2 matrix operation. */ 158 159 /** 160 * Some clarification: 161 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 162 * 163 * But since it is slow to multiply matrices we decompose it. Decomposing 164 * inversion and transposition both invert the product order leaving us with 165 * the same original order: 166 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 167 * 168 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 169 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 170 * ViewMatrix * transpose(ModelMatrixInverse) 171 **/ 172 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 173 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 174 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 175 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 176 177 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 178 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 179 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 180 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 181 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 182 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 183 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 184 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 185 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 186 187 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 188 * to make vertex shaders work. even if it's actually dead code. */ 189 #ifdef GPU_INTEL 190 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 191 #else 192 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 193 #endif 194 195 #define DRW_BASE_SELECTED (1 << 1) 196 #define DRW_BASE_FROM_DUPLI (1 << 2) 197 #define DRW_BASE_FROM_SET (1 << 3) 198 #define DRW_BASE_ACTIVE (1 << 4) 199 #define INFINITE 1000.0 200 201 uniform vec3 lightDirection = vec3(0.57, 0.57, -0.57); 202 uniform float lightDistance = 1e4; 203 204 in vec3 pos; 205 206 out VertexData 207 { 208 vec3 pos; /* local position */ 209 vec4 frontPosition; /* final ndc position */ 210 vec4 backPosition; 211 } 212 vData; 213 214 void main() 215 { 216 vData.pos = pos; 217 vData.frontPosition = point_object_to_ndc(pos); 218 vData.backPosition = point_object_to_ndc(pos + lightDirection * lightDistance); 219 } 0(129) : error C0105: Syntax error in #if 0(129) : error C0105: Syntax error in #if 0(130) : error C0000: syntax error, unexpected '!' at token "!" 0(134) : error C0000: syntax error, unexpected '}' at token "}" 0(138) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define SHADOW_FAIL 12 #define DOUBLE_MANIFOLD ===== shader string 6 ==== 13 #define COMMON_VIEW_LIB 14 #define DRW_RESOURCE_CHUNK_LEN 512 15 16 /* keep in sync with DRWManager.view_data */ 17 layout(std140) uniform viewBlock 18 { 19 /* Same order as DRWViewportMatrixType */ 20 mat4 ViewProjectionMatrix; 21 mat4 ViewProjectionMatrixInverse; 22 mat4 ViewMatrix; 23 mat4 ViewMatrixInverse; 24 mat4 ProjectionMatrix; 25 mat4 ProjectionMatrixInverse; 26 27 vec4 clipPlanes[6]; 28 29 /* TODO move it elsewhere. */ 30 vec4 CameraTexCoFactors; 31 }; 32 33 #ifdef world_clip_planes_calc_clip_distance 34 # undef world_clip_planes_calc_clip_distance 35 # define world_clip_planes_calc_clip_distance(p) \ 36 _world_clip_planes_calc_clip_distance(p, clipPlanes) 37 #endif 38 39 #ifdef COMMON_GLOBALS_LIB 40 float mul_project_m4_v3_zfac(in vec3 co) 41 { 42 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 43 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 44 } 45 #endif 46 47 /* Not the right place but need to be common to all overlay's. 48 * TODO Split to an overlay lib. */ 49 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 50 { 51 const float div = 1.0 / 255.0; 52 int a = int(mat[0][3]); 53 int b = int(mat[1][3]); 54 int c = int(mat[2][3]); 55 int d = int(mat[3][3]); 56 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 57 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 58 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 59 mat[3][3] = 1.0; 60 return mat; 61 } 62 63 /* Same here, Not the right place but need to be common to all overlay's. 64 * TODO Split to an overlay lib. */ 65 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 66 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 67 { 68 vec2 edge = edge_start - edge_pos; 69 float len = length(edge); 70 if (len > 0.0) { 71 edge /= len; 72 vec2 perp = vec2(-edge.y, edge.x); 73 float dist = dot(perp, frag_co - edge_start); 74 /* Add 0.1 to diffenrentiate with cleared pixels. */ 75 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 76 } 77 else { 78 /* Default line if the origin is perfectly aligned with a pixel. */ 79 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 80 } 81 } 82 83 uniform int resourceChunk; 84 85 #ifdef GPU_VERTEX_SHADER 86 # ifdef GL_ARB_shader_draw_parameters 87 # define baseInstance gl_BaseInstanceARB 88 # else /* no ARB_shader_draw_parameters */ 89 uniform int baseInstance; 90 # endif 91 92 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 93 /* When drawing instances of an object at the same position. */ 94 # define instanceId 0 95 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 96 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 97 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 98 * Ignore gl_InstanceID then. */ 99 # define instanceId 0 100 # else 101 # define instanceId gl_InstanceID 102 # endif 103 104 # define resource_id (baseInstance + instanceId) 105 106 /* Use this to declare and pass the value if 107 * the fragment shader uses the resource_id. */ 108 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 109 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 110 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 111 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 112 #endif 113 114 /* If used in a fragment / geometry shader, we pass 115 * resource_id as varying. */ 116 #ifdef GPU_GEOMETRY_SHADER 117 # define RESOURCE_ID_VARYING \ 118 flat out int resourceIDFrag; \ 119 flat in int resourceIDGeom[]; 120 121 # define resource_id resourceIDGeom 122 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 123 #endif 124 125 #ifdef GPU_FRAGMENT_SHADER 126 flat in int resourceIDFrag; 127 # define resource_id resourceIDFrag 128 #endif 129 130 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 131 !defined(INSTANCED_ATTRIB) 132 struct ObjectMatrices { 133 mat4 drw_modelMatrix; 134 mat4 drw_modelMatrixInverse; 135 }; 136 137 layout(std140) uniform modelBlock 138 { 139 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 140 }; 141 142 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 143 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 144 145 #else /* GPU_INTEL */ 146 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 147 * So for now we just force using the legacy path. */ 148 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 149 * and older amd driver on windows. */ 150 uniform mat4 ModelMatrix; 151 uniform mat4 ModelMatrixInverse; 152 #endif 153 154 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 155 156 /** Transform shortcuts. */ 157 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 158 * will always be decomposed in at least 2 matrix operation. */ 159 160 /** 161 * Some clarification: 162 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 163 * 164 * But since it is slow to multiply matrices we decompose it. Decomposing 165 * inversion and transposition both invert the product order leaving us with 166 * the same original order: 167 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 168 * 169 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 170 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 171 * ViewMatrix * transpose(ModelMatrixInverse) 172 **/ 173 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 174 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 175 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 176 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 177 178 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 179 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 180 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 181 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 182 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 183 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 184 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 185 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 186 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 187 188 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 189 * to make vertex shaders work. even if it's actually dead code. */ 190 #ifdef GPU_INTEL 191 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 192 #else 193 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 194 #endif 195 196 #define DRW_BASE_SELECTED (1 << 1) 197 #define DRW_BASE_FROM_DUPLI (1 << 2) 198 #define DRW_BASE_FROM_SET (1 << 3) 199 #define DRW_BASE_ACTIVE (1 << 4) 200 #define INFINITE 1000.0 201 202 uniform vec3 lightDirection = vec3(0.57, 0.57, -0.57); 203 uniform float lightDistance = 1e4; 204 205 in vec3 pos; 206 207 out VertexData 208 { 209 vec3 pos; /* local position */ 210 vec4 frontPosition; /* final ndc position */ 211 vec4 backPosition; 212 } 213 vData; 214 215 void main() 216 { 217 vData.pos = pos; 218 vData.frontPosition = point_object_to_ndc(pos); 219 vData.backPosition = point_object_to_ndc(pos + lightDirection * lightDistance); 220 } 0(130) : error C0105: Syntax error in #if 0(130) : error C0105: Syntax error in #if 0(131) : error C0000: syntax error, unexpected '!' at token "!" 0(135) : error C0000: syntax error, unexpected '}' at token "}" 0(139) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define SHADOW_FAIL ===== shader string 6 ==== 12 #define COMMON_VIEW_LIB 13 #define DRW_RESOURCE_CHUNK_LEN 512 14 15 /* keep in sync with DRWManager.view_data */ 16 layout(std140) uniform viewBlock 17 { 18 /* Same order as DRWViewportMatrixType */ 19 mat4 ViewProjectionMatrix; 20 mat4 ViewProjectionMatrixInverse; 21 mat4 ViewMatrix; 22 mat4 ViewMatrixInverse; 23 mat4 ProjectionMatrix; 24 mat4 ProjectionMatrixInverse; 25 26 vec4 clipPlanes[6]; 27 28 /* TODO move it elsewhere. */ 29 vec4 CameraTexCoFactors; 30 }; 31 32 #ifdef world_clip_planes_calc_clip_distance 33 # undef world_clip_planes_calc_clip_distance 34 # define world_clip_planes_calc_clip_distance(p) \ 35 _world_clip_planes_calc_clip_distance(p, clipPlanes) 36 #endif 37 38 #ifdef COMMON_GLOBALS_LIB 39 float mul_project_m4_v3_zfac(in vec3 co) 40 { 41 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 42 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 43 } 44 #endif 45 46 /* Not the right place but need to be common to all overlay's. 47 * TODO Split to an overlay lib. */ 48 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 49 { 50 const float div = 1.0 / 255.0; 51 int a = int(mat[0][3]); 52 int b = int(mat[1][3]); 53 int c = int(mat[2][3]); 54 int d = int(mat[3][3]); 55 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 56 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 57 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 58 mat[3][3] = 1.0; 59 return mat; 60 } 61 62 /* Same here, Not the right place but need to be common to all overlay's. 63 * TODO Split to an overlay lib. */ 64 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 65 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 66 { 67 vec2 edge = edge_start - edge_pos; 68 float len = length(edge); 69 if (len > 0.0) { 70 edge /= len; 71 vec2 perp = vec2(-edge.y, edge.x); 72 float dist = dot(perp, frag_co - edge_start); 73 /* Add 0.1 to diffenrentiate with cleared pixels. */ 74 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 75 } 76 else { 77 /* Default line if the origin is perfectly aligned with a pixel. */ 78 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 79 } 80 } 81 82 uniform int resourceChunk; 83 84 #ifdef GPU_VERTEX_SHADER 85 # ifdef GL_ARB_shader_draw_parameters 86 # define baseInstance gl_BaseInstanceARB 87 # else /* no ARB_shader_draw_parameters */ 88 uniform int baseInstance; 89 # endif 90 91 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 92 /* When drawing instances of an object at the same position. */ 93 # define instanceId 0 94 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 95 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 96 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 97 * Ignore gl_InstanceID then. */ 98 # define instanceId 0 99 # else 100 # define instanceId gl_InstanceID 101 # endif 102 103 # define resource_id (baseInstance + instanceId) 104 105 /* Use this to declare and pass the value if 106 * the fragment shader uses the resource_id. */ 107 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 108 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 109 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 110 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 111 #endif 112 113 /* If used in a fragment / geometry shader, we pass 114 * resource_id as varying. */ 115 #ifdef GPU_GEOMETRY_SHADER 116 # define RESOURCE_ID_VARYING \ 117 flat out int resourceIDFrag; \ 118 flat in int resourceIDGeom[]; 119 120 # define resource_id resourceIDGeom 121 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 122 #endif 123 124 #ifdef GPU_FRAGMENT_SHADER 125 flat in int resourceIDFrag; 126 # define resource_id resourceIDFrag 127 #endif 128 129 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 130 !defined(INSTANCED_ATTRIB) 131 struct ObjectMatrices { 132 mat4 drw_modelMatrix; 133 mat4 drw_modelMatrixInverse; 134 }; 135 136 layout(std140) uniform modelBlock 137 { 138 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 139 }; 140 141 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 142 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 143 144 #else /* GPU_INTEL */ 145 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 146 * So for now we just force using the legacy path. */ 147 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 148 * and older amd driver on windows. */ 149 uniform mat4 ModelMatrix; 150 uniform mat4 ModelMatrixInverse; 151 #endif 152 153 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 154 155 /** Transform shortcuts. */ 156 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 157 * will always be decomposed in at least 2 matrix operation. */ 158 159 /** 160 * Some clarification: 161 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 162 * 163 * But since it is slow to multiply matrices we decompose it. Decomposing 164 * inversion and transposition both invert the product order leaving us with 165 * the same original order: 166 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 167 * 168 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 169 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 170 * ViewMatrix * transpose(ModelMatrixInverse) 171 **/ 172 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 173 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 174 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 175 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 176 177 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 178 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 179 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 180 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 181 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 182 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 183 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 184 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 185 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 186 187 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 188 * to make vertex shaders work. even if it's actually dead code. */ 189 #ifdef GPU_INTEL 190 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 191 #else 192 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 193 #endif 194 195 #define DRW_BASE_SELECTED (1 << 1) 196 #define DRW_BASE_FROM_DUPLI (1 << 2) 197 #define DRW_BASE_FROM_SET (1 << 3) 198 #define DRW_BASE_ACTIVE (1 << 4) 199 #define INFINITE 1000.0 200 201 uniform vec3 lightDirection = vec3(0.57, 0.57, -0.57); 202 uniform float lightDistance = 1e4; 203 204 in vec3 pos; 205 206 out VertexData 207 { 208 vec3 pos; /* local position */ 209 vec4 frontPosition; /* final ndc position */ 210 vec4 backPosition; 211 } 212 vData; 213 214 void main() 215 { 216 vData.pos = pos; 217 vData.frontPosition = point_object_to_ndc(pos); 218 vData.backPosition = point_object_to_ndc(pos + lightDirection * lightDistance); 219 } 0(129) : error C0105: Syntax error in #if 0(129) : error C0105: Syntax error in #if 0(130) : error C0000: syntax error, unexpected '!' at token "!" 0(134) : error C0000: syntax error, unexpected '}' at token "}" 0(138) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define SHADOW_FAIL 12 #define DOUBLE_MANIFOLD ===== shader string 6 ==== 13 #define COMMON_VIEW_LIB 14 #define DRW_RESOURCE_CHUNK_LEN 512 15 16 /* keep in sync with DRWManager.view_data */ 17 layout(std140) uniform viewBlock 18 { 19 /* Same order as DRWViewportMatrixType */ 20 mat4 ViewProjectionMatrix; 21 mat4 ViewProjectionMatrixInverse; 22 mat4 ViewMatrix; 23 mat4 ViewMatrixInverse; 24 mat4 ProjectionMatrix; 25 mat4 ProjectionMatrixInverse; 26 27 vec4 clipPlanes[6]; 28 29 /* TODO move it elsewhere. */ 30 vec4 CameraTexCoFactors; 31 }; 32 33 #ifdef world_clip_planes_calc_clip_distance 34 # undef world_clip_planes_calc_clip_distance 35 # define world_clip_planes_calc_clip_distance(p) \ 36 _world_clip_planes_calc_clip_distance(p, clipPlanes) 37 #endif 38 39 #ifdef COMMON_GLOBALS_LIB 40 float mul_project_m4_v3_zfac(in vec3 co) 41 { 42 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 43 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 44 } 45 #endif 46 47 /* Not the right place but need to be common to all overlay's. 48 * TODO Split to an overlay lib. */ 49 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 50 { 51 const float div = 1.0 / 255.0; 52 int a = int(mat[0][3]); 53 int b = int(mat[1][3]); 54 int c = int(mat[2][3]); 55 int d = int(mat[3][3]); 56 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 57 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 58 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 59 mat[3][3] = 1.0; 60 return mat; 61 } 62 63 /* Same here, Not the right place but need to be common to all overlay's. 64 * TODO Split to an overlay lib. */ 65 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 66 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 67 { 68 vec2 edge = edge_start - edge_pos; 69 float len = length(edge); 70 if (len > 0.0) { 71 edge /= len; 72 vec2 perp = vec2(-edge.y, edge.x); 73 float dist = dot(perp, frag_co - edge_start); 74 /* Add 0.1 to diffenrentiate with cleared pixels. */ 75 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 76 } 77 else { 78 /* Default line if the origin is perfectly aligned with a pixel. */ 79 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 80 } 81 } 82 83 uniform int resourceChunk; 84 85 #ifdef GPU_VERTEX_SHADER 86 # ifdef GL_ARB_shader_draw_parameters 87 # define baseInstance gl_BaseInstanceARB 88 # else /* no ARB_shader_draw_parameters */ 89 uniform int baseInstance; 90 # endif 91 92 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 93 /* When drawing instances of an object at the same position. */ 94 # define instanceId 0 95 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 96 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 97 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 98 * Ignore gl_InstanceID then. */ 99 # define instanceId 0 100 # else 101 # define instanceId gl_InstanceID 102 # endif 103 104 # define resource_id (baseInstance + instanceId) 105 106 /* Use this to declare and pass the value if 107 * the fragment shader uses the resource_id. */ 108 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 109 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 110 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 111 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 112 #endif 113 114 /* If used in a fragment / geometry shader, we pass 115 * resource_id as varying. */ 116 #ifdef GPU_GEOMETRY_SHADER 117 # define RESOURCE_ID_VARYING \ 118 flat out int resourceIDFrag; \ 119 flat in int resourceIDGeom[]; 120 121 # define resource_id resourceIDGeom 122 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 123 #endif 124 125 #ifdef GPU_FRAGMENT_SHADER 126 flat in int resourceIDFrag; 127 # define resource_id resourceIDFrag 128 #endif 129 130 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 131 !defined(INSTANCED_ATTRIB) 132 struct ObjectMatrices { 133 mat4 drw_modelMatrix; 134 mat4 drw_modelMatrixInverse; 135 }; 136 137 layout(std140) uniform modelBlock 138 { 139 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 140 }; 141 142 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 143 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 144 145 #else /* GPU_INTEL */ 146 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 147 * So for now we just force using the legacy path. */ 148 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 149 * and older amd driver on windows. */ 150 uniform mat4 ModelMatrix; 151 uniform mat4 ModelMatrixInverse; 152 #endif 153 154 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 155 156 /** Transform shortcuts. */ 157 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 158 * will always be decomposed in at least 2 matrix operation. */ 159 160 /** 161 * Some clarification: 162 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 163 * 164 * But since it is slow to multiply matrices we decompose it. Decomposing 165 * inversion and transposition both invert the product order leaving us with 166 * the same original order: 167 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 168 * 169 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 170 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 171 * ViewMatrix * transpose(ModelMatrixInverse) 172 **/ 173 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 174 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 175 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 176 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 177 178 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 179 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 180 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 181 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 182 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 183 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 184 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 185 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 186 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 187 188 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 189 * to make vertex shaders work. even if it's actually dead code. */ 190 #ifdef GPU_INTEL 191 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 192 #else 193 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 194 #endif 195 196 #define DRW_BASE_SELECTED (1 << 1) 197 #define DRW_BASE_FROM_DUPLI (1 << 2) 198 #define DRW_BASE_FROM_SET (1 << 3) 199 #define DRW_BASE_ACTIVE (1 << 4) 200 #define INFINITE 1000.0 201 202 uniform vec3 lightDirection = vec3(0.57, 0.57, -0.57); 203 uniform float lightDistance = 1e4; 204 205 in vec3 pos; 206 207 out VertexData 208 { 209 vec3 pos; /* local position */ 210 vec4 frontPosition; /* final ndc position */ 211 vec4 backPosition; 212 } 213 vData; 214 215 void main() 216 { 217 vData.pos = pos; 218 vData.frontPosition = point_object_to_ndc(pos); 219 vData.backPosition = point_object_to_ndc(pos + lightDirection * lightDistance); 220 } 0(130) : error C0105: Syntax error in #if 0(130) : error C0105: Syntax error in #if 0(131) : error C0000: syntax error, unexpected '!' at token "!" 0(135) : error C0000: syntax error, unexpected '}' at token "}" 0(139) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define SHADOW_FAIL ===== shader string 6 ==== 12 #define COMMON_VIEW_LIB 13 #define DRW_RESOURCE_CHUNK_LEN 512 14 15 /* keep in sync with DRWManager.view_data */ 16 layout(std140) uniform viewBlock 17 { 18 /* Same order as DRWViewportMatrixType */ 19 mat4 ViewProjectionMatrix; 20 mat4 ViewProjectionMatrixInverse; 21 mat4 ViewMatrix; 22 mat4 ViewMatrixInverse; 23 mat4 ProjectionMatrix; 24 mat4 ProjectionMatrixInverse; 25 26 vec4 clipPlanes[6]; 27 28 /* TODO move it elsewhere. */ 29 vec4 CameraTexCoFactors; 30 }; 31 32 #ifdef world_clip_planes_calc_clip_distance 33 # undef world_clip_planes_calc_clip_distance 34 # define world_clip_planes_calc_clip_distance(p) \ 35 _world_clip_planes_calc_clip_distance(p, clipPlanes) 36 #endif 37 38 #ifdef COMMON_GLOBALS_LIB 39 float mul_project_m4_v3_zfac(in vec3 co) 40 { 41 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 42 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 43 } 44 #endif 45 46 /* Not the right place but need to be common to all overlay's. 47 * TODO Split to an overlay lib. */ 48 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 49 { 50 const float div = 1.0 / 255.0; 51 int a = int(mat[0][3]); 52 int b = int(mat[1][3]); 53 int c = int(mat[2][3]); 54 int d = int(mat[3][3]); 55 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 56 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 57 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 58 mat[3][3] = 1.0; 59 return mat; 60 } 61 62 /* Same here, Not the right place but need to be common to all overlay's. 63 * TODO Split to an overlay lib. */ 64 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 65 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 66 { 67 vec2 edge = edge_start - edge_pos; 68 float len = length(edge); 69 if (len > 0.0) { 70 edge /= len; 71 vec2 perp = vec2(-edge.y, edge.x); 72 float dist = dot(perp, frag_co - edge_start); 73 /* Add 0.1 to diffenrentiate with cleared pixels. */ 74 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 75 } 76 else { 77 /* Default line if the origin is perfectly aligned with a pixel. */ 78 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 79 } 80 } 81 82 uniform int resourceChunk; 83 84 #ifdef GPU_VERTEX_SHADER 85 # ifdef GL_ARB_shader_draw_parameters 86 # define baseInstance gl_BaseInstanceARB 87 # else /* no ARB_shader_draw_parameters */ 88 uniform int baseInstance; 89 # endif 90 91 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 92 /* When drawing instances of an object at the same position. */ 93 # define instanceId 0 94 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 95 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 96 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 97 * Ignore gl_InstanceID then. */ 98 # define instanceId 0 99 # else 100 # define instanceId gl_InstanceID 101 # endif 102 103 # define resource_id (baseInstance + instanceId) 104 105 /* Use this to declare and pass the value if 106 * the fragment shader uses the resource_id. */ 107 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 108 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 109 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 110 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 111 #endif 112 113 /* If used in a fragment / geometry shader, we pass 114 * resource_id as varying. */ 115 #ifdef GPU_GEOMETRY_SHADER 116 # define RESOURCE_ID_VARYING \ 117 flat out int resourceIDFrag; \ 118 flat in int resourceIDGeom[]; 119 120 # define resource_id resourceIDGeom 121 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 122 #endif 123 124 #ifdef GPU_FRAGMENT_SHADER 125 flat in int resourceIDFrag; 126 # define resource_id resourceIDFrag 127 #endif 128 129 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 130 !defined(INSTANCED_ATTRIB) 131 struct ObjectMatrices { 132 mat4 drw_modelMatrix; 133 mat4 drw_modelMatrixInverse; 134 }; 135 136 layout(std140) uniform modelBlock 137 { 138 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 139 }; 140 141 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 142 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 143 144 #else /* GPU_INTEL */ 145 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 146 * So for now we just force using the legacy path. */ 147 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 148 * and older amd driver on windows. */ 149 uniform mat4 ModelMatrix; 150 uniform mat4 ModelMatrixInverse; 151 #endif 152 153 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 154 155 /** Transform shortcuts. */ 156 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 157 * will always be decomposed in at least 2 matrix operation. */ 158 159 /** 160 * Some clarification: 161 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 162 * 163 * But since it is slow to multiply matrices we decompose it. Decomposing 164 * inversion and transposition both invert the product order leaving us with 165 * the same original order: 166 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 167 * 168 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 169 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 170 * ViewMatrix * transpose(ModelMatrixInverse) 171 **/ 172 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 173 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 174 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 175 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 176 177 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 178 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 179 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 180 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 181 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 182 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 183 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 184 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 185 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 186 187 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 188 * to make vertex shaders work. even if it's actually dead code. */ 189 #ifdef GPU_INTEL 190 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 191 #else 192 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 193 #endif 194 195 #define DRW_BASE_SELECTED (1 << 1) 196 #define DRW_BASE_FROM_DUPLI (1 << 2) 197 #define DRW_BASE_FROM_SET (1 << 3) 198 #define DRW_BASE_ACTIVE (1 << 4) 199 #define INFINITE 1000.0 200 201 uniform vec3 lightDirection = vec3(0.57, 0.57, -0.57); 202 uniform float lightDistance = 1e4; 203 204 in vec3 pos; 205 206 out VertexData 207 { 208 vec3 pos; /* local position */ 209 vec4 frontPosition; /* final ndc position */ 210 vec4 backPosition; 211 } 212 vData; 213 214 void main() 215 { 216 vData.pos = pos; 217 vData.frontPosition = point_object_to_ndc(pos); 218 vData.backPosition = point_object_to_ndc(pos + lightDirection * lightDistance); 219 } 0(129) : error C0105: Syntax error in #if 0(129) : error C0105: Syntax error in #if 0(130) : error C0000: syntax error, unexpected '!' at token "!" 0(134) : error C0000: syntax error, unexpected '}' at token "}" 0(138) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS ===== shader string 6 ==== 15 #define COMMON_VIEW_LIB 16 #define DRW_RESOURCE_CHUNK_LEN 512 17 18 /* keep in sync with DRWManager.view_data */ 19 layout(std140) uniform viewBlock 20 { 21 /* Same order as DRWViewportMatrixType */ 22 mat4 ViewProjectionMatrix; 23 mat4 ViewProjectionMatrixInverse; 24 mat4 ViewMatrix; 25 mat4 ViewMatrixInverse; 26 mat4 ProjectionMatrix; 27 mat4 ProjectionMatrixInverse; 28 29 vec4 clipPlanes[6]; 30 31 /* TODO move it elsewhere. */ 32 vec4 CameraTexCoFactors; 33 }; 34 35 #ifdef world_clip_planes_calc_clip_distance 36 # undef world_clip_planes_calc_clip_distance 37 # define world_clip_planes_calc_clip_distance(p) \ 38 _world_clip_planes_calc_clip_distance(p, clipPlanes) 39 #endif 40 41 #ifdef COMMON_GLOBALS_LIB 42 float mul_project_m4_v3_zfac(in vec3 co) 43 { 44 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 45 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 46 } 47 #endif 48 49 /* Not the right place but need to be common to all overlay's. 50 * TODO Split to an overlay lib. */ 51 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 52 { 53 const float div = 1.0 / 255.0; 54 int a = int(mat[0][3]); 55 int b = int(mat[1][3]); 56 int c = int(mat[2][3]); 57 int d = int(mat[3][3]); 58 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 59 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 60 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 61 mat[3][3] = 1.0; 62 return mat; 63 } 64 65 /* Same here, Not the right place but need to be common to all overlay's. 66 * TODO Split to an overlay lib. */ 67 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 68 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 69 { 70 vec2 edge = edge_start - edge_pos; 71 float len = length(edge); 72 if (len > 0.0) { 73 edge /= len; 74 vec2 perp = vec2(-edge.y, edge.x); 75 float dist = dot(perp, frag_co - edge_start); 76 /* Add 0.1 to diffenrentiate with cleared pixels. */ 77 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 78 } 79 else { 80 /* Default line if the origin is perfectly aligned with a pixel. */ 81 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 82 } 83 } 84 85 uniform int resourceChunk; 86 87 #ifdef GPU_VERTEX_SHADER 88 # ifdef GL_ARB_shader_draw_parameters 89 # define baseInstance gl_BaseInstanceARB 90 # else /* no ARB_shader_draw_parameters */ 91 uniform int baseInstance; 92 # endif 93 94 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 95 /* When drawing instances of an object at the same position. */ 96 # define instanceId 0 97 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 98 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 99 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 100 * Ignore gl_InstanceID then. */ 101 # define instanceId 0 102 # else 103 # define instanceId gl_InstanceID 104 # endif 105 106 # define resource_id (baseInstance + instanceId) 107 108 /* Use this to declare and pass the value if 109 * the fragment shader uses the resource_id. */ 110 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 111 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 112 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 113 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 114 #endif 115 116 /* If used in a fragment / geometry shader, we pass 117 * resource_id as varying. */ 118 #ifdef GPU_GEOMETRY_SHADER 119 # define RESOURCE_ID_VARYING \ 120 flat out int resourceIDFrag; \ 121 flat in int resourceIDGeom[]; 122 123 # define resource_id resourceIDGeom 124 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 125 #endif 126 127 #ifdef GPU_FRAGMENT_SHADER 128 flat in int resourceIDFrag; 129 # define resource_id resourceIDFrag 130 #endif 131 132 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 133 !defined(INSTANCED_ATTRIB) 134 struct ObjectMatrices { 135 mat4 drw_modelMatrix; 136 mat4 drw_modelMatrixInverse; 137 }; 138 139 layout(std140) uniform modelBlock 140 { 141 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 142 }; 143 144 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 145 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 146 147 #else /* GPU_INTEL */ 148 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 149 * So for now we just force using the legacy path. */ 150 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 151 * and older amd driver on windows. */ 152 uniform mat4 ModelMatrix; 153 uniform mat4 ModelMatrixInverse; 154 #endif 155 156 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 157 158 /** Transform shortcuts. */ 159 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 160 * will always be decomposed in at least 2 matrix operation. */ 161 162 /** 163 * Some clarification: 164 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 165 * 166 * But since it is slow to multiply matrices we decompose it. Decomposing 167 * inversion and transposition both invert the product order leaving us with 168 * the same original order: 169 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 170 * 171 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 172 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 173 * ViewMatrix * transpose(ModelMatrixInverse) 174 **/ 175 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 176 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 177 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 178 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 179 180 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 181 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 182 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 183 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 184 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 185 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 186 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 187 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 188 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 189 190 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 191 * to make vertex shaders work. even if it's actually dead code. */ 192 #ifdef GPU_INTEL 193 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 194 #else 195 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 196 #endif 197 198 #define DRW_BASE_SELECTED (1 << 1) 199 #define DRW_BASE_FROM_DUPLI (1 << 2) 200 #define DRW_BASE_FROM_SET (1 << 3) 201 #define DRW_BASE_ACTIVE (1 << 4) 202 203 #ifndef HAIR_SHADER 204 in vec3 pos; 205 in vec3 nor; 206 in vec2 au; /* active texture layer */ 207 # ifdef V3D_SHADING_VERTEX_COLOR 208 in vec3 ac; /* active color */ 209 # endif 210 # define uv au 211 #else /* HAIR_SHADER */ 212 # ifdef V3D_SHADING_TEXTURE_COLOR 213 uniform samplerBuffer au; /* active texture layer */ 214 # endif 215 flat out float hair_rand; 216 #endif /* HAIR_SHADER */ 217 218 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 219 out vec3 normal_viewport; 220 #endif 221 222 #ifdef V3D_SHADING_TEXTURE_COLOR 223 out vec2 uv_interp; 224 #endif 225 #ifdef V3D_SHADING_VERTEX_COLOR 226 out vec3 vertexColor; 227 #endif 228 229 #ifdef OBJECT_ID_PASS_ENABLED 230 RESOURCE_ID_VARYING 231 #endif 232 233 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 234 float integer_noise(int n) 235 { 236 n = (n >> 13) ^ n; 237 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 238 return (float(nn) / 1073741824.0); 239 } 240 241 #ifdef V3D_SHADING_VERTEX_COLOR 242 vec3 srgb_to_linear_attr(vec3 c) 243 { 244 c = max(c, vec3(0.0)); 245 vec3 c1 = c * (1.0 / 12.92); 246 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 247 return mix(c1, c2, step(vec3(0.04045), c)); 248 } 249 #endif 250 251 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 252 { 253 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 254 vec3 nor = cross(tan, binor); 255 nor = normalize(mix(nor, -tan, rand * 0.1)); 256 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 257 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 258 nor = nor * sin_theta + binor * cos_theta; 259 return nor; 260 } 261 262 void main() 263 { 264 #ifdef HAIR_SHADER 265 # ifdef V3D_SHADING_TEXTURE_COLOR 266 vec2 uv = hair_get_customdata_vec2(au); 267 # endif 268 float time, thick_time, thickness; 269 vec3 world_pos, tan, binor; 270 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 271 ModelMatrixInverse, 272 ViewMatrixInverse[3].xyz, 273 ViewMatrixInverse[2].xyz, 274 world_pos, 275 tan, 276 binor, 277 time, 278 thickness, 279 thick_time); 280 281 hair_rand = integer_noise(hair_get_strand_id()); 282 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 283 #else 284 vec3 world_pos = point_object_to_world(pos); 285 #endif 286 gl_Position = point_world_to_ndc(world_pos); 287 288 #ifdef V3D_SHADING_TEXTURE_COLOR 289 uv_interp = uv; 290 #endif 291 292 #ifdef V3D_SHADING_VERTEX_COLOR 293 # ifndef HAIR_SHADER 294 vertexColor = srgb_to_linear_attr(ac); 295 # endif 296 #endif 297 298 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 299 # ifndef HAIR_SHADER 300 normal_viewport = normal_object_to_view(nor); 301 normal_viewport = normalize(normal_viewport); 302 # else 303 normal_viewport = normal_world_to_view(nor); 304 # endif 305 #endif 306 307 #ifdef OBJECT_ID_PASS_ENABLED 308 PASS_RESOURCE_ID 309 #endif 310 311 #ifdef USE_WORLD_CLIP_PLANES 312 world_clip_planes_calc_clip_distance(world_pos); 313 #endif 314 } 0(132) : error C0105: Syntax error in #if 0(132) : error C0105: Syntax error in #if 0(133) : error C0000: syntax error, unexpected '!' at token "!" 0(137) : error C0000: syntax error, unexpected '}' at token "}" 0(141) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS 15 #define HAIR_SHADER ===== shader string 6 ==== 16 /** 17 * Library to create hairs dynamically from control points. 18 * This is less bandwidth intensive than fetching the vertex attributes 19 * but does more ALU work per vertex. This also reduces the amount 20 * of data the CPU has to precompute and transfer for each update. 21 */ 22 23 /** 24 * hairStrandsRes: Number of points per hair strand. 25 * 2 - no subdivision 26 * 3+ - 1 or more interpolated points per hair. 27 */ 28 uniform int hairStrandsRes = 8; 29 30 /** 31 * hairThicknessRes : Subdiv around the hair. 32 * 1 - Wire Hair: Only one pixel thick, independent of view distance. 33 * 2 - Polystrip Hair: Correct width, flat if camera is parallel. 34 * 3+ - Cylinder Hair: Massive calculation but potentially perfect. Still need proper support. 35 */ 36 uniform int hairThicknessRes = 1; 37 38 /* Hair thickness shape. */ 39 uniform float hairRadRoot = 0.01; 40 uniform float hairRadTip = 0.0; 41 uniform float hairRadShape = 0.5; 42 uniform bool hairCloseTip = true; 43 44 uniform mat4 hairDupliMatrix; 45 46 /* -- Per control points -- */ 47 uniform samplerBuffer hairPointBuffer; /* RGBA32F */ 48 #define point_position xyz 49 #define point_time w /* Position along the hair length */ 50 51 /* -- Per strands data -- */ 52 uniform usamplerBuffer hairStrandBuffer; /* R32UI */ 53 uniform usamplerBuffer hairStrandSegBuffer; /* R16UI */ 54 55 /* Not used, use one buffer per uv layer */ 56 // uniform samplerBuffer hairUVBuffer; /* RG32F */ 57 // uniform samplerBuffer hairColBuffer; /* RGBA16 linear color */ 58 59 /* -- Subdivision stage -- */ 60 /** 61 * We use a transform feedback to preprocess the strands and add more subdivision to it. 62 * For the moment these are simple smooth interpolation but one could hope to see the full 63 * children particle modifiers being evaluated at this stage. 64 * 65 * If no more subdivision is needed, we can skip this step. 66 */ 67 68 #ifdef HAIR_PHASE_SUBDIV 69 int hair_get_base_id(float local_time, int strand_segments, out float interp_time) 70 { 71 float time_per_strand_seg = 1.0 / float(strand_segments); 72 73 float ratio = local_time / time_per_strand_seg; 74 interp_time = fract(ratio); 75 76 return int(ratio); 77 } 78 79 void hair_get_interp_attrs( 80 out vec4 data0, out vec4 data1, out vec4 data2, out vec4 data3, out float interp_time) 81 { 82 float local_time = float(gl_VertexID % hairStrandsRes) / float(hairStrandsRes - 1); 83 84 int hair_id = gl_VertexID / hairStrandsRes; 85 int strand_offset = int(texelFetch(hairStrandBuffer, hair_id).x); 86 int strand_segments = int(texelFetch(hairStrandSegBuffer, hair_id).x); 87 88 int id = hair_get_base_id(local_time, strand_segments, interp_time); 89 90 int ofs_id = id + strand_offset; 91 92 data0 = texelFetch(hairPointBuffer, ofs_id - 1); 93 data1 = texelFetch(hairPointBuffer, ofs_id); 94 data2 = texelFetch(hairPointBuffer, ofs_id + 1); 95 data3 = texelFetch(hairPointBuffer, ofs_id + 2); 96 97 if (id <= 0) { 98 /* root points. Need to reconstruct previous data. */ 99 data0 = data1 * 2.0 - data2; 100 } 101 if (id + 1 >= strand_segments) { 102 /* tip points. Need to reconstruct next data. */ 103 data3 = data2 * 2.0 - data1; 104 } 105 } 106 #endif 107 108 /* -- Drawing stage -- */ 109 /** 110 * For final drawing, the vertex index and the number of vertex per segment 111 */ 112 113 #ifndef HAIR_PHASE_SUBDIV 114 int hair_get_strand_id(void) 115 { 116 return gl_VertexID / (hairStrandsRes * hairThicknessRes); 117 } 118 119 int hair_get_base_id(void) 120 { 121 return gl_VertexID / hairThicknessRes; 122 } 123 124 /* Copied from cycles. */ 125 float hair_shaperadius(float shape, float root, float tip, float time) 126 { 127 float radius = 1.0 - time; 128 129 if (shape < 0.0) { 130 radius = pow(radius, 1.0 + shape); 131 } 132 else { 133 radius = pow(radius, 1.0 / (1.0 - shape)); 134 } 135 136 if (hairCloseTip && (time > 0.99)) { 137 return 0.0; 138 } 139 140 return (radius * (root - tip)) + tip; 141 } 142 143 # ifdef OS_MAC 144 in float dummy; 145 # endif 146 147 void hair_get_pos_tan_binor_time(bool is_persp, 148 mat4 invmodel_mat, 149 vec3 camera_pos, 150 vec3 camera_z, 151 out vec3 wpos, 152 out vec3 wtan, 153 out vec3 wbinor, 154 out float time, 155 out float thickness, 156 out float thick_time) 157 { 158 int id = hair_get_base_id(); 159 vec4 data = texelFetch(hairPointBuffer, id); 160 wpos = data.point_position; 161 time = data.point_time; 162 163 # ifdef OS_MAC 164 /* Generate a dummy read to avoid the driver bug with shaders having no 165 * vertex reads on macOS (T60171) */ 166 wpos.y += dummy * 0.0; 167 # endif 168 169 if (time == 0.0) { 170 /* Hair root */ 171 wtan = texelFetch(hairPointBuffer, id + 1).point_position - wpos; 172 } 173 else { 174 wtan = wpos - texelFetch(hairPointBuffer, id - 1).point_position; 175 } 176 177 wpos = (hairDupliMatrix * vec4(wpos, 1.0)).xyz; 178 wtan = -normalize(mat3(hairDupliMatrix) * wtan); 179 180 vec3 camera_vec = (is_persp) ? camera_pos - wpos : camera_z; 181 wbinor = normalize(cross(camera_vec, wtan)); 182 183 thickness = hair_shaperadius(hairRadShape, hairRadRoot, hairRadTip, time); 184 185 if (hairThicknessRes > 1) { 186 thick_time = float(gl_VertexID % hairThicknessRes) / float(hairThicknessRes - 1); 187 thick_time = thickness * (thick_time * 2.0 - 1.0); 188 189 /* Take object scale into account. 190 * NOTE: This only works fine with uniform scaling. */ 191 float scale = 1.0 / length(mat3(invmodel_mat) * wbinor); 192 193 wpos += wbinor * thick_time * scale; 194 } 195 } 196 197 vec2 hair_get_customdata_vec2(const samplerBuffer cd_buf) 198 { 199 int id = hair_get_strand_id(); 200 return texelFetch(cd_buf, id).rg; 201 } 202 203 vec3 hair_get_customdata_vec3(const samplerBuffer cd_buf) 204 { 205 int id = hair_get_strand_id(); 206 return texelFetch(cd_buf, id).rgb; 207 } 208 209 vec4 hair_get_customdata_vec4(const samplerBuffer cd_buf) 210 { 211 int id = hair_get_strand_id(); 212 return texelFetch(cd_buf, id).rgba; 213 } 214 215 vec3 hair_get_strand_pos(void) 216 { 217 int id = hair_get_strand_id() * hairStrandsRes; 218 return texelFetch(hairPointBuffer, id).point_position; 219 } 220 221 #endif 222 #define COMMON_VIEW_LIB 223 #define DRW_RESOURCE_CHUNK_LEN 512 224 225 /* keep in sync with DRWManager.view_data */ 226 layout(std140) uniform viewBlock 227 { 228 /* Same order as DRWViewportMatrixType */ 229 mat4 ViewProjectionMatrix; 230 mat4 ViewProjectionMatrixInverse; 231 mat4 ViewMatrix; 232 mat4 ViewMatrixInverse; 233 mat4 ProjectionMatrix; 234 mat4 ProjectionMatrixInverse; 235 236 vec4 clipPlanes[6]; 237 238 /* TODO move it elsewhere. */ 239 vec4 CameraTexCoFactors; 240 }; 241 242 #ifdef world_clip_planes_calc_clip_distance 243 # undef world_clip_planes_calc_clip_distance 244 # define world_clip_planes_calc_clip_distance(p) \ 245 _world_clip_planes_calc_clip_distance(p, clipPlanes) 246 #endif 247 248 #ifdef COMMON_GLOBALS_LIB 249 float mul_project_m4_v3_zfac(in vec3 co) 250 { 251 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 252 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 253 } 254 #endif 255 256 /* Not the right place but need to be common to all overlay's. 257 * TODO Split to an overlay lib. */ 258 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 259 { 260 const float div = 1.0 / 255.0; 261 int a = int(mat[0][3]); 262 int b = int(mat[1][3]); 263 int c = int(mat[2][3]); 264 int d = int(mat[3][3]); 265 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 266 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 267 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 268 mat[3][3] = 1.0; 269 return mat; 270 } 271 272 /* Same here, Not the right place but need to be common to all overlay's. 273 * TODO Split to an overlay lib. */ 274 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 275 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 276 { 277 vec2 edge = edge_start - edge_pos; 278 float len = length(edge); 279 if (len > 0.0) { 280 edge /= len; 281 vec2 perp = vec2(-edge.y, edge.x); 282 float dist = dot(perp, frag_co - edge_start); 283 /* Add 0.1 to diffenrentiate with cleared pixels. */ 284 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 285 } 286 else { 287 /* Default line if the origin is perfectly aligned with a pixel. */ 288 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 289 } 290 } 291 292 uniform int resourceChunk; 293 294 #ifdef GPU_VERTEX_SHADER 295 # ifdef GL_ARB_shader_draw_parameters 296 # define baseInstance gl_BaseInstanceARB 297 # else /* no ARB_shader_draw_parameters */ 298 uniform int baseInstance; 299 # endif 300 301 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 302 /* When drawing instances of an object at the same position. */ 303 # define instanceId 0 304 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 305 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 306 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 307 * Ignore gl_InstanceID then. */ 308 # define instanceId 0 309 # else 310 # define instanceId gl_InstanceID 311 # endif 312 313 # define resource_id (baseInstance + instanceId) 314 315 /* Use this to declare and pass the value if 316 * the fragment shader uses the resource_id. */ 317 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 318 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 319 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 320 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 321 #endif 322 323 /* If used in a fragment / geometry shader, we pass 324 * resource_id as varying. */ 325 #ifdef GPU_GEOMETRY_SHADER 326 # define RESOURCE_ID_VARYING \ 327 flat out int resourceIDFrag; \ 328 flat in int resourceIDGeom[]; 329 330 # define resource_id resourceIDGeom 331 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 332 #endif 333 334 #ifdef GPU_FRAGMENT_SHADER 335 flat in int resourceIDFrag; 336 # define resource_id resourceIDFrag 337 #endif 338 339 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 340 !defined(INSTANCED_ATTRIB) 341 struct ObjectMatrices { 342 mat4 drw_modelMatrix; 343 mat4 drw_modelMatrixInverse; 344 }; 345 346 layout(std140) uniform modelBlock 347 { 348 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 349 }; 350 351 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 352 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 353 354 #else /* GPU_INTEL */ 355 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 356 * So for now we just force using the legacy path. */ 357 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 358 * and older amd driver on windows. */ 359 uniform mat4 ModelMatrix; 360 uniform mat4 ModelMatrixInverse; 361 #endif 362 363 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 364 365 /** Transform shortcuts. */ 366 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 367 * will always be decomposed in at least 2 matrix operation. */ 368 369 /** 370 * Some clarification: 371 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 372 * 373 * But since it is slow to multiply matrices we decompose it. Decomposing 374 * inversion and transposition both invert the product order leaving us with 375 * the same original order: 376 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 377 * 378 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 379 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 380 * ViewMatrix * transpose(ModelMatrixInverse) 381 **/ 382 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 383 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 384 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 385 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 386 387 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 388 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 389 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 390 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 391 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 392 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 393 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 394 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 395 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 396 397 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 398 * to make vertex shaders work. even if it's actually dead code. */ 399 #ifdef GPU_INTEL 400 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 401 #else 402 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 403 #endif 404 405 #define DRW_BASE_SELECTED (1 << 1) 406 #define DRW_BASE_FROM_DUPLI (1 << 2) 407 #define DRW_BASE_FROM_SET (1 << 3) 408 #define DRW_BASE_ACTIVE (1 << 4) 409 410 #ifndef HAIR_SHADER 411 in vec3 pos; 412 in vec3 nor; 413 in vec2 au; /* active texture layer */ 414 # ifdef V3D_SHADING_VERTEX_COLOR 415 in vec3 ac; /* active color */ 416 # endif 417 # define uv au 418 #else /* HAIR_SHADER */ 419 # ifdef V3D_SHADING_TEXTURE_COLOR 420 uniform samplerBuffer au; /* active texture layer */ 421 # endif 422 flat out float hair_rand; 423 #endif /* HAIR_SHADER */ 424 425 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 426 out vec3 normal_viewport; 427 #endif 428 429 #ifdef V3D_SHADING_TEXTURE_COLOR 430 out vec2 uv_interp; 431 #endif 432 #ifdef V3D_SHADING_VERTEX_COLOR 433 out vec3 vertexColor; 434 #endif 435 436 #ifdef OBJECT_ID_PASS_ENABLED 437 RESOURCE_ID_VARYING 438 #endif 439 440 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 441 float integer_noise(int n) 442 { 443 n = (n >> 13) ^ n; 444 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 445 return (float(nn) / 1073741824.0); 446 } 447 448 #ifdef V3D_SHADING_VERTEX_COLOR 449 vec3 srgb_to_linear_attr(vec3 c) 450 { 451 c = max(c, vec3(0.0)); 452 vec3 c1 = c * (1.0 / 12.92); 453 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 454 return mix(c1, c2, step(vec3(0.04045), c)); 455 } 456 #endif 457 458 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 459 { 460 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 461 vec3 nor = cross(tan, binor); 462 nor = normalize(mix(nor, -tan, rand * 0.1)); 463 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 464 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 465 nor = nor * sin_theta + binor * cos_theta; 466 return nor; 467 } 468 469 void main() 470 { 471 #ifdef HAIR_SHADER 472 # ifdef V3D_SHADING_TEXTURE_COLOR 473 vec2 uv = hair_get_customdata_vec2(au); 474 # endif 475 float time, thick_time, thickness; 476 vec3 world_pos, tan, binor; 477 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 478 ModelMatrixInverse, 479 ViewMatrixInverse[3].xyz, 480 ViewMatrixInverse[2].xyz, 481 world_pos, 482 tan, 483 binor, 484 time, 485 thickness, 486 thick_time); 487 488 hair_rand = integer_noise(hair_get_strand_id()); 489 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 490 #else 491 vec3 world_pos = point_object_to_world(pos); 492 #endif 493 gl_Position = point_world_to_ndc(world_pos); 494 495 #ifdef V3D_SHADING_TEXTURE_COLOR 496 uv_interp = uv; 497 #endif 498 499 #ifdef V3D_SHADING_VERTEX_COLOR 500 # ifndef HAIR_SHADER 501 vertexColor = srgb_to_linear_attr(ac); 502 # endif 503 #endif 504 505 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 506 # ifndef HAIR_SHADER 507 normal_viewport = normal_object_to_view(nor); 508 normal_viewport = normalize(normal_viewport); 509 # else 510 normal_viewport = normal_world_to_view(nor); 511 # endif 512 #endif 513 514 #ifdef OBJECT_ID_PASS_ENABLED 515 PASS_RESOURCE_ID 516 #endif 517 518 #ifdef USE_WORLD_CLIP_PLANES 519 world_clip_planes_calc_clip_distance(world_pos); 520 #endif 521 } 0(339) : error C0105: Syntax error in #if 0(339) : error C0105: Syntax error in #if 0(340) : error C0000: syntax error, unexpected '!' at token "!" 0(344) : error C0000: syntax error, unexpected '}' at token "}" 0(348) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS ===== shader string 6 ==== 15 #define COMMON_VIEW_LIB 16 #define DRW_RESOURCE_CHUNK_LEN 512 17 18 /* keep in sync with DRWManager.view_data */ 19 layout(std140) uniform viewBlock 20 { 21 /* Same order as DRWViewportMatrixType */ 22 mat4 ViewProjectionMatrix; 23 mat4 ViewProjectionMatrixInverse; 24 mat4 ViewMatrix; 25 mat4 ViewMatrixInverse; 26 mat4 ProjectionMatrix; 27 mat4 ProjectionMatrixInverse; 28 29 vec4 clipPlanes[6]; 30 31 /* TODO move it elsewhere. */ 32 vec4 CameraTexCoFactors; 33 }; 34 35 #ifdef world_clip_planes_calc_clip_distance 36 # undef world_clip_planes_calc_clip_distance 37 # define world_clip_planes_calc_clip_distance(p) \ 38 _world_clip_planes_calc_clip_distance(p, clipPlanes) 39 #endif 40 41 #ifdef COMMON_GLOBALS_LIB 42 float mul_project_m4_v3_zfac(in vec3 co) 43 { 44 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 45 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 46 } 47 #endif 48 49 /* Not the right place but need to be common to all overlay's. 50 * TODO Split to an overlay lib. */ 51 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 52 { 53 const float div = 1.0 / 255.0; 54 int a = int(mat[0][3]); 55 int b = int(mat[1][3]); 56 int c = int(mat[2][3]); 57 int d = int(mat[3][3]); 58 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 59 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 60 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 61 mat[3][3] = 1.0; 62 return mat; 63 } 64 65 /* Same here, Not the right place but need to be common to all overlay's. 66 * TODO Split to an overlay lib. */ 67 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 68 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 69 { 70 vec2 edge = edge_start - edge_pos; 71 float len = length(edge); 72 if (len > 0.0) { 73 edge /= len; 74 vec2 perp = vec2(-edge.y, edge.x); 75 float dist = dot(perp, frag_co - edge_start); 76 /* Add 0.1 to diffenrentiate with cleared pixels. */ 77 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 78 } 79 else { 80 /* Default line if the origin is perfectly aligned with a pixel. */ 81 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 82 } 83 } 84 85 uniform int resourceChunk; 86 87 #ifdef GPU_VERTEX_SHADER 88 # ifdef GL_ARB_shader_draw_parameters 89 # define baseInstance gl_BaseInstanceARB 90 # else /* no ARB_shader_draw_parameters */ 91 uniform int baseInstance; 92 # endif 93 94 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 95 /* When drawing instances of an object at the same position. */ 96 # define instanceId 0 97 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 98 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 99 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 100 * Ignore gl_InstanceID then. */ 101 # define instanceId 0 102 # else 103 # define instanceId gl_InstanceID 104 # endif 105 106 # define resource_id (baseInstance + instanceId) 107 108 /* Use this to declare and pass the value if 109 * the fragment shader uses the resource_id. */ 110 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 111 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 112 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 113 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 114 #endif 115 116 /* If used in a fragment / geometry shader, we pass 117 * resource_id as varying. */ 118 #ifdef GPU_GEOMETRY_SHADER 119 # define RESOURCE_ID_VARYING \ 120 flat out int resourceIDFrag; \ 121 flat in int resourceIDGeom[]; 122 123 # define resource_id resourceIDGeom 124 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 125 #endif 126 127 #ifdef GPU_FRAGMENT_SHADER 128 flat in int resourceIDFrag; 129 # define resource_id resourceIDFrag 130 #endif 131 132 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 133 !defined(INSTANCED_ATTRIB) 134 struct ObjectMatrices { 135 mat4 drw_modelMatrix; 136 mat4 drw_modelMatrixInverse; 137 }; 138 139 layout(std140) uniform modelBlock 140 { 141 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 142 }; 143 144 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 145 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 146 147 #else /* GPU_INTEL */ 148 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 149 * So for now we just force using the legacy path. */ 150 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 151 * and older amd driver on windows. */ 152 uniform mat4 ModelMatrix; 153 uniform mat4 ModelMatrixInverse; 154 #endif 155 156 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 157 158 /** Transform shortcuts. */ 159 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 160 * will always be decomposed in at least 2 matrix operation. */ 161 162 /** 163 * Some clarification: 164 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 165 * 166 * But since it is slow to multiply matrices we decompose it. Decomposing 167 * inversion and transposition both invert the product order leaving us with 168 * the same original order: 169 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 170 * 171 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 172 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 173 * ViewMatrix * transpose(ModelMatrixInverse) 174 **/ 175 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 176 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 177 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 178 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 179 180 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 181 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 182 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 183 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 184 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 185 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 186 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 187 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 188 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 189 190 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 191 * to make vertex shaders work. even if it's actually dead code. */ 192 #ifdef GPU_INTEL 193 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 194 #else 195 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 196 #endif 197 198 #define DRW_BASE_SELECTED (1 << 1) 199 #define DRW_BASE_FROM_DUPLI (1 << 2) 200 #define DRW_BASE_FROM_SET (1 << 3) 201 #define DRW_BASE_ACTIVE (1 << 4) 202 203 #ifndef HAIR_SHADER 204 in vec3 pos; 205 in vec3 nor; 206 in vec2 au; /* active texture layer */ 207 # ifdef V3D_SHADING_VERTEX_COLOR 208 in vec3 ac; /* active color */ 209 # endif 210 # define uv au 211 #else /* HAIR_SHADER */ 212 # ifdef V3D_SHADING_TEXTURE_COLOR 213 uniform samplerBuffer au; /* active texture layer */ 214 # endif 215 flat out float hair_rand; 216 #endif /* HAIR_SHADER */ 217 218 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 219 out vec3 normal_viewport; 220 #endif 221 222 #ifdef V3D_SHADING_TEXTURE_COLOR 223 out vec2 uv_interp; 224 #endif 225 #ifdef V3D_SHADING_VERTEX_COLOR 226 out vec3 vertexColor; 227 #endif 228 229 #ifdef OBJECT_ID_PASS_ENABLED 230 RESOURCE_ID_VARYING 231 #endif 232 233 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 234 float integer_noise(int n) 235 { 236 n = (n >> 13) ^ n; 237 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 238 return (float(nn) / 1073741824.0); 239 } 240 241 #ifdef V3D_SHADING_VERTEX_COLOR 242 vec3 srgb_to_linear_attr(vec3 c) 243 { 244 c = max(c, vec3(0.0)); 245 vec3 c1 = c * (1.0 / 12.92); 246 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 247 return mix(c1, c2, step(vec3(0.04045), c)); 248 } 249 #endif 250 251 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 252 { 253 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 254 vec3 nor = cross(tan, binor); 255 nor = normalize(mix(nor, -tan, rand * 0.1)); 256 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 257 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 258 nor = nor * sin_theta + binor * cos_theta; 259 return nor; 260 } 261 262 void main() 263 { 264 #ifdef HAIR_SHADER 265 # ifdef V3D_SHADING_TEXTURE_COLOR 266 vec2 uv = hair_get_customdata_vec2(au); 267 # endif 268 float time, thick_time, thickness; 269 vec3 world_pos, tan, binor; 270 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 271 ModelMatrixInverse, 272 ViewMatrixInverse[3].xyz, 273 ViewMatrixInverse[2].xyz, 274 world_pos, 275 tan, 276 binor, 277 time, 278 thickness, 279 thick_time); 280 281 hair_rand = integer_noise(hair_get_strand_id()); 282 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 283 #else 284 vec3 world_pos = point_object_to_world(pos); 285 #endif 286 gl_Position = point_world_to_ndc(world_pos); 287 288 #ifdef V3D_SHADING_TEXTURE_COLOR 289 uv_interp = uv; 290 #endif 291 292 #ifdef V3D_SHADING_VERTEX_COLOR 293 # ifndef HAIR_SHADER 294 vertexColor = srgb_to_linear_attr(ac); 295 # endif 296 #endif 297 298 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 299 # ifndef HAIR_SHADER 300 normal_viewport = normal_object_to_view(nor); 301 normal_viewport = normalize(normal_viewport); 302 # else 303 normal_viewport = normal_world_to_view(nor); 304 # endif 305 #endif 306 307 #ifdef OBJECT_ID_PASS_ENABLED 308 PASS_RESOURCE_ID 309 #endif 310 311 #ifdef USE_WORLD_CLIP_PLANES 312 world_clip_planes_calc_clip_distance(world_pos); 313 #endif 314 } 0(132) : error C0105: Syntax error in #if 0(132) : error C0105: Syntax error in #if 0(133) : error C0000: syntax error, unexpected '!' at token "!" 0(137) : error C0000: syntax error, unexpected '}' at token "}" 0(141) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS 15 #define HAIR_SHADER ===== shader string 6 ==== 16 /** 17 * Library to create hairs dynamically from control points. 18 * This is less bandwidth intensive than fetching the vertex attributes 19 * but does more ALU work per vertex. This also reduces the amount 20 * of data the CPU has to precompute and transfer for each update. 21 */ 22 23 /** 24 * hairStrandsRes: Number of points per hair strand. 25 * 2 - no subdivision 26 * 3+ - 1 or more interpolated points per hair. 27 */ 28 uniform int hairStrandsRes = 8; 29 30 /** 31 * hairThicknessRes : Subdiv around the hair. 32 * 1 - Wire Hair: Only one pixel thick, independent of view distance. 33 * 2 - Polystrip Hair: Correct width, flat if camera is parallel. 34 * 3+ - Cylinder Hair: Massive calculation but potentially perfect. Still need proper support. 35 */ 36 uniform int hairThicknessRes = 1; 37 38 /* Hair thickness shape. */ 39 uniform float hairRadRoot = 0.01; 40 uniform float hairRadTip = 0.0; 41 uniform float hairRadShape = 0.5; 42 uniform bool hairCloseTip = true; 43 44 uniform mat4 hairDupliMatrix; 45 46 /* -- Per control points -- */ 47 uniform samplerBuffer hairPointBuffer; /* RGBA32F */ 48 #define point_position xyz 49 #define point_time w /* Position along the hair length */ 50 51 /* -- Per strands data -- */ 52 uniform usamplerBuffer hairStrandBuffer; /* R32UI */ 53 uniform usamplerBuffer hairStrandSegBuffer; /* R16UI */ 54 55 /* Not used, use one buffer per uv layer */ 56 // uniform samplerBuffer hairUVBuffer; /* RG32F */ 57 // uniform samplerBuffer hairColBuffer; /* RGBA16 linear color */ 58 59 /* -- Subdivision stage -- */ 60 /** 61 * We use a transform feedback to preprocess the strands and add more subdivision to it. 62 * For the moment these are simple smooth interpolation but one could hope to see the full 63 * children particle modifiers being evaluated at this stage. 64 * 65 * If no more subdivision is needed, we can skip this step. 66 */ 67 68 #ifdef HAIR_PHASE_SUBDIV 69 int hair_get_base_id(float local_time, int strand_segments, out float interp_time) 70 { 71 float time_per_strand_seg = 1.0 / float(strand_segments); 72 73 float ratio = local_time / time_per_strand_seg; 74 interp_time = fract(ratio); 75 76 return int(ratio); 77 } 78 79 void hair_get_interp_attrs( 80 out vec4 data0, out vec4 data1, out vec4 data2, out vec4 data3, out float interp_time) 81 { 82 float local_time = float(gl_VertexID % hairStrandsRes) / float(hairStrandsRes - 1); 83 84 int hair_id = gl_VertexID / hairStrandsRes; 85 int strand_offset = int(texelFetch(hairStrandBuffer, hair_id).x); 86 int strand_segments = int(texelFetch(hairStrandSegBuffer, hair_id).x); 87 88 int id = hair_get_base_id(local_time, strand_segments, interp_time); 89 90 int ofs_id = id + strand_offset; 91 92 data0 = texelFetch(hairPointBuffer, ofs_id - 1); 93 data1 = texelFetch(hairPointBuffer, ofs_id); 94 data2 = texelFetch(hairPointBuffer, ofs_id + 1); 95 data3 = texelFetch(hairPointBuffer, ofs_id + 2); 96 97 if (id <= 0) { 98 /* root points. Need to reconstruct previous data. */ 99 data0 = data1 * 2.0 - data2; 100 } 101 if (id + 1 >= strand_segments) { 102 /* tip points. Need to reconstruct next data. */ 103 data3 = data2 * 2.0 - data1; 104 } 105 } 106 #endif 107 108 /* -- Drawing stage -- */ 109 /** 110 * For final drawing, the vertex index and the number of vertex per segment 111 */ 112 113 #ifndef HAIR_PHASE_SUBDIV 114 int hair_get_strand_id(void) 115 { 116 return gl_VertexID / (hairStrandsRes * hairThicknessRes); 117 } 118 119 int hair_get_base_id(void) 120 { 121 return gl_VertexID / hairThicknessRes; 122 } 123 124 /* Copied from cycles. */ 125 float hair_shaperadius(float shape, float root, float tip, float time) 126 { 127 float radius = 1.0 - time; 128 129 if (shape < 0.0) { 130 radius = pow(radius, 1.0 + shape); 131 } 132 else { 133 radius = pow(radius, 1.0 / (1.0 - shape)); 134 } 135 136 if (hairCloseTip && (time > 0.99)) { 137 return 0.0; 138 } 139 140 return (radius * (root - tip)) + tip; 141 } 142 143 # ifdef OS_MAC 144 in float dummy; 145 # endif 146 147 void hair_get_pos_tan_binor_time(bool is_persp, 148 mat4 invmodel_mat, 149 vec3 camera_pos, 150 vec3 camera_z, 151 out vec3 wpos, 152 out vec3 wtan, 153 out vec3 wbinor, 154 out float time, 155 out float thickness, 156 out float thick_time) 157 { 158 int id = hair_get_base_id(); 159 vec4 data = texelFetch(hairPointBuffer, id); 160 wpos = data.point_position; 161 time = data.point_time; 162 163 # ifdef OS_MAC 164 /* Generate a dummy read to avoid the driver bug with shaders having no 165 * vertex reads on macOS (T60171) */ 166 wpos.y += dummy * 0.0; 167 # endif 168 169 if (time == 0.0) { 170 /* Hair root */ 171 wtan = texelFetch(hairPointBuffer, id + 1).point_position - wpos; 172 } 173 else { 174 wtan = wpos - texelFetch(hairPointBuffer, id - 1).point_position; 175 } 176 177 wpos = (hairDupliMatrix * vec4(wpos, 1.0)).xyz; 178 wtan = -normalize(mat3(hairDupliMatrix) * wtan); 179 180 vec3 camera_vec = (is_persp) ? camera_pos - wpos : camera_z; 181 wbinor = normalize(cross(camera_vec, wtan)); 182 183 thickness = hair_shaperadius(hairRadShape, hairRadRoot, hairRadTip, time); 184 185 if (hairThicknessRes > 1) { 186 thick_time = float(gl_VertexID % hairThicknessRes) / float(hairThicknessRes - 1); 187 thick_time = thickness * (thick_time * 2.0 - 1.0); 188 189 /* Take object scale into account. 190 * NOTE: This only works fine with uniform scaling. */ 191 float scale = 1.0 / length(mat3(invmodel_mat) * wbinor); 192 193 wpos += wbinor * thick_time * scale; 194 } 195 } 196 197 vec2 hair_get_customdata_vec2(const samplerBuffer cd_buf) 198 { 199 int id = hair_get_strand_id(); 200 return texelFetch(cd_buf, id).rg; 201 } 202 203 vec3 hair_get_customdata_vec3(const samplerBuffer cd_buf) 204 { 205 int id = hair_get_strand_id(); 206 return texelFetch(cd_buf, id).rgb; 207 } 208 209 vec4 hair_get_customdata_vec4(const samplerBuffer cd_buf) 210 { 211 int id = hair_get_strand_id(); 212 return texelFetch(cd_buf, id).rgba; 213 } 214 215 vec3 hair_get_strand_pos(void) 216 { 217 int id = hair_get_strand_id() * hairStrandsRes; 218 return texelFetch(hairPointBuffer, id).point_position; 219 } 220 221 #endif 222 #define COMMON_VIEW_LIB 223 #define DRW_RESOURCE_CHUNK_LEN 512 224 225 /* keep in sync with DRWManager.view_data */ 226 layout(std140) uniform viewBlock 227 { 228 /* Same order as DRWViewportMatrixType */ 229 mat4 ViewProjectionMatrix; 230 mat4 ViewProjectionMatrixInverse; 231 mat4 ViewMatrix; 232 mat4 ViewMatrixInverse; 233 mat4 ProjectionMatrix; 234 mat4 ProjectionMatrixInverse; 235 236 vec4 clipPlanes[6]; 237 238 /* TODO move it elsewhere. */ 239 vec4 CameraTexCoFactors; 240 }; 241 242 #ifdef world_clip_planes_calc_clip_distance 243 # undef world_clip_planes_calc_clip_distance 244 # define world_clip_planes_calc_clip_distance(p) \ 245 _world_clip_planes_calc_clip_distance(p, clipPlanes) 246 #endif 247 248 #ifdef COMMON_GLOBALS_LIB 249 float mul_project_m4_v3_zfac(in vec3 co) 250 { 251 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 252 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 253 } 254 #endif 255 256 /* Not the right place but need to be common to all overlay's. 257 * TODO Split to an overlay lib. */ 258 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 259 { 260 const float div = 1.0 / 255.0; 261 int a = int(mat[0][3]); 262 int b = int(mat[1][3]); 263 int c = int(mat[2][3]); 264 int d = int(mat[3][3]); 265 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 266 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 267 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 268 mat[3][3] = 1.0; 269 return mat; 270 } 271 272 /* Same here, Not the right place but need to be common to all overlay's. 273 * TODO Split to an overlay lib. */ 274 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 275 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 276 { 277 vec2 edge = edge_start - edge_pos; 278 float len = length(edge); 279 if (len > 0.0) { 280 edge /= len; 281 vec2 perp = vec2(-edge.y, edge.x); 282 float dist = dot(perp, frag_co - edge_start); 283 /* Add 0.1 to diffenrentiate with cleared pixels. */ 284 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 285 } 286 else { 287 /* Default line if the origin is perfectly aligned with a pixel. */ 288 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 289 } 290 } 291 292 uniform int resourceChunk; 293 294 #ifdef GPU_VERTEX_SHADER 295 # ifdef GL_ARB_shader_draw_parameters 296 # define baseInstance gl_BaseInstanceARB 297 # else /* no ARB_shader_draw_parameters */ 298 uniform int baseInstance; 299 # endif 300 301 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 302 /* When drawing instances of an object at the same position. */ 303 # define instanceId 0 304 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 305 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 306 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 307 * Ignore gl_InstanceID then. */ 308 # define instanceId 0 309 # else 310 # define instanceId gl_InstanceID 311 # endif 312 313 # define resource_id (baseInstance + instanceId) 314 315 /* Use this to declare and pass the value if 316 * the fragment shader uses the resource_id. */ 317 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 318 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 319 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 320 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 321 #endif 322 323 /* If used in a fragment / geometry shader, we pass 324 * resource_id as varying. */ 325 #ifdef GPU_GEOMETRY_SHADER 326 # define RESOURCE_ID_VARYING \ 327 flat out int resourceIDFrag; \ 328 flat in int resourceIDGeom[]; 329 330 # define resource_id resourceIDGeom 331 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 332 #endif 333 334 #ifdef GPU_FRAGMENT_SHADER 335 flat in int resourceIDFrag; 336 # define resource_id resourceIDFrag 337 #endif 338 339 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 340 !defined(INSTANCED_ATTRIB) 341 struct ObjectMatrices { 342 mat4 drw_modelMatrix; 343 mat4 drw_modelMatrixInverse; 344 }; 345 346 layout(std140) uniform modelBlock 347 { 348 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 349 }; 350 351 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 352 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 353 354 #else /* GPU_INTEL */ 355 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 356 * So for now we just force using the legacy path. */ 357 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 358 * and older amd driver on windows. */ 359 uniform mat4 ModelMatrix; 360 uniform mat4 ModelMatrixInverse; 361 #endif 362 363 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 364 365 /** Transform shortcuts. */ 366 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 367 * will always be decomposed in at least 2 matrix operation. */ 368 369 /** 370 * Some clarification: 371 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 372 * 373 * But since it is slow to multiply matrices we decompose it. Decomposing 374 * inversion and transposition both invert the product order leaving us with 375 * the same original order: 376 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 377 * 378 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 379 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 380 * ViewMatrix * transpose(ModelMatrixInverse) 381 **/ 382 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 383 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 384 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 385 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 386 387 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 388 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 389 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 390 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 391 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 392 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 393 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 394 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 395 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 396 397 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 398 * to make vertex shaders work. even if it's actually dead code. */ 399 #ifdef GPU_INTEL 400 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 401 #else 402 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 403 #endif 404 405 #define DRW_BASE_SELECTED (1 << 1) 406 #define DRW_BASE_FROM_DUPLI (1 << 2) 407 #define DRW_BASE_FROM_SET (1 << 3) 408 #define DRW_BASE_ACTIVE (1 << 4) 409 410 #ifndef HAIR_SHADER 411 in vec3 pos; 412 in vec3 nor; 413 in vec2 au; /* active texture layer */ 414 # ifdef V3D_SHADING_VERTEX_COLOR 415 in vec3 ac; /* active color */ 416 # endif 417 # define uv au 418 #else /* HAIR_SHADER */ 419 # ifdef V3D_SHADING_TEXTURE_COLOR 420 uniform samplerBuffer au; /* active texture layer */ 421 # endif 422 flat out float hair_rand; 423 #endif /* HAIR_SHADER */ 424 425 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 426 out vec3 normal_viewport; 427 #endif 428 429 #ifdef V3D_SHADING_TEXTURE_COLOR 430 out vec2 uv_interp; 431 #endif 432 #ifdef V3D_SHADING_VERTEX_COLOR 433 out vec3 vertexColor; 434 #endif 435 436 #ifdef OBJECT_ID_PASS_ENABLED 437 RESOURCE_ID_VARYING 438 #endif 439 440 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 441 float integer_noise(int n) 442 { 443 n = (n >> 13) ^ n; 444 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 445 return (float(nn) / 1073741824.0); 446 } 447 448 #ifdef V3D_SHADING_VERTEX_COLOR 449 vec3 srgb_to_linear_attr(vec3 c) 450 { 451 c = max(c, vec3(0.0)); 452 vec3 c1 = c * (1.0 / 12.92); 453 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 454 return mix(c1, c2, step(vec3(0.04045), c)); 455 } 456 #endif 457 458 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 459 { 460 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 461 vec3 nor = cross(tan, binor); 462 nor = normalize(mix(nor, -tan, rand * 0.1)); 463 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 464 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 465 nor = nor * sin_theta + binor * cos_theta; 466 return nor; 467 } 468 469 void main() 470 { 471 #ifdef HAIR_SHADER 472 # ifdef V3D_SHADING_TEXTURE_COLOR 473 vec2 uv = hair_get_customdata_vec2(au); 474 # endif 475 float time, thick_time, thickness; 476 vec3 world_pos, tan, binor; 477 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 478 ModelMatrixInverse, 479 ViewMatrixInverse[3].xyz, 480 ViewMatrixInverse[2].xyz, 481 world_pos, 482 tan, 483 binor, 484 time, 485 thickness, 486 thick_time); 487 488 hair_rand = integer_noise(hair_get_strand_id()); 489 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 490 #else 491 vec3 world_pos = point_object_to_world(pos); 492 #endif 493 gl_Position = point_world_to_ndc(world_pos); 494 495 #ifdef V3D_SHADING_TEXTURE_COLOR 496 uv_interp = uv; 497 #endif 498 499 #ifdef V3D_SHADING_VERTEX_COLOR 500 # ifndef HAIR_SHADER 501 vertexColor = srgb_to_linear_attr(ac); 502 # endif 503 #endif 504 505 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 506 # ifndef HAIR_SHADER 507 normal_viewport = normal_object_to_view(nor); 508 normal_viewport = normalize(normal_viewport); 509 # else 510 normal_viewport = normal_world_to_view(nor); 511 # endif 512 #endif 513 514 #ifdef OBJECT_ID_PASS_ENABLED 515 PASS_RESOURCE_ID 516 #endif 517 518 #ifdef USE_WORLD_CLIP_PLANES 519 world_clip_planes_calc_clip_distance(world_pos); 520 #endif 521 } 0(339) : error C0105: Syntax error in #if 0(339) : error C0105: Syntax error in #if 0(340) : error C0000: syntax error, unexpected '!' at token "!" 0(344) : error C0000: syntax error, unexpected '}' at token "}" 0(348) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define V3D_SHADING_TEXTURE_COLOR 15 #define WORKBENCH_ENCODE_NORMALS ===== shader string 6 ==== 16 #define COMMON_VIEW_LIB 17 #define DRW_RESOURCE_CHUNK_LEN 512 18 19 /* keep in sync with DRWManager.view_data */ 20 layout(std140) uniform viewBlock 21 { 22 /* Same order as DRWViewportMatrixType */ 23 mat4 ViewProjectionMatrix; 24 mat4 ViewProjectionMatrixInverse; 25 mat4 ViewMatrix; 26 mat4 ViewMatrixInverse; 27 mat4 ProjectionMatrix; 28 mat4 ProjectionMatrixInverse; 29 30 vec4 clipPlanes[6]; 31 32 /* TODO move it elsewhere. */ 33 vec4 CameraTexCoFactors; 34 }; 35 36 #ifdef world_clip_planes_calc_clip_distance 37 # undef world_clip_planes_calc_clip_distance 38 # define world_clip_planes_calc_clip_distance(p) \ 39 _world_clip_planes_calc_clip_distance(p, clipPlanes) 40 #endif 41 42 #ifdef COMMON_GLOBALS_LIB 43 float mul_project_m4_v3_zfac(in vec3 co) 44 { 45 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 46 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 47 } 48 #endif 49 50 /* Not the right place but need to be common to all overlay's. 51 * TODO Split to an overlay lib. */ 52 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 53 { 54 const float div = 1.0 / 255.0; 55 int a = int(mat[0][3]); 56 int b = int(mat[1][3]); 57 int c = int(mat[2][3]); 58 int d = int(mat[3][3]); 59 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 60 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 61 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 62 mat[3][3] = 1.0; 63 return mat; 64 } 65 66 /* Same here, Not the right place but need to be common to all overlay's. 67 * TODO Split to an overlay lib. */ 68 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 69 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 70 { 71 vec2 edge = edge_start - edge_pos; 72 float len = length(edge); 73 if (len > 0.0) { 74 edge /= len; 75 vec2 perp = vec2(-edge.y, edge.x); 76 float dist = dot(perp, frag_co - edge_start); 77 /* Add 0.1 to diffenrentiate with cleared pixels. */ 78 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 79 } 80 else { 81 /* Default line if the origin is perfectly aligned with a pixel. */ 82 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 83 } 84 } 85 86 uniform int resourceChunk; 87 88 #ifdef GPU_VERTEX_SHADER 89 # ifdef GL_ARB_shader_draw_parameters 90 # define baseInstance gl_BaseInstanceARB 91 # else /* no ARB_shader_draw_parameters */ 92 uniform int baseInstance; 93 # endif 94 95 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 96 /* When drawing instances of an object at the same position. */ 97 # define instanceId 0 98 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 99 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 100 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 101 * Ignore gl_InstanceID then. */ 102 # define instanceId 0 103 # else 104 # define instanceId gl_InstanceID 105 # endif 106 107 # define resource_id (baseInstance + instanceId) 108 109 /* Use this to declare and pass the value if 110 * the fragment shader uses the resource_id. */ 111 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 112 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 113 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 114 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 115 #endif 116 117 /* If used in a fragment / geometry shader, we pass 118 * resource_id as varying. */ 119 #ifdef GPU_GEOMETRY_SHADER 120 # define RESOURCE_ID_VARYING \ 121 flat out int resourceIDFrag; \ 122 flat in int resourceIDGeom[]; 123 124 # define resource_id resourceIDGeom 125 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 126 #endif 127 128 #ifdef GPU_FRAGMENT_SHADER 129 flat in int resourceIDFrag; 130 # define resource_id resourceIDFrag 131 #endif 132 133 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 134 !defined(INSTANCED_ATTRIB) 135 struct ObjectMatrices { 136 mat4 drw_modelMatrix; 137 mat4 drw_modelMatrixInverse; 138 }; 139 140 layout(std140) uniform modelBlock 141 { 142 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 143 }; 144 145 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 146 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 147 148 #else /* GPU_INTEL */ 149 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 150 * So for now we just force using the legacy path. */ 151 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 152 * and older amd driver on windows. */ 153 uniform mat4 ModelMatrix; 154 uniform mat4 ModelMatrixInverse; 155 #endif 156 157 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 158 159 /** Transform shortcuts. */ 160 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 161 * will always be decomposed in at least 2 matrix operation. */ 162 163 /** 164 * Some clarification: 165 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 166 * 167 * But since it is slow to multiply matrices we decompose it. Decomposing 168 * inversion and transposition both invert the product order leaving us with 169 * the same original order: 170 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 171 * 172 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 173 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 174 * ViewMatrix * transpose(ModelMatrixInverse) 175 **/ 176 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 177 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 178 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 179 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 180 181 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 182 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 183 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 184 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 185 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 186 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 187 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 188 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 189 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 190 191 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 192 * to make vertex shaders work. even if it's actually dead code. */ 193 #ifdef GPU_INTEL 194 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 195 #else 196 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 197 #endif 198 199 #define DRW_BASE_SELECTED (1 << 1) 200 #define DRW_BASE_FROM_DUPLI (1 << 2) 201 #define DRW_BASE_FROM_SET (1 << 3) 202 #define DRW_BASE_ACTIVE (1 << 4) 203 204 #ifndef HAIR_SHADER 205 in vec3 pos; 206 in vec3 nor; 207 in vec2 au; /* active texture layer */ 208 # ifdef V3D_SHADING_VERTEX_COLOR 209 in vec3 ac; /* active color */ 210 # endif 211 # define uv au 212 #else /* HAIR_SHADER */ 213 # ifdef V3D_SHADING_TEXTURE_COLOR 214 uniform samplerBuffer au; /* active texture layer */ 215 # endif 216 flat out float hair_rand; 217 #endif /* HAIR_SHADER */ 218 219 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 220 out vec3 normal_viewport; 221 #endif 222 223 #ifdef V3D_SHADING_TEXTURE_COLOR 224 out vec2 uv_interp; 225 #endif 226 #ifdef V3D_SHADING_VERTEX_COLOR 227 out vec3 vertexColor; 228 #endif 229 230 #ifdef OBJECT_ID_PASS_ENABLED 231 RESOURCE_ID_VARYING 232 #endif 233 234 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 235 float integer_noise(int n) 236 { 237 n = (n >> 13) ^ n; 238 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 239 return (float(nn) / 1073741824.0); 240 } 241 242 #ifdef V3D_SHADING_VERTEX_COLOR 243 vec3 srgb_to_linear_attr(vec3 c) 244 { 245 c = max(c, vec3(0.0)); 246 vec3 c1 = c * (1.0 / 12.92); 247 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 248 return mix(c1, c2, step(vec3(0.04045), c)); 249 } 250 #endif 251 252 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 253 { 254 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 255 vec3 nor = cross(tan, binor); 256 nor = normalize(mix(nor, -tan, rand * 0.1)); 257 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 258 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 259 nor = nor * sin_theta + binor * cos_theta; 260 return nor; 261 } 262 263 void main() 264 { 265 #ifdef HAIR_SHADER 266 # ifdef V3D_SHADING_TEXTURE_COLOR 267 vec2 uv = hair_get_customdata_vec2(au); 268 # endif 269 float time, thick_time, thickness; 270 vec3 world_pos, tan, binor; 271 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 272 ModelMatrixInverse, 273 ViewMatrixInverse[3].xyz, 274 ViewMatrixInverse[2].xyz, 275 world_pos, 276 tan, 277 binor, 278 time, 279 thickness, 280 thick_time); 281 282 hair_rand = integer_noise(hair_get_strand_id()); 283 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 284 #else 285 vec3 world_pos = point_object_to_world(pos); 286 #endif 287 gl_Position = point_world_to_ndc(world_pos); 288 289 #ifdef V3D_SHADING_TEXTURE_COLOR 290 uv_interp = uv; 291 #endif 292 293 #ifdef V3D_SHADING_VERTEX_COLOR 294 # ifndef HAIR_SHADER 295 vertexColor = srgb_to_linear_attr(ac); 296 # endif 297 #endif 298 299 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 300 # ifndef HAIR_SHADER 301 normal_viewport = normal_object_to_view(nor); 302 normal_viewport = normalize(normal_viewport); 303 # else 304 normal_viewport = normal_world_to_view(nor); 305 # endif 306 #endif 307 308 #ifdef OBJECT_ID_PASS_ENABLED 309 PASS_RESOURCE_ID 310 #endif 311 312 #ifdef USE_WORLD_CLIP_PLANES 313 world_clip_planes_calc_clip_distance(world_pos); 314 #endif 315 } 0(133) : error C0105: Syntax error in #if 0(133) : error C0105: Syntax error in #if 0(134) : error C0000: syntax error, unexpected '!' at token "!" 0(138) : error C0000: syntax error, unexpected '}' at token "}" 0(142) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS 15 #define OBJECT_ID_PASS_ENABLED ===== shader string 6 ==== 16 #define COMMON_VIEW_LIB 17 #define DRW_RESOURCE_CHUNK_LEN 512 18 19 /* keep in sync with DRWManager.view_data */ 20 layout(std140) uniform viewBlock 21 { 22 /* Same order as DRWViewportMatrixType */ 23 mat4 ViewProjectionMatrix; 24 mat4 ViewProjectionMatrixInverse; 25 mat4 ViewMatrix; 26 mat4 ViewMatrixInverse; 27 mat4 ProjectionMatrix; 28 mat4 ProjectionMatrixInverse; 29 30 vec4 clipPlanes[6]; 31 32 /* TODO move it elsewhere. */ 33 vec4 CameraTexCoFactors; 34 }; 35 36 #ifdef world_clip_planes_calc_clip_distance 37 # undef world_clip_planes_calc_clip_distance 38 # define world_clip_planes_calc_clip_distance(p) \ 39 _world_clip_planes_calc_clip_distance(p, clipPlanes) 40 #endif 41 42 #ifdef COMMON_GLOBALS_LIB 43 float mul_project_m4_v3_zfac(in vec3 co) 44 { 45 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 46 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 47 } 48 #endif 49 50 /* Not the right place but need to be common to all overlay's. 51 * TODO Split to an overlay lib. */ 52 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 53 { 54 const float div = 1.0 / 255.0; 55 int a = int(mat[0][3]); 56 int b = int(mat[1][3]); 57 int c = int(mat[2][3]); 58 int d = int(mat[3][3]); 59 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 60 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 61 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 62 mat[3][3] = 1.0; 63 return mat; 64 } 65 66 /* Same here, Not the right place but need to be common to all overlay's. 67 * TODO Split to an overlay lib. */ 68 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 69 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 70 { 71 vec2 edge = edge_start - edge_pos; 72 float len = length(edge); 73 if (len > 0.0) { 74 edge /= len; 75 vec2 perp = vec2(-edge.y, edge.x); 76 float dist = dot(perp, frag_co - edge_start); 77 /* Add 0.1 to diffenrentiate with cleared pixels. */ 78 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 79 } 80 else { 81 /* Default line if the origin is perfectly aligned with a pixel. */ 82 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 83 } 84 } 85 86 uniform int resourceChunk; 87 88 #ifdef GPU_VERTEX_SHADER 89 # ifdef GL_ARB_shader_draw_parameters 90 # define baseInstance gl_BaseInstanceARB 91 # else /* no ARB_shader_draw_parameters */ 92 uniform int baseInstance; 93 # endif 94 95 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 96 /* When drawing instances of an object at the same position. */ 97 # define instanceId 0 98 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 99 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 100 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 101 * Ignore gl_InstanceID then. */ 102 # define instanceId 0 103 # else 104 # define instanceId gl_InstanceID 105 # endif 106 107 # define resource_id (baseInstance + instanceId) 108 109 /* Use this to declare and pass the value if 110 * the fragment shader uses the resource_id. */ 111 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 112 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 113 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 114 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 115 #endif 116 117 /* If used in a fragment / geometry shader, we pass 118 * resource_id as varying. */ 119 #ifdef GPU_GEOMETRY_SHADER 120 # define RESOURCE_ID_VARYING \ 121 flat out int resourceIDFrag; \ 122 flat in int resourceIDGeom[]; 123 124 # define resource_id resourceIDGeom 125 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 126 #endif 127 128 #ifdef GPU_FRAGMENT_SHADER 129 flat in int resourceIDFrag; 130 # define resource_id resourceIDFrag 131 #endif 132 133 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 134 !defined(INSTANCED_ATTRIB) 135 struct ObjectMatrices { 136 mat4 drw_modelMatrix; 137 mat4 drw_modelMatrixInverse; 138 }; 139 140 layout(std140) uniform modelBlock 141 { 142 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 143 }; 144 145 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 146 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 147 148 #else /* GPU_INTEL */ 149 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 150 * So for now we just force using the legacy path. */ 151 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 152 * and older amd driver on windows. */ 153 uniform mat4 ModelMatrix; 154 uniform mat4 ModelMatrixInverse; 155 #endif 156 157 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 158 159 /** Transform shortcuts. */ 160 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 161 * will always be decomposed in at least 2 matrix operation. */ 162 163 /** 164 * Some clarification: 165 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 166 * 167 * But since it is slow to multiply matrices we decompose it. Decomposing 168 * inversion and transposition both invert the product order leaving us with 169 * the same original order: 170 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 171 * 172 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 173 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 174 * ViewMatrix * transpose(ModelMatrixInverse) 175 **/ 176 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 177 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 178 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 179 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 180 181 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 182 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 183 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 184 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 185 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 186 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 187 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 188 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 189 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 190 191 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 192 * to make vertex shaders work. even if it's actually dead code. */ 193 #ifdef GPU_INTEL 194 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 195 #else 196 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 197 #endif 198 199 #define DRW_BASE_SELECTED (1 << 1) 200 #define DRW_BASE_FROM_DUPLI (1 << 2) 201 #define DRW_BASE_FROM_SET (1 << 3) 202 #define DRW_BASE_ACTIVE (1 << 4) 203 204 #ifndef HAIR_SHADER 205 in vec3 pos; 206 in vec3 nor; 207 in vec2 au; /* active texture layer */ 208 # ifdef V3D_SHADING_VERTEX_COLOR 209 in vec3 ac; /* active color */ 210 # endif 211 # define uv au 212 #else /* HAIR_SHADER */ 213 # ifdef V3D_SHADING_TEXTURE_COLOR 214 uniform samplerBuffer au; /* active texture layer */ 215 # endif 216 flat out float hair_rand; 217 #endif /* HAIR_SHADER */ 218 219 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 220 out vec3 normal_viewport; 221 #endif 222 223 #ifdef V3D_SHADING_TEXTURE_COLOR 224 out vec2 uv_interp; 225 #endif 226 #ifdef V3D_SHADING_VERTEX_COLOR 227 out vec3 vertexColor; 228 #endif 229 230 #ifdef OBJECT_ID_PASS_ENABLED 231 RESOURCE_ID_VARYING 232 #endif 233 234 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 235 float integer_noise(int n) 236 { 237 n = (n >> 13) ^ n; 238 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 239 return (float(nn) / 1073741824.0); 240 } 241 242 #ifdef V3D_SHADING_VERTEX_COLOR 243 vec3 srgb_to_linear_attr(vec3 c) 244 { 245 c = max(c, vec3(0.0)); 246 vec3 c1 = c * (1.0 / 12.92); 247 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 248 return mix(c1, c2, step(vec3(0.04045), c)); 249 } 250 #endif 251 252 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 253 { 254 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 255 vec3 nor = cross(tan, binor); 256 nor = normalize(mix(nor, -tan, rand * 0.1)); 257 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 258 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 259 nor = nor * sin_theta + binor * cos_theta; 260 return nor; 261 } 262 263 void main() 264 { 265 #ifdef HAIR_SHADER 266 # ifdef V3D_SHADING_TEXTURE_COLOR 267 vec2 uv = hair_get_customdata_vec2(au); 268 # endif 269 float time, thick_time, thickness; 270 vec3 world_pos, tan, binor; 271 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 272 ModelMatrixInverse, 273 ViewMatrixInverse[3].xyz, 274 ViewMatrixInverse[2].xyz, 275 world_pos, 276 tan, 277 binor, 278 time, 279 thickness, 280 thick_time); 281 282 hair_rand = integer_noise(hair_get_strand_id()); 283 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 284 #else 285 vec3 world_pos = point_object_to_world(pos); 286 #endif 287 gl_Position = point_world_to_ndc(world_pos); 288 289 #ifdef V3D_SHADING_TEXTURE_COLOR 290 uv_interp = uv; 291 #endif 292 293 #ifdef V3D_SHADING_VERTEX_COLOR 294 # ifndef HAIR_SHADER 295 vertexColor = srgb_to_linear_attr(ac); 296 # endif 297 #endif 298 299 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 300 # ifndef HAIR_SHADER 301 normal_viewport = normal_object_to_view(nor); 302 normal_viewport = normalize(normal_viewport); 303 # else 304 normal_viewport = normal_world_to_view(nor); 305 # endif 306 #endif 307 308 #ifdef OBJECT_ID_PASS_ENABLED 309 PASS_RESOURCE_ID 310 #endif 311 312 #ifdef USE_WORLD_CLIP_PLANES 313 world_clip_planes_calc_clip_distance(world_pos); 314 #endif 315 } 0(133) : error C0105: Syntax error in #if 0(133) : error C0105: Syntax error in #if 0(134) : error C0000: syntax error, unexpected '!' at token "!" 0(138) : error C0000: syntax error, unexpected '}' at token "}" 0(142) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS 15 #define OBJECT_ID_PASS_ENABLED ===== shader string 6 ==== 16 #define COMMON_VIEW_LIB 17 #define DRW_RESOURCE_CHUNK_LEN 512 18 19 /* keep in sync with DRWManager.view_data */ 20 layout(std140) uniform viewBlock 21 { 22 /* Same order as DRWViewportMatrixType */ 23 mat4 ViewProjectionMatrix; 24 mat4 ViewProjectionMatrixInverse; 25 mat4 ViewMatrix; 26 mat4 ViewMatrixInverse; 27 mat4 ProjectionMatrix; 28 mat4 ProjectionMatrixInverse; 29 30 vec4 clipPlanes[6]; 31 32 /* TODO move it elsewhere. */ 33 vec4 CameraTexCoFactors; 34 }; 35 36 #ifdef world_clip_planes_calc_clip_distance 37 # undef world_clip_planes_calc_clip_distance 38 # define world_clip_planes_calc_clip_distance(p) \ 39 _world_clip_planes_calc_clip_distance(p, clipPlanes) 40 #endif 41 42 #ifdef COMMON_GLOBALS_LIB 43 float mul_project_m4_v3_zfac(in vec3 co) 44 { 45 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 46 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 47 } 48 #endif 49 50 /* Not the right place but need to be common to all overlay's. 51 * TODO Split to an overlay lib. */ 52 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 53 { 54 const float div = 1.0 / 255.0; 55 int a = int(mat[0][3]); 56 int b = int(mat[1][3]); 57 int c = int(mat[2][3]); 58 int d = int(mat[3][3]); 59 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 60 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 61 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 62 mat[3][3] = 1.0; 63 return mat; 64 } 65 66 /* Same here, Not the right place but need to be common to all overlay's. 67 * TODO Split to an overlay lib. */ 68 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 69 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 70 { 71 vec2 edge = edge_start - edge_pos; 72 float len = length(edge); 73 if (len > 0.0) { 74 edge /= len; 75 vec2 perp = vec2(-edge.y, edge.x); 76 float dist = dot(perp, frag_co - edge_start); 77 /* Add 0.1 to diffenrentiate with cleared pixels. */ 78 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 79 } 80 else { 81 /* Default line if the origin is perfectly aligned with a pixel. */ 82 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 83 } 84 } 85 86 uniform int resourceChunk; 87 88 #ifdef GPU_VERTEX_SHADER 89 # ifdef GL_ARB_shader_draw_parameters 90 # define baseInstance gl_BaseInstanceARB 91 # else /* no ARB_shader_draw_parameters */ 92 uniform int baseInstance; 93 # endif 94 95 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 96 /* When drawing instances of an object at the same position. */ 97 # define instanceId 0 98 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 99 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 100 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 101 * Ignore gl_InstanceID then. */ 102 # define instanceId 0 103 # else 104 # define instanceId gl_InstanceID 105 # endif 106 107 # define resource_id (baseInstance + instanceId) 108 109 /* Use this to declare and pass the value if 110 * the fragment shader uses the resource_id. */ 111 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 112 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 113 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 114 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 115 #endif 116 117 /* If used in a fragment / geometry shader, we pass 118 * resource_id as varying. */ 119 #ifdef GPU_GEOMETRY_SHADER 120 # define RESOURCE_ID_VARYING \ 121 flat out int resourceIDFrag; \ 122 flat in int resourceIDGeom[]; 123 124 # define resource_id resourceIDGeom 125 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 126 #endif 127 128 #ifdef GPU_FRAGMENT_SHADER 129 flat in int resourceIDFrag; 130 # define resource_id resourceIDFrag 131 #endif 132 133 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 134 !defined(INSTANCED_ATTRIB) 135 struct ObjectMatrices { 136 mat4 drw_modelMatrix; 137 mat4 drw_modelMatrixInverse; 138 }; 139 140 layout(std140) uniform modelBlock 141 { 142 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 143 }; 144 145 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 146 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 147 148 #else /* GPU_INTEL */ 149 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 150 * So for now we just force using the legacy path. */ 151 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 152 * and older amd driver on windows. */ 153 uniform mat4 ModelMatrix; 154 uniform mat4 ModelMatrixInverse; 155 #endif 156 157 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 158 159 /** Transform shortcuts. */ 160 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 161 * will always be decomposed in at least 2 matrix operation. */ 162 163 /** 164 * Some clarification: 165 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 166 * 167 * But since it is slow to multiply matrices we decompose it. Decomposing 168 * inversion and transposition both invert the product order leaving us with 169 * the same original order: 170 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 171 * 172 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 173 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 174 * ViewMatrix * transpose(ModelMatrixInverse) 175 **/ 176 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 177 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 178 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 179 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 180 181 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 182 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 183 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 184 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 185 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 186 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 187 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 188 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 189 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 190 191 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 192 * to make vertex shaders work. even if it's actually dead code. */ 193 #ifdef GPU_INTEL 194 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 195 #else 196 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 197 #endif 198 199 #define DRW_BASE_SELECTED (1 << 1) 200 #define DRW_BASE_FROM_DUPLI (1 << 2) 201 #define DRW_BASE_FROM_SET (1 << 3) 202 #define DRW_BASE_ACTIVE (1 << 4) 203 204 #ifndef HAIR_SHADER 205 in vec3 pos; 206 in vec3 nor; 207 in vec2 au; /* active texture layer */ 208 # ifdef V3D_SHADING_VERTEX_COLOR 209 in vec3 ac; /* active color */ 210 # endif 211 # define uv au 212 #else /* HAIR_SHADER */ 213 # ifdef V3D_SHADING_TEXTURE_COLOR 214 uniform samplerBuffer au; /* active texture layer */ 215 # endif 216 flat out float hair_rand; 217 #endif /* HAIR_SHADER */ 218 219 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 220 out vec3 normal_viewport; 221 #endif 222 223 #ifdef V3D_SHADING_TEXTURE_COLOR 224 out vec2 uv_interp; 225 #endif 226 #ifdef V3D_SHADING_VERTEX_COLOR 227 out vec3 vertexColor; 228 #endif 229 230 #ifdef OBJECT_ID_PASS_ENABLED 231 RESOURCE_ID_VARYING 232 #endif 233 234 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 235 float integer_noise(int n) 236 { 237 n = (n >> 13) ^ n; 238 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 239 return (float(nn) / 1073741824.0); 240 } 241 242 #ifdef V3D_SHADING_VERTEX_COLOR 243 vec3 srgb_to_linear_attr(vec3 c) 244 { 245 c = max(c, vec3(0.0)); 246 vec3 c1 = c * (1.0 / 12.92); 247 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 248 return mix(c1, c2, step(vec3(0.04045), c)); 249 } 250 #endif 251 252 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 253 { 254 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 255 vec3 nor = cross(tan, binor); 256 nor = normalize(mix(nor, -tan, rand * 0.1)); 257 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 258 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 259 nor = nor * sin_theta + binor * cos_theta; 260 return nor; 261 } 262 263 void main() 264 { 265 #ifdef HAIR_SHADER 266 # ifdef V3D_SHADING_TEXTURE_COLOR 267 vec2 uv = hair_get_customdata_vec2(au); 268 # endif 269 float time, thick_time, thickness; 270 vec3 world_pos, tan, binor; 271 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 272 ModelMatrixInverse, 273 ViewMatrixInverse[3].xyz, 274 ViewMatrixInverse[2].xyz, 275 world_pos, 276 tan, 277 binor, 278 time, 279 thickness, 280 thick_time); 281 282 hair_rand = integer_noise(hair_get_strand_id()); 283 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 284 #else 285 vec3 world_pos = point_object_to_world(pos); 286 #endif 287 gl_Position = point_world_to_ndc(world_pos); 288 289 #ifdef V3D_SHADING_TEXTURE_COLOR 290 uv_interp = uv; 291 #endif 292 293 #ifdef V3D_SHADING_VERTEX_COLOR 294 # ifndef HAIR_SHADER 295 vertexColor = srgb_to_linear_attr(ac); 296 # endif 297 #endif 298 299 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 300 # ifndef HAIR_SHADER 301 normal_viewport = normal_object_to_view(nor); 302 normal_viewport = normalize(normal_viewport); 303 # else 304 normal_viewport = normal_world_to_view(nor); 305 # endif 306 #endif 307 308 #ifdef OBJECT_ID_PASS_ENABLED 309 PASS_RESOURCE_ID 310 #endif 311 312 #ifdef USE_WORLD_CLIP_PLANES 313 world_clip_planes_calc_clip_distance(world_pos); 314 #endif 315 } 0(133) : error C0105: Syntax error in #if 0(133) : error C0105: Syntax error in #if 0(134) : error C0000: syntax error, unexpected '!' at token "!" 0(138) : error C0000: syntax error, unexpected '}' at token "}" 0(142) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS 15 #define HAIR_SHADER 16 #define OBJECT_ID_PASS_ENABLED ===== shader string 6 ==== 17 /** 18 * Library to create hairs dynamically from control points. 19 * This is less bandwidth intensive than fetching the vertex attributes 20 * but does more ALU work per vertex. This also reduces the amount 21 * of data the CPU has to precompute and transfer for each update. 22 */ 23 24 /** 25 * hairStrandsRes: Number of points per hair strand. 26 * 2 - no subdivision 27 * 3+ - 1 or more interpolated points per hair. 28 */ 29 uniform int hairStrandsRes = 8; 30 31 /** 32 * hairThicknessRes : Subdiv around the hair. 33 * 1 - Wire Hair: Only one pixel thick, independent of view distance. 34 * 2 - Polystrip Hair: Correct width, flat if camera is parallel. 35 * 3+ - Cylinder Hair: Massive calculation but potentially perfect. Still need proper support. 36 */ 37 uniform int hairThicknessRes = 1; 38 39 /* Hair thickness shape. */ 40 uniform float hairRadRoot = 0.01; 41 uniform float hairRadTip = 0.0; 42 uniform float hairRadShape = 0.5; 43 uniform bool hairCloseTip = true; 44 45 uniform mat4 hairDupliMatrix; 46 47 /* -- Per control points -- */ 48 uniform samplerBuffer hairPointBuffer; /* RGBA32F */ 49 #define point_position xyz 50 #define point_time w /* Position along the hair length */ 51 52 /* -- Per strands data -- */ 53 uniform usamplerBuffer hairStrandBuffer; /* R32UI */ 54 uniform usamplerBuffer hairStrandSegBuffer; /* R16UI */ 55 56 /* Not used, use one buffer per uv layer */ 57 // uniform samplerBuffer hairUVBuffer; /* RG32F */ 58 // uniform samplerBuffer hairColBuffer; /* RGBA16 linear color */ 59 60 /* -- Subdivision stage -- */ 61 /** 62 * We use a transform feedback to preprocess the strands and add more subdivision to it. 63 * For the moment these are simple smooth interpolation but one could hope to see the full 64 * children particle modifiers being evaluated at this stage. 65 * 66 * If no more subdivision is needed, we can skip this step. 67 */ 68 69 #ifdef HAIR_PHASE_SUBDIV 70 int hair_get_base_id(float local_time, int strand_segments, out float interp_time) 71 { 72 float time_per_strand_seg = 1.0 / float(strand_segments); 73 74 float ratio = local_time / time_per_strand_seg; 75 interp_time = fract(ratio); 76 77 return int(ratio); 78 } 79 80 void hair_get_interp_attrs( 81 out vec4 data0, out vec4 data1, out vec4 data2, out vec4 data3, out float interp_time) 82 { 83 float local_time = float(gl_VertexID % hairStrandsRes) / float(hairStrandsRes - 1); 84 85 int hair_id = gl_VertexID / hairStrandsRes; 86 int strand_offset = int(texelFetch(hairStrandBuffer, hair_id).x); 87 int strand_segments = int(texelFetch(hairStrandSegBuffer, hair_id).x); 88 89 int id = hair_get_base_id(local_time, strand_segments, interp_time); 90 91 int ofs_id = id + strand_offset; 92 93 data0 = texelFetch(hairPointBuffer, ofs_id - 1); 94 data1 = texelFetch(hairPointBuffer, ofs_id); 95 data2 = texelFetch(hairPointBuffer, ofs_id + 1); 96 data3 = texelFetch(hairPointBuffer, ofs_id + 2); 97 98 if (id <= 0) { 99 /* root points. Need to reconstruct previous data. */ 100 data0 = data1 * 2.0 - data2; 101 } 102 if (id + 1 >= strand_segments) { 103 /* tip points. Need to reconstruct next data. */ 104 data3 = data2 * 2.0 - data1; 105 } 106 } 107 #endif 108 109 /* -- Drawing stage -- */ 110 /** 111 * For final drawing, the vertex index and the number of vertex per segment 112 */ 113 114 #ifndef HAIR_PHASE_SUBDIV 115 int hair_get_strand_id(void) 116 { 117 return gl_VertexID / (hairStrandsRes * hairThicknessRes); 118 } 119 120 int hair_get_base_id(void) 121 { 122 return gl_VertexID / hairThicknessRes; 123 } 124 125 /* Copied from cycles. */ 126 float hair_shaperadius(float shape, float root, float tip, float time) 127 { 128 float radius = 1.0 - time; 129 130 if (shape < 0.0) { 131 radius = pow(radius, 1.0 + shape); 132 } 133 else { 134 radius = pow(radius, 1.0 / (1.0 - shape)); 135 } 136 137 if (hairCloseTip && (time > 0.99)) { 138 return 0.0; 139 } 140 141 return (radius * (root - tip)) + tip; 142 } 143 144 # ifdef OS_MAC 145 in float dummy; 146 # endif 147 148 void hair_get_pos_tan_binor_time(bool is_persp, 149 mat4 invmodel_mat, 150 vec3 camera_pos, 151 vec3 camera_z, 152 out vec3 wpos, 153 out vec3 wtan, 154 out vec3 wbinor, 155 out float time, 156 out float thickness, 157 out float thick_time) 158 { 159 int id = hair_get_base_id(); 160 vec4 data = texelFetch(hairPointBuffer, id); 161 wpos = data.point_position; 162 time = data.point_time; 163 164 # ifdef OS_MAC 165 /* Generate a dummy read to avoid the driver bug with shaders having no 166 * vertex reads on macOS (T60171) */ 167 wpos.y += dummy * 0.0; 168 # endif 169 170 if (time == 0.0) { 171 /* Hair root */ 172 wtan = texelFetch(hairPointBuffer, id + 1).point_position - wpos; 173 } 174 else { 175 wtan = wpos - texelFetch(hairPointBuffer, id - 1).point_position; 176 } 177 178 wpos = (hairDupliMatrix * vec4(wpos, 1.0)).xyz; 179 wtan = -normalize(mat3(hairDupliMatrix) * wtan); 180 181 vec3 camera_vec = (is_persp) ? camera_pos - wpos : camera_z; 182 wbinor = normalize(cross(camera_vec, wtan)); 183 184 thickness = hair_shaperadius(hairRadShape, hairRadRoot, hairRadTip, time); 185 186 if (hairThicknessRes > 1) { 187 thick_time = float(gl_VertexID % hairThicknessRes) / float(hairThicknessRes - 1); 188 thick_time = thickness * (thick_time * 2.0 - 1.0); 189 190 /* Take object scale into account. 191 * NOTE: This only works fine with uniform scaling. */ 192 float scale = 1.0 / length(mat3(invmodel_mat) * wbinor); 193 194 wpos += wbinor * thick_time * scale; 195 } 196 } 197 198 vec2 hair_get_customdata_vec2(const samplerBuffer cd_buf) 199 { 200 int id = hair_get_strand_id(); 201 return texelFetch(cd_buf, id).rg; 202 } 203 204 vec3 hair_get_customdata_vec3(const samplerBuffer cd_buf) 205 { 206 int id = hair_get_strand_id(); 207 return texelFetch(cd_buf, id).rgb; 208 } 209 210 vec4 hair_get_customdata_vec4(const samplerBuffer cd_buf) 211 { 212 int id = hair_get_strand_id(); 213 return texelFetch(cd_buf, id).rgba; 214 } 215 216 vec3 hair_get_strand_pos(void) 217 { 218 int id = hair_get_strand_id() * hairStrandsRes; 219 return texelFetch(hairPointBuffer, id).point_position; 220 } 221 222 #endif 223 #define COMMON_VIEW_LIB 224 #define DRW_RESOURCE_CHUNK_LEN 512 225 226 /* keep in sync with DRWManager.view_data */ 227 layout(std140) uniform viewBlock 228 { 229 /* Same order as DRWViewportMatrixType */ 230 mat4 ViewProjectionMatrix; 231 mat4 ViewProjectionMatrixInverse; 232 mat4 ViewMatrix; 233 mat4 ViewMatrixInverse; 234 mat4 ProjectionMatrix; 235 mat4 ProjectionMatrixInverse; 236 237 vec4 clipPlanes[6]; 238 239 /* TODO move it elsewhere. */ 240 vec4 CameraTexCoFactors; 241 }; 242 243 #ifdef world_clip_planes_calc_clip_distance 244 # undef world_clip_planes_calc_clip_distance 245 # define world_clip_planes_calc_clip_distance(p) \ 246 _world_clip_planes_calc_clip_distance(p, clipPlanes) 247 #endif 248 249 #ifdef COMMON_GLOBALS_LIB 250 float mul_project_m4_v3_zfac(in vec3 co) 251 { 252 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 253 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 254 } 255 #endif 256 257 /* Not the right place but need to be common to all overlay's. 258 * TODO Split to an overlay lib. */ 259 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 260 { 261 const float div = 1.0 / 255.0; 262 int a = int(mat[0][3]); 263 int b = int(mat[1][3]); 264 int c = int(mat[2][3]); 265 int d = int(mat[3][3]); 266 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 267 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 268 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 269 mat[3][3] = 1.0; 270 return mat; 271 } 272 273 /* Same here, Not the right place but need to be common to all overlay's. 274 * TODO Split to an overlay lib. */ 275 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 276 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 277 { 278 vec2 edge = edge_start - edge_pos; 279 float len = length(edge); 280 if (len > 0.0) { 281 edge /= len; 282 vec2 perp = vec2(-edge.y, edge.x); 283 float dist = dot(perp, frag_co - edge_start); 284 /* Add 0.1 to diffenrentiate with cleared pixels. */ 285 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 286 } 287 else { 288 /* Default line if the origin is perfectly aligned with a pixel. */ 289 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 290 } 291 } 292 293 uniform int resourceChunk; 294 295 #ifdef GPU_VERTEX_SHADER 296 # ifdef GL_ARB_shader_draw_parameters 297 # define baseInstance gl_BaseInstanceARB 298 # else /* no ARB_shader_draw_parameters */ 299 uniform int baseInstance; 300 # endif 301 302 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 303 /* When drawing instances of an object at the same position. */ 304 # define instanceId 0 305 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 306 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 307 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 308 * Ignore gl_InstanceID then. */ 309 # define instanceId 0 310 # else 311 # define instanceId gl_InstanceID 312 # endif 313 314 # define resource_id (baseInstance + instanceId) 315 316 /* Use this to declare and pass the value if 317 * the fragment shader uses the resource_id. */ 318 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 319 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 320 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 321 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 322 #endif 323 324 /* If used in a fragment / geometry shader, we pass 325 * resource_id as varying. */ 326 #ifdef GPU_GEOMETRY_SHADER 327 # define RESOURCE_ID_VARYING \ 328 flat out int resourceIDFrag; \ 329 flat in int resourceIDGeom[]; 330 331 # define resource_id resourceIDGeom 332 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 333 #endif 334 335 #ifdef GPU_FRAGMENT_SHADER 336 flat in int resourceIDFrag; 337 # define resource_id resourceIDFrag 338 #endif 339 340 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 341 !defined(INSTANCED_ATTRIB) 342 struct ObjectMatrices { 343 mat4 drw_modelMatrix; 344 mat4 drw_modelMatrixInverse; 345 }; 346 347 layout(std140) uniform modelBlock 348 { 349 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 350 }; 351 352 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 353 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 354 355 #else /* GPU_INTEL */ 356 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 357 * So for now we just force using the legacy path. */ 358 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 359 * and older amd driver on windows. */ 360 uniform mat4 ModelMatrix; 361 uniform mat4 ModelMatrixInverse; 362 #endif 363 364 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 365 366 /** Transform shortcuts. */ 367 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 368 * will always be decomposed in at least 2 matrix operation. */ 369 370 /** 371 * Some clarification: 372 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 373 * 374 * But since it is slow to multiply matrices we decompose it. Decomposing 375 * inversion and transposition both invert the product order leaving us with 376 * the same original order: 377 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 378 * 379 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 380 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 381 * ViewMatrix * transpose(ModelMatrixInverse) 382 **/ 383 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 384 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 385 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 386 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 387 388 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 389 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 390 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 391 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 392 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 393 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 394 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 395 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 396 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 397 398 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 399 * to make vertex shaders work. even if it's actually dead code. */ 400 #ifdef GPU_INTEL 401 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 402 #else 403 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 404 #endif 405 406 #define DRW_BASE_SELECTED (1 << 1) 407 #define DRW_BASE_FROM_DUPLI (1 << 2) 408 #define DRW_BASE_FROM_SET (1 << 3) 409 #define DRW_BASE_ACTIVE (1 << 4) 410 411 #ifndef HAIR_SHADER 412 in vec3 pos; 413 in vec3 nor; 414 in vec2 au; /* active texture layer */ 415 # ifdef V3D_SHADING_VERTEX_COLOR 416 in vec3 ac; /* active color */ 417 # endif 418 # define uv au 419 #else /* HAIR_SHADER */ 420 # ifdef V3D_SHADING_TEXTURE_COLOR 421 uniform samplerBuffer au; /* active texture layer */ 422 # endif 423 flat out float hair_rand; 424 #endif /* HAIR_SHADER */ 425 426 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 427 out vec3 normal_viewport; 428 #endif 429 430 #ifdef V3D_SHADING_TEXTURE_COLOR 431 out vec2 uv_interp; 432 #endif 433 #ifdef V3D_SHADING_VERTEX_COLOR 434 out vec3 vertexColor; 435 #endif 436 437 #ifdef OBJECT_ID_PASS_ENABLED 438 RESOURCE_ID_VARYING 439 #endif 440 441 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 442 float integer_noise(int n) 443 { 444 n = (n >> 13) ^ n; 445 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 446 return (float(nn) / 1073741824.0); 447 } 448 449 #ifdef V3D_SHADING_VERTEX_COLOR 450 vec3 srgb_to_linear_attr(vec3 c) 451 { 452 c = max(c, vec3(0.0)); 453 vec3 c1 = c * (1.0 / 12.92); 454 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 455 return mix(c1, c2, step(vec3(0.04045), c)); 456 } 457 #endif 458 459 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 460 { 461 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 462 vec3 nor = cross(tan, binor); 463 nor = normalize(mix(nor, -tan, rand * 0.1)); 464 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 465 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 466 nor = nor * sin_theta + binor * cos_theta; 467 return nor; 468 } 469 470 void main() 471 { 472 #ifdef HAIR_SHADER 473 # ifdef V3D_SHADING_TEXTURE_COLOR 474 vec2 uv = hair_get_customdata_vec2(au); 475 # endif 476 float time, thick_time, thickness; 477 vec3 world_pos, tan, binor; 478 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 479 ModelMatrixInverse, 480 ViewMatrixInverse[3].xyz, 481 ViewMatrixInverse[2].xyz, 482 world_pos, 483 tan, 484 binor, 485 time, 486 thickness, 487 thick_time); 488 489 hair_rand = integer_noise(hair_get_strand_id()); 490 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 491 #else 492 vec3 world_pos = point_object_to_world(pos); 493 #endif 494 gl_Position = point_world_to_ndc(world_pos); 495 496 #ifdef V3D_SHADING_TEXTURE_COLOR 497 uv_interp = uv; 498 #endif 499 500 #ifdef V3D_SHADING_VERTEX_COLOR 501 # ifndef HAIR_SHADER 502 vertexColor = srgb_to_linear_attr(ac); 503 # endif 504 #endif 505 506 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 507 # ifndef HAIR_SHADER 508 normal_viewport = normal_object_to_view(nor); 509 normal_viewport = normalize(normal_viewport); 510 # else 511 normal_viewport = normal_world_to_view(nor); 512 # endif 513 #endif 514 515 #ifdef OBJECT_ID_PASS_ENABLED 516 PASS_RESOURCE_ID 517 #endif 518 519 #ifdef USE_WORLD_CLIP_PLANES 520 world_clip_planes_calc_clip_distance(world_pos); 521 #endif 522 } 0(340) : error C0105: Syntax error in #if 0(340) : error C0105: Syntax error in #if 0(341) : error C0000: syntax error, unexpected '!' at token "!" 0(345) : error C0000: syntax error, unexpected '}' at token "}" 0(349) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define COMMON_VIEW_LIB 12 #define DRW_RESOURCE_CHUNK_LEN 512 13 14 /* keep in sync with DRWManager.view_data */ 15 layout(std140) uniform viewBlock 16 { 17 /* Same order as DRWViewportMatrixType */ 18 mat4 ViewProjectionMatrix; 19 mat4 ViewProjectionMatrixInverse; 20 mat4 ViewMatrix; 21 mat4 ViewMatrixInverse; 22 mat4 ProjectionMatrix; 23 mat4 ProjectionMatrixInverse; 24 25 vec4 clipPlanes[6]; 26 27 /* TODO move it elsewhere. */ 28 vec4 CameraTexCoFactors; 29 }; 30 31 #ifdef world_clip_planes_calc_clip_distance 32 # undef world_clip_planes_calc_clip_distance 33 # define world_clip_planes_calc_clip_distance(p) \ 34 _world_clip_planes_calc_clip_distance(p, clipPlanes) 35 #endif 36 37 #ifdef COMMON_GLOBALS_LIB 38 float mul_project_m4_v3_zfac(in vec3 co) 39 { 40 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 41 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 42 } 43 #endif 44 45 /* Not the right place but need to be common to all overlay's. 46 * TODO Split to an overlay lib. */ 47 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 48 { 49 const float div = 1.0 / 255.0; 50 int a = int(mat[0][3]); 51 int b = int(mat[1][3]); 52 int c = int(mat[2][3]); 53 int d = int(mat[3][3]); 54 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 55 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 56 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 57 mat[3][3] = 1.0; 58 return mat; 59 } 60 61 /* Same here, Not the right place but need to be common to all overlay's. 62 * TODO Split to an overlay lib. */ 63 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 64 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 65 { 66 vec2 edge = edge_start - edge_pos; 67 float len = length(edge); 68 if (len > 0.0) { 69 edge /= len; 70 vec2 perp = vec2(-edge.y, edge.x); 71 float dist = dot(perp, frag_co - edge_start); 72 /* Add 0.1 to diffenrentiate with cleared pixels. */ 73 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 74 } 75 else { 76 /* Default line if the origin is perfectly aligned with a pixel. */ 77 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 78 } 79 } 80 81 uniform int resourceChunk; 82 83 #ifdef GPU_VERTEX_SHADER 84 # ifdef GL_ARB_shader_draw_parameters 85 # define baseInstance gl_BaseInstanceARB 86 # else /* no ARB_shader_draw_parameters */ 87 uniform int baseInstance; 88 # endif 89 90 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 91 /* When drawing instances of an object at the same position. */ 92 # define instanceId 0 93 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 94 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 95 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 96 * Ignore gl_InstanceID then. */ 97 # define instanceId 0 98 # else 99 # define instanceId gl_InstanceID 100 # endif 101 102 # define resource_id (baseInstance + instanceId) 103 104 /* Use this to declare and pass the value if 105 * the fragment shader uses the resource_id. */ 106 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 107 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 108 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 109 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 110 #endif 111 112 /* If used in a fragment / geometry shader, we pass 113 * resource_id as varying. */ 114 #ifdef GPU_GEOMETRY_SHADER 115 # define RESOURCE_ID_VARYING \ 116 flat out int resourceIDFrag; \ 117 flat in int resourceIDGeom[]; 118 119 # define resource_id resourceIDGeom 120 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 121 #endif 122 123 #ifdef GPU_FRAGMENT_SHADER 124 flat in int resourceIDFrag; 125 # define resource_id resourceIDFrag 126 #endif 127 128 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 129 !defined(INSTANCED_ATTRIB) 130 struct ObjectMatrices { 131 mat4 drw_modelMatrix; 132 mat4 drw_modelMatrixInverse; 133 }; 134 135 layout(std140) uniform modelBlock 136 { 137 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 138 }; 139 140 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 141 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 142 143 #else /* GPU_INTEL */ 144 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 145 * So for now we just force using the legacy path. */ 146 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 147 * and older amd driver on windows. */ 148 uniform mat4 ModelMatrix; 149 uniform mat4 ModelMatrixInverse; 150 #endif 151 152 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 153 154 /** Transform shortcuts. */ 155 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 156 * will always be decomposed in at least 2 matrix operation. */ 157 158 /** 159 * Some clarification: 160 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 161 * 162 * But since it is slow to multiply matrices we decompose it. Decomposing 163 * inversion and transposition both invert the product order leaving us with 164 * the same original order: 165 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 166 * 167 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 168 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 169 * ViewMatrix * transpose(ModelMatrixInverse) 170 **/ 171 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 172 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 173 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 174 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 175 176 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 177 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 178 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 179 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 180 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 181 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 182 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 183 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 184 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 185 186 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 187 * to make vertex shaders work. even if it's actually dead code. */ 188 #ifdef GPU_INTEL 189 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 190 #else 191 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 192 #endif 193 194 #define DRW_BASE_SELECTED (1 << 1) 195 #define DRW_BASE_FROM_DUPLI (1 << 2) 196 #define DRW_BASE_FROM_SET (1 << 3) 197 #define DRW_BASE_ACTIVE (1 << 4) 198 199 uniform mat4 gpModelMatrix; 200 201 in vec3 pos; 202 in vec4 color; 203 in vec2 texCoord; 204 205 out vec4 finalColor; 206 out vec2 texCoord_interp; 207 208 void main(void) 209 { 210 gl_Position = point_world_to_ndc((gpModelMatrix * vec4(pos, 1.0)).xyz); 211 finalColor = color; 212 texCoord_interp = texCoord; 213 } 0(128) : error C0105: Syntax error in #if 0(128) : error C0105: Syntax error in #if 0(129) : error C0000: syntax error, unexpected '!' at token "!" 0(133) : error C0000: syntax error, unexpected '}' at token "}" 0(137) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define COMMON_VIEW_LIB 12 #define DRW_RESOURCE_CHUNK_LEN 512 13 14 /* keep in sync with DRWManager.view_data */ 15 layout(std140) uniform viewBlock 16 { 17 /* Same order as DRWViewportMatrixType */ 18 mat4 ViewProjectionMatrix; 19 mat4 ViewProjectionMatrixInverse; 20 mat4 ViewMatrix; 21 mat4 ViewMatrixInverse; 22 mat4 ProjectionMatrix; 23 mat4 ProjectionMatrixInverse; 24 25 vec4 clipPlanes[6]; 26 27 /* TODO move it elsewhere. */ 28 vec4 CameraTexCoFactors; 29 }; 30 31 #ifdef world_clip_planes_calc_clip_distance 32 # undef world_clip_planes_calc_clip_distance 33 # define world_clip_planes_calc_clip_distance(p) \ 34 _world_clip_planes_calc_clip_distance(p, clipPlanes) 35 #endif 36 37 #ifdef COMMON_GLOBALS_LIB 38 float mul_project_m4_v3_zfac(in vec3 co) 39 { 40 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 41 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 42 } 43 #endif 44 45 /* Not the right place but need to be common to all overlay's. 46 * TODO Split to an overlay lib. */ 47 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 48 { 49 const float div = 1.0 / 255.0; 50 int a = int(mat[0][3]); 51 int b = int(mat[1][3]); 52 int c = int(mat[2][3]); 53 int d = int(mat[3][3]); 54 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 55 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 56 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 57 mat[3][3] = 1.0; 58 return mat; 59 } 60 61 /* Same here, Not the right place but need to be common to all overlay's. 62 * TODO Split to an overlay lib. */ 63 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 64 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 65 { 66 vec2 edge = edge_start - edge_pos; 67 float len = length(edge); 68 if (len > 0.0) { 69 edge /= len; 70 vec2 perp = vec2(-edge.y, edge.x); 71 float dist = dot(perp, frag_co - edge_start); 72 /* Add 0.1 to diffenrentiate with cleared pixels. */ 73 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 74 } 75 else { 76 /* Default line if the origin is perfectly aligned with a pixel. */ 77 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 78 } 79 } 80 81 uniform int resourceChunk; 82 83 #ifdef GPU_VERTEX_SHADER 84 # ifdef GL_ARB_shader_draw_parameters 85 # define baseInstance gl_BaseInstanceARB 86 # else /* no ARB_shader_draw_parameters */ 87 uniform int baseInstance; 88 # endif 89 90 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 91 /* When drawing instances of an object at the same position. */ 92 # define instanceId 0 93 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 94 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 95 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 96 * Ignore gl_InstanceID then. */ 97 # define instanceId 0 98 # else 99 # define instanceId gl_InstanceID 100 # endif 101 102 # define resource_id (baseInstance + instanceId) 103 104 /* Use this to declare and pass the value if 105 * the fragment shader uses the resource_id. */ 106 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 107 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 108 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 109 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 110 #endif 111 112 /* If used in a fragment / geometry shader, we pass 113 * resource_id as varying. */ 114 #ifdef GPU_GEOMETRY_SHADER 115 # define RESOURCE_ID_VARYING \ 116 flat out int resourceIDFrag; \ 117 flat in int resourceIDGeom[]; 118 119 # define resource_id resourceIDGeom 120 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 121 #endif 122 123 #ifdef GPU_FRAGMENT_SHADER 124 flat in int resourceIDFrag; 125 # define resource_id resourceIDFrag 126 #endif 127 128 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 129 !defined(INSTANCED_ATTRIB) 130 struct ObjectMatrices { 131 mat4 drw_modelMatrix; 132 mat4 drw_modelMatrixInverse; 133 }; 134 135 layout(std140) uniform modelBlock 136 { 137 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 138 }; 139 140 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 141 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 142 143 #else /* GPU_INTEL */ 144 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 145 * So for now we just force using the legacy path. */ 146 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 147 * and older amd driver on windows. */ 148 uniform mat4 ModelMatrix; 149 uniform mat4 ModelMatrixInverse; 150 #endif 151 152 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 153 154 /** Transform shortcuts. */ 155 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 156 * will always be decomposed in at least 2 matrix operation. */ 157 158 /** 159 * Some clarification: 160 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 161 * 162 * But since it is slow to multiply matrices we decompose it. Decomposing 163 * inversion and transposition both invert the product order leaving us with 164 * the same original order: 165 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 166 * 167 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 168 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 169 * ViewMatrix * transpose(ModelMatrixInverse) 170 **/ 171 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 172 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 173 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 174 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 175 176 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 177 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 178 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 179 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 180 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 181 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 182 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 183 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 184 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 185 186 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 187 * to make vertex shaders work. even if it's actually dead code. */ 188 #ifdef GPU_INTEL 189 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 190 #else 191 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 192 #endif 193 194 #define DRW_BASE_SELECTED (1 << 1) 195 #define DRW_BASE_FROM_DUPLI (1 << 2) 196 #define DRW_BASE_FROM_SET (1 << 3) 197 #define DRW_BASE_ACTIVE (1 << 4) 198 199 uniform float pixsize; /* rv3d->pixsize */ 200 uniform int keep_size; 201 uniform float objscale; 202 uniform float pixfactor; 203 uniform int viewport_xray; 204 uniform int shading_type[2]; 205 uniform vec4 wire_color; 206 uniform mat4 gpModelMatrix; 207 208 in vec3 pos; 209 in vec4 color; 210 in float thickness; 211 in vec2 uvdata; 212 213 out vec4 finalColor; 214 out float finalThickness; 215 out vec2 finaluvdata; 216 217 #define TRUE 1 218 219 #define OB_WIRE 2 220 #define OB_SOLID 3 221 222 #define V3D_SHADING_MATERIAL_COLOR 0 223 #define V3D_SHADING_TEXTURE_COLOR 3 224 #define V3D_SHADING_VERTEX_COLOR 5 225 226 float defaultpixsize = pixsize * (1000.0 / pixfactor); 227 228 void main(void) 229 { 230 gl_Position = point_world_to_ndc((gpModelMatrix * vec4(pos, 1.0)).xyz); 231 finalColor = color; 232 233 if (keep_size == TRUE) { 234 finalThickness = thickness; 235 } 236 else { 237 float size = (ProjectionMatrix[3][3] == 0.0) ? (thickness / (gl_Position.z * defaultpixsize)) : 238 (thickness / defaultpixsize); 239 finalThickness = max(size * objscale, 1.0); 240 } 241 242 /* for wireframe override size and color */ 243 if (shading_type[0] == OB_WIRE) { 244 finalThickness = 1.0; 245 finalColor = wire_color; 246 } 247 /* for solid override color */ 248 if (shading_type[0] == OB_SOLID) { 249 if ((shading_type[1] != V3D_SHADING_MATERIAL_COLOR) && 250 (shading_type[1] != V3D_SHADING_TEXTURE_COLOR) && 251 (shading_type[1] != V3D_SHADING_VERTEX_COLOR)) { 252 finalColor = wire_color; 253 } 254 if (viewport_xray == 1) { 255 finalColor.a *= 0.5; 256 } 257 } 258 259 finaluvdata = uvdata; 260 } 0(128) : error C0105: Syntax error in #if 0(128) : error C0105: Syntax error in #if 0(129) : error C0000: syntax error, unexpected '!' at token "!" 0(133) : error C0000: syntax error, unexpected '}' at token "}" 0(137) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define COMMON_VIEW_LIB 12 #define DRW_RESOURCE_CHUNK_LEN 512 13 14 /* keep in sync with DRWManager.view_data */ 15 layout(std140) uniform viewBlock 16 { 17 /* Same order as DRWViewportMatrixType */ 18 mat4 ViewProjectionMatrix; 19 mat4 ViewProjectionMatrixInverse; 20 mat4 ViewMatrix; 21 mat4 ViewMatrixInverse; 22 mat4 ProjectionMatrix; 23 mat4 ProjectionMatrixInverse; 24 25 vec4 clipPlanes[6]; 26 27 /* TODO move it elsewhere. */ 28 vec4 CameraTexCoFactors; 29 }; 30 31 #ifdef world_clip_planes_calc_clip_distance 32 # undef world_clip_planes_calc_clip_distance 33 # define world_clip_planes_calc_clip_distance(p) \ 34 _world_clip_planes_calc_clip_distance(p, clipPlanes) 35 #endif 36 37 #ifdef COMMON_GLOBALS_LIB 38 float mul_project_m4_v3_zfac(in vec3 co) 39 { 40 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 41 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 42 } 43 #endif 44 45 /* Not the right place but need to be common to all overlay's. 46 * TODO Split to an overlay lib. */ 47 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 48 { 49 const float div = 1.0 / 255.0; 50 int a = int(mat[0][3]); 51 int b = int(mat[1][3]); 52 int c = int(mat[2][3]); 53 int d = int(mat[3][3]); 54 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 55 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 56 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 57 mat[3][3] = 1.0; 58 return mat; 59 } 60 61 /* Same here, Not the right place but need to be common to all overlay's. 62 * TODO Split to an overlay lib. */ 63 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 64 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 65 { 66 vec2 edge = edge_start - edge_pos; 67 float len = length(edge); 68 if (len > 0.0) { 69 edge /= len; 70 vec2 perp = vec2(-edge.y, edge.x); 71 float dist = dot(perp, frag_co - edge_start); 72 /* Add 0.1 to diffenrentiate with cleared pixels. */ 73 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 74 } 75 else { 76 /* Default line if the origin is perfectly aligned with a pixel. */ 77 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 78 } 79 } 80 81 uniform int resourceChunk; 82 83 #ifdef GPU_VERTEX_SHADER 84 # ifdef GL_ARB_shader_draw_parameters 85 # define baseInstance gl_BaseInstanceARB 86 # else /* no ARB_shader_draw_parameters */ 87 uniform int baseInstance; 88 # endif 89 90 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 91 /* When drawing instances of an object at the same position. */ 92 # define instanceId 0 93 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 94 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 95 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 96 * Ignore gl_InstanceID then. */ 97 # define instanceId 0 98 # else 99 # define instanceId gl_InstanceID 100 # endif 101 102 # define resource_id (baseInstance + instanceId) 103 104 /* Use this to declare and pass the value if 105 * the fragment shader uses the resource_id. */ 106 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 107 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 108 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 109 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 110 #endif 111 112 /* If used in a fragment / geometry shader, we pass 113 * resource_id as varying. */ 114 #ifdef GPU_GEOMETRY_SHADER 115 # define RESOURCE_ID_VARYING \ 116 flat out int resourceIDFrag; \ 117 flat in int resourceIDGeom[]; 118 119 # define resource_id resourceIDGeom 120 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 121 #endif 122 123 #ifdef GPU_FRAGMENT_SHADER 124 flat in int resourceIDFrag; 125 # define resource_id resourceIDFrag 126 #endif 127 128 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 129 !defined(INSTANCED_ATTRIB) 130 struct ObjectMatrices { 131 mat4 drw_modelMatrix; 132 mat4 drw_modelMatrixInverse; 133 }; 134 135 layout(std140) uniform modelBlock 136 { 137 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 138 }; 139 140 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 141 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 142 143 #else /* GPU_INTEL */ 144 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 145 * So for now we just force using the legacy path. */ 146 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 147 * and older amd driver on windows. */ 148 uniform mat4 ModelMatrix; 149 uniform mat4 ModelMatrixInverse; 150 #endif 151 152 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 153 154 /** Transform shortcuts. */ 155 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 156 * will always be decomposed in at least 2 matrix operation. */ 157 158 /** 159 * Some clarification: 160 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 161 * 162 * But since it is slow to multiply matrices we decompose it. Decomposing 163 * inversion and transposition both invert the product order leaving us with 164 * the same original order: 165 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 166 * 167 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 168 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 169 * ViewMatrix * transpose(ModelMatrixInverse) 170 **/ 171 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 172 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 173 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 174 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 175 176 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 177 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 178 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 179 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 180 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 181 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 182 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 183 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 184 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 185 186 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 187 * to make vertex shaders work. even if it's actually dead code. */ 188 #ifdef GPU_INTEL 189 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 190 #else 191 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 192 #endif 193 194 #define DRW_BASE_SELECTED (1 << 1) 195 #define DRW_BASE_FROM_DUPLI (1 << 2) 196 #define DRW_BASE_FROM_SET (1 << 3) 197 #define DRW_BASE_ACTIVE (1 << 4) 198 199 uniform float pixsize; /* rv3d->pixsize */ 200 uniform int keep_size; 201 uniform float objscale; 202 uniform float pixfactor; 203 uniform int viewport_xray; 204 uniform int shading_type[2]; 205 uniform vec4 wire_color; 206 uniform mat4 gpModelMatrix; 207 208 in vec3 pos; 209 in vec4 color; 210 in float thickness; 211 in vec2 uvdata; 212 in vec3 prev_pos; 213 214 out vec4 finalColor; 215 out float finalThickness; 216 out vec2 finaluvdata; 217 out vec4 finalprev_pos; 218 219 #define TRUE 1 220 221 #define OB_WIRE 2 222 #define OB_SOLID 3 223 224 #define V3D_SHADING_MATERIAL_COLOR 0 225 #define V3D_SHADING_TEXTURE_COLOR 3 226 #define V3D_SHADING_VERTEX_COLOR 5 227 228 float defaultpixsize = pixsize * (1000.0 / pixfactor); 229 230 void main() 231 { 232 gl_Position = point_world_to_ndc((gpModelMatrix * vec4(pos, 1.0)).xyz); 233 finalprev_pos = point_world_to_ndc((gpModelMatrix * vec4(prev_pos, 1.0)).xyz); 234 finalColor = color; 235 236 if (keep_size == TRUE) { 237 finalThickness = thickness; 238 } 239 else { 240 float size = (ProjectionMatrix[3][3] == 0.0) ? (thickness / (gl_Position.z * defaultpixsize)) : 241 (thickness / defaultpixsize); 242 finalThickness = max(size * objscale, 0.5); /* set a minimum size */ 243 } 244 245 /* for wireframe override size and color */ 246 if (shading_type[0] == OB_WIRE) { 247 finalThickness = 2.0; 248 finalColor = wire_color; 249 } 250 /* for solid override color */ 251 if (shading_type[0] == OB_SOLID) { 252 if ((shading_type[1] != V3D_SHADING_MATERIAL_COLOR) && 253 (shading_type[1] != V3D_SHADING_TEXTURE_COLOR) && 254 (shading_type[1] != V3D_SHADING_VERTEX_COLOR)) { 255 finalColor = wire_color; 256 } 257 if (viewport_xray == 1) { 258 finalColor.a *= 0.5; 259 } 260 } 261 262 finaluvdata = uvdata; 263 } 0(128) : error C0105: Syntax error in #if 0(128) : error C0105: Syntax error in #if 0(129) : error C0000: syntax error, unexpected '!' at token "!" 0(133) : error C0000: syntax error, unexpected '}' at token "}" 0(137) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define COMMON_VIEW_LIB 12 #define DRW_RESOURCE_CHUNK_LEN 512 13 14 /* keep in sync with DRWManager.view_data */ 15 layout(std140) uniform viewBlock 16 { 17 /* Same order as DRWViewportMatrixType */ 18 mat4 ViewProjectionMatrix; 19 mat4 ViewProjectionMatrixInverse; 20 mat4 ViewMatrix; 21 mat4 ViewMatrixInverse; 22 mat4 ProjectionMatrix; 23 mat4 ProjectionMatrixInverse; 24 25 vec4 clipPlanes[6]; 26 27 /* TODO move it elsewhere. */ 28 vec4 CameraTexCoFactors; 29 }; 30 31 #ifdef world_clip_planes_calc_clip_distance 32 # undef world_clip_planes_calc_clip_distance 33 # define world_clip_planes_calc_clip_distance(p) \ 34 _world_clip_planes_calc_clip_distance(p, clipPlanes) 35 #endif 36 37 #ifdef COMMON_GLOBALS_LIB 38 float mul_project_m4_v3_zfac(in vec3 co) 39 { 40 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 41 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 42 } 43 #endif 44 45 /* Not the right place but need to be common to all overlay's. 46 * TODO Split to an overlay lib. */ 47 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 48 { 49 const float div = 1.0 / 255.0; 50 int a = int(mat[0][3]); 51 int b = int(mat[1][3]); 52 int c = int(mat[2][3]); 53 int d = int(mat[3][3]); 54 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 55 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 56 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 57 mat[3][3] = 1.0; 58 return mat; 59 } 60 61 /* Same here, Not the right place but need to be common to all overlay's. 62 * TODO Split to an overlay lib. */ 63 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 64 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 65 { 66 vec2 edge = edge_start - edge_pos; 67 float len = length(edge); 68 if (len > 0.0) { 69 edge /= len; 70 vec2 perp = vec2(-edge.y, edge.x); 71 float dist = dot(perp, frag_co - edge_start); 72 /* Add 0.1 to diffenrentiate with cleared pixels. */ 73 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 74 } 75 else { 76 /* Default line if the origin is perfectly aligned with a pixel. */ 77 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 78 } 79 } 80 81 uniform int resourceChunk; 82 83 #ifdef GPU_VERTEX_SHADER 84 # ifdef GL_ARB_shader_draw_parameters 85 # define baseInstance gl_BaseInstanceARB 86 # else /* no ARB_shader_draw_parameters */ 87 uniform int baseInstance; 88 # endif 89 90 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 91 /* When drawing instances of an object at the same position. */ 92 # define instanceId 0 93 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 94 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 95 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 96 * Ignore gl_InstanceID then. */ 97 # define instanceId 0 98 # else 99 # define instanceId gl_InstanceID 100 # endif 101 102 # define resource_id (baseInstance + instanceId) 103 104 /* Use this to declare and pass the value if 105 * the fragment shader uses the resource_id. */ 106 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 107 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 108 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 109 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 110 #endif 111 112 /* If used in a fragment / geometry shader, we pass 113 * resource_id as varying. */ 114 #ifdef GPU_GEOMETRY_SHADER 115 # define RESOURCE_ID_VARYING \ 116 flat out int resourceIDFrag; \ 117 flat in int resourceIDGeom[]; 118 119 # define resource_id resourceIDGeom 120 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 121 #endif 122 123 #ifdef GPU_FRAGMENT_SHADER 124 flat in int resourceIDFrag; 125 # define resource_id resourceIDFrag 126 #endif 127 128 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 129 !defined(INSTANCED_ATTRIB) 130 struct ObjectMatrices { 131 mat4 drw_modelMatrix; 132 mat4 drw_modelMatrixInverse; 133 }; 134 135 layout(std140) uniform modelBlock 136 { 137 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 138 }; 139 140 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 141 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 142 143 #else /* GPU_INTEL */ 144 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 145 * So for now we just force using the legacy path. */ 146 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 147 * and older amd driver on windows. */ 148 uniform mat4 ModelMatrix; 149 uniform mat4 ModelMatrixInverse; 150 #endif 151 152 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 153 154 /** Transform shortcuts. */ 155 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 156 * will always be decomposed in at least 2 matrix operation. */ 157 158 /** 159 * Some clarification: 160 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 161 * 162 * But since it is slow to multiply matrices we decompose it. Decomposing 163 * inversion and transposition both invert the product order leaving us with 164 * the same original order: 165 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 166 * 167 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 168 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 169 * ViewMatrix * transpose(ModelMatrixInverse) 170 **/ 171 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 172 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 173 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 174 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 175 176 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 177 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 178 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 179 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 180 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 181 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 182 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 183 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 184 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 185 186 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 187 * to make vertex shaders work. even if it's actually dead code. */ 188 #ifdef GPU_INTEL 189 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 190 #else 191 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 192 #endif 193 194 #define DRW_BASE_SELECTED (1 << 1) 195 #define DRW_BASE_FROM_DUPLI (1 << 2) 196 #define DRW_BASE_FROM_SET (1 << 3) 197 #define DRW_BASE_ACTIVE (1 << 4) 198 199 uniform mat4 gpModelMatrix; 200 201 in vec3 pos; 202 in vec4 color; 203 in float size; 204 205 out vec4 finalColor; 206 out float finalThickness; 207 208 void main() 209 { 210 gl_Position = point_world_to_ndc((gpModelMatrix * vec4(pos, 1.0)).xyz); 211 finalColor = color; 212 finalThickness = size; 213 } 0(128) : error C0105: Syntax error in #if 0(128) : error C0105: Syntax error in #if 0(129) : error C0000: syntax error, unexpected '!' at token "!" 0(133) : error C0000: syntax error, unexpected '}' at token "}" 0(137) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define COMMON_VIEW_LIB 12 #define DRW_RESOURCE_CHUNK_LEN 512 13 14 /* keep in sync with DRWManager.view_data */ 15 layout(std140) uniform viewBlock 16 { 17 /* Same order as DRWViewportMatrixType */ 18 mat4 ViewProjectionMatrix; 19 mat4 ViewProjectionMatrixInverse; 20 mat4 ViewMatrix; 21 mat4 ViewMatrixInverse; 22 mat4 ProjectionMatrix; 23 mat4 ProjectionMatrixInverse; 24 25 vec4 clipPlanes[6]; 26 27 /* TODO move it elsewhere. */ 28 vec4 CameraTexCoFactors; 29 }; 30 31 #ifdef world_clip_planes_calc_clip_distance 32 # undef world_clip_planes_calc_clip_distance 33 # define world_clip_planes_calc_clip_distance(p) \ 34 _world_clip_planes_calc_clip_distance(p, clipPlanes) 35 #endif 36 37 #ifdef COMMON_GLOBALS_LIB 38 float mul_project_m4_v3_zfac(in vec3 co) 39 { 40 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 41 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 42 } 43 #endif 44 45 /* Not the right place but need to be common to all overlay's. 46 * TODO Split to an overlay lib. */ 47 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 48 { 49 const float div = 1.0 / 255.0; 50 int a = int(mat[0][3]); 51 int b = int(mat[1][3]); 52 int c = int(mat[2][3]); 53 int d = int(mat[3][3]); 54 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 55 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 56 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 57 mat[3][3] = 1.0; 58 return mat; 59 } 60 61 /* Same here, Not the right place but need to be common to all overlay's. 62 * TODO Split to an overlay lib. */ 63 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 64 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 65 { 66 vec2 edge = edge_start - edge_pos; 67 float len = length(edge); 68 if (len > 0.0) { 69 edge /= len; 70 vec2 perp = vec2(-edge.y, edge.x); 71 float dist = dot(perp, frag_co - edge_start); 72 /* Add 0.1 to diffenrentiate with cleared pixels. */ 73 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 74 } 75 else { 76 /* Default line if the origin is perfectly aligned with a pixel. */ 77 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 78 } 79 } 80 81 uniform int resourceChunk; 82 83 #ifdef GPU_VERTEX_SHADER 84 # ifdef GL_ARB_shader_draw_parameters 85 # define baseInstance gl_BaseInstanceARB 86 # else /* no ARB_shader_draw_parameters */ 87 uniform int baseInstance; 88 # endif 89 90 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 91 /* When drawing instances of an object at the same position. */ 92 # define instanceId 0 93 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 94 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 95 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 96 * Ignore gl_InstanceID then. */ 97 # define instanceId 0 98 # else 99 # define instanceId gl_InstanceID 100 # endif 101 102 # define resource_id (baseInstance + instanceId) 103 104 /* Use this to declare and pass the value if 105 * the fragment shader uses the resource_id. */ 106 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 107 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 108 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 109 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 110 #endif 111 112 /* If used in a fragment / geometry shader, we pass 113 * resource_id as varying. */ 114 #ifdef GPU_GEOMETRY_SHADER 115 # define RESOURCE_ID_VARYING \ 116 flat out int resourceIDFrag; \ 117 flat in int resourceIDGeom[]; 118 119 # define resource_id resourceIDGeom 120 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 121 #endif 122 123 #ifdef GPU_FRAGMENT_SHADER 124 flat in int resourceIDFrag; 125 # define resource_id resourceIDFrag 126 #endif 127 128 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 129 !defined(INSTANCED_ATTRIB) 130 struct ObjectMatrices { 131 mat4 drw_modelMatrix; 132 mat4 drw_modelMatrixInverse; 133 }; 134 135 layout(std140) uniform modelBlock 136 { 137 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 138 }; 139 140 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 141 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 142 143 #else /* GPU_INTEL */ 144 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 145 * So for now we just force using the legacy path. */ 146 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 147 * and older amd driver on windows. */ 148 uniform mat4 ModelMatrix; 149 uniform mat4 ModelMatrixInverse; 150 #endif 151 152 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 153 154 /** Transform shortcuts. */ 155 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 156 * will always be decomposed in at least 2 matrix operation. */ 157 158 /** 159 * Some clarification: 160 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 161 * 162 * But since it is slow to multiply matrices we decompose it. Decomposing 163 * inversion and transposition both invert the product order leaving us with 164 * the same original order: 165 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 166 * 167 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 168 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 169 * ViewMatrix * transpose(ModelMatrixInverse) 170 **/ 171 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 172 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 173 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 174 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 175 176 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 177 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 178 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 179 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 180 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 181 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 182 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 183 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 184 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 185 186 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 187 * to make vertex shaders work. even if it's actually dead code. */ 188 #ifdef GPU_INTEL 189 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 190 #else 191 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 192 #endif 193 194 #define DRW_BASE_SELECTED (1 << 1) 195 #define DRW_BASE_FROM_DUPLI (1 << 2) 196 #define DRW_BASE_FROM_SET (1 << 3) 197 #define DRW_BASE_ACTIVE (1 << 4) 198 199 uniform mat4 gpModelMatrix; 200 201 in vec3 pos; 202 in vec4 color; 203 in float size; 204 205 out vec4 finalColor; 206 out float finalThickness; 207 208 void main() 209 { 210 gl_Position = point_world_to_ndc((gpModelMatrix * vec4(pos, 1.0)).xyz); 211 finalColor = color; 212 finalThickness = size; 213 } 0(128) : error C0105: Syntax error in #if 0(128) : error C0105: Syntax error in #if 0(129) : error C0000: syntax error, unexpected '!' at token "!" 0(133) : error C0000: syntax error, unexpected '}' at token "}" 0(137) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS ===== shader string 6 ==== 15 #define COMMON_VIEW_LIB 16 #define DRW_RESOURCE_CHUNK_LEN 512 17 18 /* keep in sync with DRWManager.view_data */ 19 layout(std140) uniform viewBlock 20 { 21 /* Same order as DRWViewportMatrixType */ 22 mat4 ViewProjectionMatrix; 23 mat4 ViewProjectionMatrixInverse; 24 mat4 ViewMatrix; 25 mat4 ViewMatrixInverse; 26 mat4 ProjectionMatrix; 27 mat4 ProjectionMatrixInverse; 28 29 vec4 clipPlanes[6]; 30 31 /* TODO move it elsewhere. */ 32 vec4 CameraTexCoFactors; 33 }; 34 35 #ifdef world_clip_planes_calc_clip_distance 36 # undef world_clip_planes_calc_clip_distance 37 # define world_clip_planes_calc_clip_distance(p) \ 38 _world_clip_planes_calc_clip_distance(p, clipPlanes) 39 #endif 40 41 #ifdef COMMON_GLOBALS_LIB 42 float mul_project_m4_v3_zfac(in vec3 co) 43 { 44 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 45 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 46 } 47 #endif 48 49 /* Not the right place but need to be common to all overlay's. 50 * TODO Split to an overlay lib. */ 51 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 52 { 53 const float div = 1.0 / 255.0; 54 int a = int(mat[0][3]); 55 int b = int(mat[1][3]); 56 int c = int(mat[2][3]); 57 int d = int(mat[3][3]); 58 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 59 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 60 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 61 mat[3][3] = 1.0; 62 return mat; 63 } 64 65 /* Same here, Not the right place but need to be common to all overlay's. 66 * TODO Split to an overlay lib. */ 67 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 68 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 69 { 70 vec2 edge = edge_start - edge_pos; 71 float len = length(edge); 72 if (len > 0.0) { 73 edge /= len; 74 vec2 perp = vec2(-edge.y, edge.x); 75 float dist = dot(perp, frag_co - edge_start); 76 /* Add 0.1 to diffenrentiate with cleared pixels. */ 77 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 78 } 79 else { 80 /* Default line if the origin is perfectly aligned with a pixel. */ 81 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 82 } 83 } 84 85 uniform int resourceChunk; 86 87 #ifdef GPU_VERTEX_SHADER 88 # ifdef GL_ARB_shader_draw_parameters 89 # define baseInstance gl_BaseInstanceARB 90 # else /* no ARB_shader_draw_parameters */ 91 uniform int baseInstance; 92 # endif 93 94 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 95 /* When drawing instances of an object at the same position. */ 96 # define instanceId 0 97 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 98 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 99 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 100 * Ignore gl_InstanceID then. */ 101 # define instanceId 0 102 # else 103 # define instanceId gl_InstanceID 104 # endif 105 106 # define resource_id (baseInstance + instanceId) 107 108 /* Use this to declare and pass the value if 109 * the fragment shader uses the resource_id. */ 110 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 111 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 112 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 113 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 114 #endif 115 116 /* If used in a fragment / geometry shader, we pass 117 * resource_id as varying. */ 118 #ifdef GPU_GEOMETRY_SHADER 119 # define RESOURCE_ID_VARYING \ 120 flat out int resourceIDFrag; \ 121 flat in int resourceIDGeom[]; 122 123 # define resource_id resourceIDGeom 124 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 125 #endif 126 127 #ifdef GPU_FRAGMENT_SHADER 128 flat in int resourceIDFrag; 129 # define resource_id resourceIDFrag 130 #endif 131 132 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 133 !defined(INSTANCED_ATTRIB) 134 struct ObjectMatrices { 135 mat4 drw_modelMatrix; 136 mat4 drw_modelMatrixInverse; 137 }; 138 139 layout(std140) uniform modelBlock 140 { 141 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 142 }; 143 144 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 145 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 146 147 #else /* GPU_INTEL */ 148 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 149 * So for now we just force using the legacy path. */ 150 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 151 * and older amd driver on windows. */ 152 uniform mat4 ModelMatrix; 153 uniform mat4 ModelMatrixInverse; 154 #endif 155 156 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 157 158 /** Transform shortcuts. */ 159 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 160 * will always be decomposed in at least 2 matrix operation. */ 161 162 /** 163 * Some clarification: 164 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 165 * 166 * But since it is slow to multiply matrices we decompose it. Decomposing 167 * inversion and transposition both invert the product order leaving us with 168 * the same original order: 169 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 170 * 171 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 172 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 173 * ViewMatrix * transpose(ModelMatrixInverse) 174 **/ 175 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 176 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 177 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 178 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 179 180 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 181 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 182 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 183 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 184 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 185 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 186 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 187 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 188 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 189 190 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 191 * to make vertex shaders work. even if it's actually dead code. */ 192 #ifdef GPU_INTEL 193 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 194 #else 195 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 196 #endif 197 198 #define DRW_BASE_SELECTED (1 << 1) 199 #define DRW_BASE_FROM_DUPLI (1 << 2) 200 #define DRW_BASE_FROM_SET (1 << 3) 201 #define DRW_BASE_ACTIVE (1 << 4) 202 203 #ifndef HAIR_SHADER 204 in vec3 pos; 205 in vec3 nor; 206 in vec2 au; /* active texture layer */ 207 # ifdef V3D_SHADING_VERTEX_COLOR 208 in vec3 ac; /* active color */ 209 # endif 210 # define uv au 211 #else /* HAIR_SHADER */ 212 # ifdef V3D_SHADING_TEXTURE_COLOR 213 uniform samplerBuffer au; /* active texture layer */ 214 # endif 215 flat out float hair_rand; 216 #endif /* HAIR_SHADER */ 217 218 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 219 out vec3 normal_viewport; 220 #endif 221 222 #ifdef V3D_SHADING_TEXTURE_COLOR 223 out vec2 uv_interp; 224 #endif 225 #ifdef V3D_SHADING_VERTEX_COLOR 226 out vec3 vertexColor; 227 #endif 228 229 #ifdef OBJECT_ID_PASS_ENABLED 230 RESOURCE_ID_VARYING 231 #endif 232 233 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 234 float integer_noise(int n) 235 { 236 n = (n >> 13) ^ n; 237 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 238 return (float(nn) / 1073741824.0); 239 } 240 241 #ifdef V3D_SHADING_VERTEX_COLOR 242 vec3 srgb_to_linear_attr(vec3 c) 243 { 244 c = max(c, vec3(0.0)); 245 vec3 c1 = c * (1.0 / 12.92); 246 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 247 return mix(c1, c2, step(vec3(0.04045), c)); 248 } 249 #endif 250 251 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 252 { 253 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 254 vec3 nor = cross(tan, binor); 255 nor = normalize(mix(nor, -tan, rand * 0.1)); 256 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 257 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 258 nor = nor * sin_theta + binor * cos_theta; 259 return nor; 260 } 261 262 void main() 263 { 264 #ifdef HAIR_SHADER 265 # ifdef V3D_SHADING_TEXTURE_COLOR 266 vec2 uv = hair_get_customdata_vec2(au); 267 # endif 268 float time, thick_time, thickness; 269 vec3 world_pos, tan, binor; 270 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 271 ModelMatrixInverse, 272 ViewMatrixInverse[3].xyz, 273 ViewMatrixInverse[2].xyz, 274 world_pos, 275 tan, 276 binor, 277 time, 278 thickness, 279 thick_time); 280 281 hair_rand = integer_noise(hair_get_strand_id()); 282 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 283 #else 284 vec3 world_pos = point_object_to_world(pos); 285 #endif 286 gl_Position = point_world_to_ndc(world_pos); 287 288 #ifdef V3D_SHADING_TEXTURE_COLOR 289 uv_interp = uv; 290 #endif 291 292 #ifdef V3D_SHADING_VERTEX_COLOR 293 # ifndef HAIR_SHADER 294 vertexColor = srgb_to_linear_attr(ac); 295 # endif 296 #endif 297 298 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 299 # ifndef HAIR_SHADER 300 normal_viewport = normal_object_to_view(nor); 301 normal_viewport = normalize(normal_viewport); 302 # else 303 normal_viewport = normal_world_to_view(nor); 304 # endif 305 #endif 306 307 #ifdef OBJECT_ID_PASS_ENABLED 308 PASS_RESOURCE_ID 309 #endif 310 311 #ifdef USE_WORLD_CLIP_PLANES 312 world_clip_planes_calc_clip_distance(world_pos); 313 #endif 314 } 0(132) : error C0105: Syntax error in #if 0(132) : error C0105: Syntax error in #if 0(133) : error C0000: syntax error, unexpected '!' at token "!" 0(137) : error C0000: syntax error, unexpected '}' at token "}" 0(141) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS 15 #define HAIR_SHADER ===== shader string 6 ==== 16 /** 17 * Library to create hairs dynamically from control points. 18 * This is less bandwidth intensive than fetching the vertex attributes 19 * but does more ALU work per vertex. This also reduces the amount 20 * of data the CPU has to precompute and transfer for each update. 21 */ 22 23 /** 24 * hairStrandsRes: Number of points per hair strand. 25 * 2 - no subdivision 26 * 3+ - 1 or more interpolated points per hair. 27 */ 28 uniform int hairStrandsRes = 8; 29 30 /** 31 * hairThicknessRes : Subdiv around the hair. 32 * 1 - Wire Hair: Only one pixel thick, independent of view distance. 33 * 2 - Polystrip Hair: Correct width, flat if camera is parallel. 34 * 3+ - Cylinder Hair: Massive calculation but potentially perfect. Still need proper support. 35 */ 36 uniform int hairThicknessRes = 1; 37 38 /* Hair thickness shape. */ 39 uniform float hairRadRoot = 0.01; 40 uniform float hairRadTip = 0.0; 41 uniform float hairRadShape = 0.5; 42 uniform bool hairCloseTip = true; 43 44 uniform mat4 hairDupliMatrix; 45 46 /* -- Per control points -- */ 47 uniform samplerBuffer hairPointBuffer; /* RGBA32F */ 48 #define point_position xyz 49 #define point_time w /* Position along the hair length */ 50 51 /* -- Per strands data -- */ 52 uniform usamplerBuffer hairStrandBuffer; /* R32UI */ 53 uniform usamplerBuffer hairStrandSegBuffer; /* R16UI */ 54 55 /* Not used, use one buffer per uv layer */ 56 // uniform samplerBuffer hairUVBuffer; /* RG32F */ 57 // uniform samplerBuffer hairColBuffer; /* RGBA16 linear color */ 58 59 /* -- Subdivision stage -- */ 60 /** 61 * We use a transform feedback to preprocess the strands and add more subdivision to it. 62 * For the moment these are simple smooth interpolation but one could hope to see the full 63 * children particle modifiers being evaluated at this stage. 64 * 65 * If no more subdivision is needed, we can skip this step. 66 */ 67 68 #ifdef HAIR_PHASE_SUBDIV 69 int hair_get_base_id(float local_time, int strand_segments, out float interp_time) 70 { 71 float time_per_strand_seg = 1.0 / float(strand_segments); 72 73 float ratio = local_time / time_per_strand_seg; 74 interp_time = fract(ratio); 75 76 return int(ratio); 77 } 78 79 void hair_get_interp_attrs( 80 out vec4 data0, out vec4 data1, out vec4 data2, out vec4 data3, out float interp_time) 81 { 82 float local_time = float(gl_VertexID % hairStrandsRes) / float(hairStrandsRes - 1); 83 84 int hair_id = gl_VertexID / hairStrandsRes; 85 int strand_offset = int(texelFetch(hairStrandBuffer, hair_id).x); 86 int strand_segments = int(texelFetch(hairStrandSegBuffer, hair_id).x); 87 88 int id = hair_get_base_id(local_time, strand_segments, interp_time); 89 90 int ofs_id = id + strand_offset; 91 92 data0 = texelFetch(hairPointBuffer, ofs_id - 1); 93 data1 = texelFetch(hairPointBuffer, ofs_id); 94 data2 = texelFetch(hairPointBuffer, ofs_id + 1); 95 data3 = texelFetch(hairPointBuffer, ofs_id + 2); 96 97 if (id <= 0) { 98 /* root points. Need to reconstruct previous data. */ 99 data0 = data1 * 2.0 - data2; 100 } 101 if (id + 1 >= strand_segments) { 102 /* tip points. Need to reconstruct next data. */ 103 data3 = data2 * 2.0 - data1; 104 } 105 } 106 #endif 107 108 /* -- Drawing stage -- */ 109 /** 110 * For final drawing, the vertex index and the number of vertex per segment 111 */ 112 113 #ifndef HAIR_PHASE_SUBDIV 114 int hair_get_strand_id(void) 115 { 116 return gl_VertexID / (hairStrandsRes * hairThicknessRes); 117 } 118 119 int hair_get_base_id(void) 120 { 121 return gl_VertexID / hairThicknessRes; 122 } 123 124 /* Copied from cycles. */ 125 float hair_shaperadius(float shape, float root, float tip, float time) 126 { 127 float radius = 1.0 - time; 128 129 if (shape < 0.0) { 130 radius = pow(radius, 1.0 + shape); 131 } 132 else { 133 radius = pow(radius, 1.0 / (1.0 - shape)); 134 } 135 136 if (hairCloseTip && (time > 0.99)) { 137 return 0.0; 138 } 139 140 return (radius * (root - tip)) + tip; 141 } 142 143 # ifdef OS_MAC 144 in float dummy; 145 # endif 146 147 void hair_get_pos_tan_binor_time(bool is_persp, 148 mat4 invmodel_mat, 149 vec3 camera_pos, 150 vec3 camera_z, 151 out vec3 wpos, 152 out vec3 wtan, 153 out vec3 wbinor, 154 out float time, 155 out float thickness, 156 out float thick_time) 157 { 158 int id = hair_get_base_id(); 159 vec4 data = texelFetch(hairPointBuffer, id); 160 wpos = data.point_position; 161 time = data.point_time; 162 163 # ifdef OS_MAC 164 /* Generate a dummy read to avoid the driver bug with shaders having no 165 * vertex reads on macOS (T60171) */ 166 wpos.y += dummy * 0.0; 167 # endif 168 169 if (time == 0.0) { 170 /* Hair root */ 171 wtan = texelFetch(hairPointBuffer, id + 1).point_position - wpos; 172 } 173 else { 174 wtan = wpos - texelFetch(hairPointBuffer, id - 1).point_position; 175 } 176 177 wpos = (hairDupliMatrix * vec4(wpos, 1.0)).xyz; 178 wtan = -normalize(mat3(hairDupliMatrix) * wtan); 179 180 vec3 camera_vec = (is_persp) ? camera_pos - wpos : camera_z; 181 wbinor = normalize(cross(camera_vec, wtan)); 182 183 thickness = hair_shaperadius(hairRadShape, hairRadRoot, hairRadTip, time); 184 185 if (hairThicknessRes > 1) { 186 thick_time = float(gl_VertexID % hairThicknessRes) / float(hairThicknessRes - 1); 187 thick_time = thickness * (thick_time * 2.0 - 1.0); 188 189 /* Take object scale into account. 190 * NOTE: This only works fine with uniform scaling. */ 191 float scale = 1.0 / length(mat3(invmodel_mat) * wbinor); 192 193 wpos += wbinor * thick_time * scale; 194 } 195 } 196 197 vec2 hair_get_customdata_vec2(const samplerBuffer cd_buf) 198 { 199 int id = hair_get_strand_id(); 200 return texelFetch(cd_buf, id).rg; 201 } 202 203 vec3 hair_get_customdata_vec3(const samplerBuffer cd_buf) 204 { 205 int id = hair_get_strand_id(); 206 return texelFetch(cd_buf, id).rgb; 207 } 208 209 vec4 hair_get_customdata_vec4(const samplerBuffer cd_buf) 210 { 211 int id = hair_get_strand_id(); 212 return texelFetch(cd_buf, id).rgba; 213 } 214 215 vec3 hair_get_strand_pos(void) 216 { 217 int id = hair_get_strand_id() * hairStrandsRes; 218 return texelFetch(hairPointBuffer, id).point_position; 219 } 220 221 #endif 222 #define COMMON_VIEW_LIB 223 #define DRW_RESOURCE_CHUNK_LEN 512 224 225 /* keep in sync with DRWManager.view_data */ 226 layout(std140) uniform viewBlock 227 { 228 /* Same order as DRWViewportMatrixType */ 229 mat4 ViewProjectionMatrix; 230 mat4 ViewProjectionMatrixInverse; 231 mat4 ViewMatrix; 232 mat4 ViewMatrixInverse; 233 mat4 ProjectionMatrix; 234 mat4 ProjectionMatrixInverse; 235 236 vec4 clipPlanes[6]; 237 238 /* TODO move it elsewhere. */ 239 vec4 CameraTexCoFactors; 240 }; 241 242 #ifdef world_clip_planes_calc_clip_distance 243 # undef world_clip_planes_calc_clip_distance 244 # define world_clip_planes_calc_clip_distance(p) \ 245 _world_clip_planes_calc_clip_distance(p, clipPlanes) 246 #endif 247 248 #ifdef COMMON_GLOBALS_LIB 249 float mul_project_m4_v3_zfac(in vec3 co) 250 { 251 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 252 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 253 } 254 #endif 255 256 /* Not the right place but need to be common to all overlay's. 257 * TODO Split to an overlay lib. */ 258 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 259 { 260 const float div = 1.0 / 255.0; 261 int a = int(mat[0][3]); 262 int b = int(mat[1][3]); 263 int c = int(mat[2][3]); 264 int d = int(mat[3][3]); 265 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 266 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 267 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 268 mat[3][3] = 1.0; 269 return mat; 270 } 271 272 /* Same here, Not the right place but need to be common to all overlay's. 273 * TODO Split to an overlay lib. */ 274 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 275 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 276 { 277 vec2 edge = edge_start - edge_pos; 278 float len = length(edge); 279 if (len > 0.0) { 280 edge /= len; 281 vec2 perp = vec2(-edge.y, edge.x); 282 float dist = dot(perp, frag_co - edge_start); 283 /* Add 0.1 to diffenrentiate with cleared pixels. */ 284 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 285 } 286 else { 287 /* Default line if the origin is perfectly aligned with a pixel. */ 288 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 289 } 290 } 291 292 uniform int resourceChunk; 293 294 #ifdef GPU_VERTEX_SHADER 295 # ifdef GL_ARB_shader_draw_parameters 296 # define baseInstance gl_BaseInstanceARB 297 # else /* no ARB_shader_draw_parameters */ 298 uniform int baseInstance; 299 # endif 300 301 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 302 /* When drawing instances of an object at the same position. */ 303 # define instanceId 0 304 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 305 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 306 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 307 * Ignore gl_InstanceID then. */ 308 # define instanceId 0 309 # else 310 # define instanceId gl_InstanceID 311 # endif 312 313 # define resource_id (baseInstance + instanceId) 314 315 /* Use this to declare and pass the value if 316 * the fragment shader uses the resource_id. */ 317 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 318 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 319 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 320 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 321 #endif 322 323 /* If used in a fragment / geometry shader, we pass 324 * resource_id as varying. */ 325 #ifdef GPU_GEOMETRY_SHADER 326 # define RESOURCE_ID_VARYING \ 327 flat out int resourceIDFrag; \ 328 flat in int resourceIDGeom[]; 329 330 # define resource_id resourceIDGeom 331 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 332 #endif 333 334 #ifdef GPU_FRAGMENT_SHADER 335 flat in int resourceIDFrag; 336 # define resource_id resourceIDFrag 337 #endif 338 339 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 340 !defined(INSTANCED_ATTRIB) 341 struct ObjectMatrices { 342 mat4 drw_modelMatrix; 343 mat4 drw_modelMatrixInverse; 344 }; 345 346 layout(std140) uniform modelBlock 347 { 348 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 349 }; 350 351 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 352 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 353 354 #else /* GPU_INTEL */ 355 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 356 * So for now we just force using the legacy path. */ 357 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 358 * and older amd driver on windows. */ 359 uniform mat4 ModelMatrix; 360 uniform mat4 ModelMatrixInverse; 361 #endif 362 363 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 364 365 /** Transform shortcuts. */ 366 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 367 * will always be decomposed in at least 2 matrix operation. */ 368 369 /** 370 * Some clarification: 371 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 372 * 373 * But since it is slow to multiply matrices we decompose it. Decomposing 374 * inversion and transposition both invert the product order leaving us with 375 * the same original order: 376 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 377 * 378 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 379 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 380 * ViewMatrix * transpose(ModelMatrixInverse) 381 **/ 382 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 383 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 384 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 385 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 386 387 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 388 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 389 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 390 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 391 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 392 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 393 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 394 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 395 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 396 397 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 398 * to make vertex shaders work. even if it's actually dead code. */ 399 #ifdef GPU_INTEL 400 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 401 #else 402 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 403 #endif 404 405 #define DRW_BASE_SELECTED (1 << 1) 406 #define DRW_BASE_FROM_DUPLI (1 << 2) 407 #define DRW_BASE_FROM_SET (1 << 3) 408 #define DRW_BASE_ACTIVE (1 << 4) 409 410 #ifndef HAIR_SHADER 411 in vec3 pos; 412 in vec3 nor; 413 in vec2 au; /* active texture layer */ 414 # ifdef V3D_SHADING_VERTEX_COLOR 415 in vec3 ac; /* active color */ 416 # endif 417 # define uv au 418 #else /* HAIR_SHADER */ 419 # ifdef V3D_SHADING_TEXTURE_COLOR 420 uniform samplerBuffer au; /* active texture layer */ 421 # endif 422 flat out float hair_rand; 423 #endif /* HAIR_SHADER */ 424 425 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 426 out vec3 normal_viewport; 427 #endif 428 429 #ifdef V3D_SHADING_TEXTURE_COLOR 430 out vec2 uv_interp; 431 #endif 432 #ifdef V3D_SHADING_VERTEX_COLOR 433 out vec3 vertexColor; 434 #endif 435 436 #ifdef OBJECT_ID_PASS_ENABLED 437 RESOURCE_ID_VARYING 438 #endif 439 440 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 441 float integer_noise(int n) 442 { 443 n = (n >> 13) ^ n; 444 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 445 return (float(nn) / 1073741824.0); 446 } 447 448 #ifdef V3D_SHADING_VERTEX_COLOR 449 vec3 srgb_to_linear_attr(vec3 c) 450 { 451 c = max(c, vec3(0.0)); 452 vec3 c1 = c * (1.0 / 12.92); 453 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 454 return mix(c1, c2, step(vec3(0.04045), c)); 455 } 456 #endif 457 458 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 459 { 460 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 461 vec3 nor = cross(tan, binor); 462 nor = normalize(mix(nor, -tan, rand * 0.1)); 463 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 464 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 465 nor = nor * sin_theta + binor * cos_theta; 466 return nor; 467 } 468 469 void main() 470 { 471 #ifdef HAIR_SHADER 472 # ifdef V3D_SHADING_TEXTURE_COLOR 473 vec2 uv = hair_get_customdata_vec2(au); 474 # endif 475 float time, thick_time, thickness; 476 vec3 world_pos, tan, binor; 477 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 478 ModelMatrixInverse, 479 ViewMatrixInverse[3].xyz, 480 ViewMatrixInverse[2].xyz, 481 world_pos, 482 tan, 483 binor, 484 time, 485 thickness, 486 thick_time); 487 488 hair_rand = integer_noise(hair_get_strand_id()); 489 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 490 #else 491 vec3 world_pos = point_object_to_world(pos); 492 #endif 493 gl_Position = point_world_to_ndc(world_pos); 494 495 #ifdef V3D_SHADING_TEXTURE_COLOR 496 uv_interp = uv; 497 #endif 498 499 #ifdef V3D_SHADING_VERTEX_COLOR 500 # ifndef HAIR_SHADER 501 vertexColor = srgb_to_linear_attr(ac); 502 # endif 503 #endif 504 505 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 506 # ifndef HAIR_SHADER 507 normal_viewport = normal_object_to_view(nor); 508 normal_viewport = normalize(normal_viewport); 509 # else 510 normal_viewport = normal_world_to_view(nor); 511 # endif 512 #endif 513 514 #ifdef OBJECT_ID_PASS_ENABLED 515 PASS_RESOURCE_ID 516 #endif 517 518 #ifdef USE_WORLD_CLIP_PLANES 519 world_clip_planes_calc_clip_distance(world_pos); 520 #endif 521 } 0(339) : error C0105: Syntax error in #if 0(339) : error C0105: Syntax error in #if 0(340) : error C0000: syntax error, unexpected '!' at token "!" 0(344) : error C0000: syntax error, unexpected '}' at token "}" 0(348) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS ===== shader string 6 ==== 15 #define COMMON_VIEW_LIB 16 #define DRW_RESOURCE_CHUNK_LEN 512 17 18 /* keep in sync with DRWManager.view_data */ 19 layout(std140) uniform viewBlock 20 { 21 /* Same order as DRWViewportMatrixType */ 22 mat4 ViewProjectionMatrix; 23 mat4 ViewProjectionMatrixInverse; 24 mat4 ViewMatrix; 25 mat4 ViewMatrixInverse; 26 mat4 ProjectionMatrix; 27 mat4 ProjectionMatrixInverse; 28 29 vec4 clipPlanes[6]; 30 31 /* TODO move it elsewhere. */ 32 vec4 CameraTexCoFactors; 33 }; 34 35 #ifdef world_clip_planes_calc_clip_distance 36 # undef world_clip_planes_calc_clip_distance 37 # define world_clip_planes_calc_clip_distance(p) \ 38 _world_clip_planes_calc_clip_distance(p, clipPlanes) 39 #endif 40 41 #ifdef COMMON_GLOBALS_LIB 42 float mul_project_m4_v3_zfac(in vec3 co) 43 { 44 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 45 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 46 } 47 #endif 48 49 /* Not the right place but need to be common to all overlay's. 50 * TODO Split to an overlay lib. */ 51 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 52 { 53 const float div = 1.0 / 255.0; 54 int a = int(mat[0][3]); 55 int b = int(mat[1][3]); 56 int c = int(mat[2][3]); 57 int d = int(mat[3][3]); 58 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 59 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 60 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 61 mat[3][3] = 1.0; 62 return mat; 63 } 64 65 /* Same here, Not the right place but need to be common to all overlay's. 66 * TODO Split to an overlay lib. */ 67 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 68 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 69 { 70 vec2 edge = edge_start - edge_pos; 71 float len = length(edge); 72 if (len > 0.0) { 73 edge /= len; 74 vec2 perp = vec2(-edge.y, edge.x); 75 float dist = dot(perp, frag_co - edge_start); 76 /* Add 0.1 to diffenrentiate with cleared pixels. */ 77 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 78 } 79 else { 80 /* Default line if the origin is perfectly aligned with a pixel. */ 81 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 82 } 83 } 84 85 uniform int resourceChunk; 86 87 #ifdef GPU_VERTEX_SHADER 88 # ifdef GL_ARB_shader_draw_parameters 89 # define baseInstance gl_BaseInstanceARB 90 # else /* no ARB_shader_draw_parameters */ 91 uniform int baseInstance; 92 # endif 93 94 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 95 /* When drawing instances of an object at the same position. */ 96 # define instanceId 0 97 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 98 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 99 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 100 * Ignore gl_InstanceID then. */ 101 # define instanceId 0 102 # else 103 # define instanceId gl_InstanceID 104 # endif 105 106 # define resource_id (baseInstance + instanceId) 107 108 /* Use this to declare and pass the value if 109 * the fragment shader uses the resource_id. */ 110 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 111 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 112 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 113 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 114 #endif 115 116 /* If used in a fragment / geometry shader, we pass 117 * resource_id as varying. */ 118 #ifdef GPU_GEOMETRY_SHADER 119 # define RESOURCE_ID_VARYING \ 120 flat out int resourceIDFrag; \ 121 flat in int resourceIDGeom[]; 122 123 # define resource_id resourceIDGeom 124 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 125 #endif 126 127 #ifdef GPU_FRAGMENT_SHADER 128 flat in int resourceIDFrag; 129 # define resource_id resourceIDFrag 130 #endif 131 132 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 133 !defined(INSTANCED_ATTRIB) 134 struct ObjectMatrices { 135 mat4 drw_modelMatrix; 136 mat4 drw_modelMatrixInverse; 137 }; 138 139 layout(std140) uniform modelBlock 140 { 141 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 142 }; 143 144 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 145 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 146 147 #else /* GPU_INTEL */ 148 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 149 * So for now we just force using the legacy path. */ 150 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 151 * and older amd driver on windows. */ 152 uniform mat4 ModelMatrix; 153 uniform mat4 ModelMatrixInverse; 154 #endif 155 156 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 157 158 /** Transform shortcuts. */ 159 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 160 * will always be decomposed in at least 2 matrix operation. */ 161 162 /** 163 * Some clarification: 164 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 165 * 166 * But since it is slow to multiply matrices we decompose it. Decomposing 167 * inversion and transposition both invert the product order leaving us with 168 * the same original order: 169 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 170 * 171 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 172 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 173 * ViewMatrix * transpose(ModelMatrixInverse) 174 **/ 175 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 176 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 177 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 178 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 179 180 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 181 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 182 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 183 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 184 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 185 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 186 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 187 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 188 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 189 190 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 191 * to make vertex shaders work. even if it's actually dead code. */ 192 #ifdef GPU_INTEL 193 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 194 #else 195 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 196 #endif 197 198 #define DRW_BASE_SELECTED (1 << 1) 199 #define DRW_BASE_FROM_DUPLI (1 << 2) 200 #define DRW_BASE_FROM_SET (1 << 3) 201 #define DRW_BASE_ACTIVE (1 << 4) 202 203 #ifndef HAIR_SHADER 204 in vec3 pos; 205 in vec3 nor; 206 in vec2 au; /* active texture layer */ 207 # ifdef V3D_SHADING_VERTEX_COLOR 208 in vec3 ac; /* active color */ 209 # endif 210 # define uv au 211 #else /* HAIR_SHADER */ 212 # ifdef V3D_SHADING_TEXTURE_COLOR 213 uniform samplerBuffer au; /* active texture layer */ 214 # endif 215 flat out float hair_rand; 216 #endif /* HAIR_SHADER */ 217 218 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 219 out vec3 normal_viewport; 220 #endif 221 222 #ifdef V3D_SHADING_TEXTURE_COLOR 223 out vec2 uv_interp; 224 #endif 225 #ifdef V3D_SHADING_VERTEX_COLOR 226 out vec3 vertexColor; 227 #endif 228 229 #ifdef OBJECT_ID_PASS_ENABLED 230 RESOURCE_ID_VARYING 231 #endif 232 233 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 234 float integer_noise(int n) 235 { 236 n = (n >> 13) ^ n; 237 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 238 return (float(nn) / 1073741824.0); 239 } 240 241 #ifdef V3D_SHADING_VERTEX_COLOR 242 vec3 srgb_to_linear_attr(vec3 c) 243 { 244 c = max(c, vec3(0.0)); 245 vec3 c1 = c * (1.0 / 12.92); 246 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 247 return mix(c1, c2, step(vec3(0.04045), c)); 248 } 249 #endif 250 251 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 252 { 253 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 254 vec3 nor = cross(tan, binor); 255 nor = normalize(mix(nor, -tan, rand * 0.1)); 256 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 257 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 258 nor = nor * sin_theta + binor * cos_theta; 259 return nor; 260 } 261 262 void main() 263 { 264 #ifdef HAIR_SHADER 265 # ifdef V3D_SHADING_TEXTURE_COLOR 266 vec2 uv = hair_get_customdata_vec2(au); 267 # endif 268 float time, thick_time, thickness; 269 vec3 world_pos, tan, binor; 270 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 271 ModelMatrixInverse, 272 ViewMatrixInverse[3].xyz, 273 ViewMatrixInverse[2].xyz, 274 world_pos, 275 tan, 276 binor, 277 time, 278 thickness, 279 thick_time); 280 281 hair_rand = integer_noise(hair_get_strand_id()); 282 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 283 #else 284 vec3 world_pos = point_object_to_world(pos); 285 #endif 286 gl_Position = point_world_to_ndc(world_pos); 287 288 #ifdef V3D_SHADING_TEXTURE_COLOR 289 uv_interp = uv; 290 #endif 291 292 #ifdef V3D_SHADING_VERTEX_COLOR 293 # ifndef HAIR_SHADER 294 vertexColor = srgb_to_linear_attr(ac); 295 # endif 296 #endif 297 298 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 299 # ifndef HAIR_SHADER 300 normal_viewport = normal_object_to_view(nor); 301 normal_viewport = normalize(normal_viewport); 302 # else 303 normal_viewport = normal_world_to_view(nor); 304 # endif 305 #endif 306 307 #ifdef OBJECT_ID_PASS_ENABLED 308 PASS_RESOURCE_ID 309 #endif 310 311 #ifdef USE_WORLD_CLIP_PLANES 312 world_clip_planes_calc_clip_distance(world_pos); 313 #endif 314 } 0(132) : error C0105: Syntax error in #if 0(132) : error C0105: Syntax error in #if 0(133) : error C0000: syntax error, unexpected '!' at token "!" 0(137) : error C0000: syntax error, unexpected '}' at token "}" 0(141) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS 15 #define HAIR_SHADER ===== shader string 6 ==== 16 /** 17 * Library to create hairs dynamically from control points. 18 * This is less bandwidth intensive than fetching the vertex attributes 19 * but does more ALU work per vertex. This also reduces the amount 20 * of data the CPU has to precompute and transfer for each update. 21 */ 22 23 /** 24 * hairStrandsRes: Number of points per hair strand. 25 * 2 - no subdivision 26 * 3+ - 1 or more interpolated points per hair. 27 */ 28 uniform int hairStrandsRes = 8; 29 30 /** 31 * hairThicknessRes : Subdiv around the hair. 32 * 1 - Wire Hair: Only one pixel thick, independent of view distance. 33 * 2 - Polystrip Hair: Correct width, flat if camera is parallel. 34 * 3+ - Cylinder Hair: Massive calculation but potentially perfect. Still need proper support. 35 */ 36 uniform int hairThicknessRes = 1; 37 38 /* Hair thickness shape. */ 39 uniform float hairRadRoot = 0.01; 40 uniform float hairRadTip = 0.0; 41 uniform float hairRadShape = 0.5; 42 uniform bool hairCloseTip = true; 43 44 uniform mat4 hairDupliMatrix; 45 46 /* -- Per control points -- */ 47 uniform samplerBuffer hairPointBuffer; /* RGBA32F */ 48 #define point_position xyz 49 #define point_time w /* Position along the hair length */ 50 51 /* -- Per strands data -- */ 52 uniform usamplerBuffer hairStrandBuffer; /* R32UI */ 53 uniform usamplerBuffer hairStrandSegBuffer; /* R16UI */ 54 55 /* Not used, use one buffer per uv layer */ 56 // uniform samplerBuffer hairUVBuffer; /* RG32F */ 57 // uniform samplerBuffer hairColBuffer; /* RGBA16 linear color */ 58 59 /* -- Subdivision stage -- */ 60 /** 61 * We use a transform feedback to preprocess the strands and add more subdivision to it. 62 * For the moment these are simple smooth interpolation but one could hope to see the full 63 * children particle modifiers being evaluated at this stage. 64 * 65 * If no more subdivision is needed, we can skip this step. 66 */ 67 68 #ifdef HAIR_PHASE_SUBDIV 69 int hair_get_base_id(float local_time, int strand_segments, out float interp_time) 70 { 71 float time_per_strand_seg = 1.0 / float(strand_segments); 72 73 float ratio = local_time / time_per_strand_seg; 74 interp_time = fract(ratio); 75 76 return int(ratio); 77 } 78 79 void hair_get_interp_attrs( 80 out vec4 data0, out vec4 data1, out vec4 data2, out vec4 data3, out float interp_time) 81 { 82 float local_time = float(gl_VertexID % hairStrandsRes) / float(hairStrandsRes - 1); 83 84 int hair_id = gl_VertexID / hairStrandsRes; 85 int strand_offset = int(texelFetch(hairStrandBuffer, hair_id).x); 86 int strand_segments = int(texelFetch(hairStrandSegBuffer, hair_id).x); 87 88 int id = hair_get_base_id(local_time, strand_segments, interp_time); 89 90 int ofs_id = id + strand_offset; 91 92 data0 = texelFetch(hairPointBuffer, ofs_id - 1); 93 data1 = texelFetch(hairPointBuffer, ofs_id); 94 data2 = texelFetch(hairPointBuffer, ofs_id + 1); 95 data3 = texelFetch(hairPointBuffer, ofs_id + 2); 96 97 if (id <= 0) { 98 /* root points. Need to reconstruct previous data. */ 99 data0 = data1 * 2.0 - data2; 100 } 101 if (id + 1 >= strand_segments) { 102 /* tip points. Need to reconstruct next data. */ 103 data3 = data2 * 2.0 - data1; 104 } 105 } 106 #endif 107 108 /* -- Drawing stage -- */ 109 /** 110 * For final drawing, the vertex index and the number of vertex per segment 111 */ 112 113 #ifndef HAIR_PHASE_SUBDIV 114 int hair_get_strand_id(void) 115 { 116 return gl_VertexID / (hairStrandsRes * hairThicknessRes); 117 } 118 119 int hair_get_base_id(void) 120 { 121 return gl_VertexID / hairThicknessRes; 122 } 123 124 /* Copied from cycles. */ 125 float hair_shaperadius(float shape, float root, float tip, float time) 126 { 127 float radius = 1.0 - time; 128 129 if (shape < 0.0) { 130 radius = pow(radius, 1.0 + shape); 131 } 132 else { 133 radius = pow(radius, 1.0 / (1.0 - shape)); 134 } 135 136 if (hairCloseTip && (time > 0.99)) { 137 return 0.0; 138 } 139 140 return (radius * (root - tip)) + tip; 141 } 142 143 # ifdef OS_MAC 144 in float dummy; 145 # endif 146 147 void hair_get_pos_tan_binor_time(bool is_persp, 148 mat4 invmodel_mat, 149 vec3 camera_pos, 150 vec3 camera_z, 151 out vec3 wpos, 152 out vec3 wtan, 153 out vec3 wbinor, 154 out float time, 155 out float thickness, 156 out float thick_time) 157 { 158 int id = hair_get_base_id(); 159 vec4 data = texelFetch(hairPointBuffer, id); 160 wpos = data.point_position; 161 time = data.point_time; 162 163 # ifdef OS_MAC 164 /* Generate a dummy read to avoid the driver bug with shaders having no 165 * vertex reads on macOS (T60171) */ 166 wpos.y += dummy * 0.0; 167 # endif 168 169 if (time == 0.0) { 170 /* Hair root */ 171 wtan = texelFetch(hairPointBuffer, id + 1).point_position - wpos; 172 } 173 else { 174 wtan = wpos - texelFetch(hairPointBuffer, id - 1).point_position; 175 } 176 177 wpos = (hairDupliMatrix * vec4(wpos, 1.0)).xyz; 178 wtan = -normalize(mat3(hairDupliMatrix) * wtan); 179 180 vec3 camera_vec = (is_persp) ? camera_pos - wpos : camera_z; 181 wbinor = normalize(cross(camera_vec, wtan)); 182 183 thickness = hair_shaperadius(hairRadShape, hairRadRoot, hairRadTip, time); 184 185 if (hairThicknessRes > 1) { 186 thick_time = float(gl_VertexID % hairThicknessRes) / float(hairThicknessRes - 1); 187 thick_time = thickness * (thick_time * 2.0 - 1.0); 188 189 /* Take object scale into account. 190 * NOTE: This only works fine with uniform scaling. */ 191 float scale = 1.0 / length(mat3(invmodel_mat) * wbinor); 192 193 wpos += wbinor * thick_time * scale; 194 } 195 } 196 197 vec2 hair_get_customdata_vec2(const samplerBuffer cd_buf) 198 { 199 int id = hair_get_strand_id(); 200 return texelFetch(cd_buf, id).rg; 201 } 202 203 vec3 hair_get_customdata_vec3(const samplerBuffer cd_buf) 204 { 205 int id = hair_get_strand_id(); 206 return texelFetch(cd_buf, id).rgb; 207 } 208 209 vec4 hair_get_customdata_vec4(const samplerBuffer cd_buf) 210 { 211 int id = hair_get_strand_id(); 212 return texelFetch(cd_buf, id).rgba; 213 } 214 215 vec3 hair_get_strand_pos(void) 216 { 217 int id = hair_get_strand_id() * hairStrandsRes; 218 return texelFetch(hairPointBuffer, id).point_position; 219 } 220 221 #endif 222 #define COMMON_VIEW_LIB 223 #define DRW_RESOURCE_CHUNK_LEN 512 224 225 /* keep in sync with DRWManager.view_data */ 226 layout(std140) uniform viewBlock 227 { 228 /* Same order as DRWViewportMatrixType */ 229 mat4 ViewProjectionMatrix; 230 mat4 ViewProjectionMatrixInverse; 231 mat4 ViewMatrix; 232 mat4 ViewMatrixInverse; 233 mat4 ProjectionMatrix; 234 mat4 ProjectionMatrixInverse; 235 236 vec4 clipPlanes[6]; 237 238 /* TODO move it elsewhere. */ 239 vec4 CameraTexCoFactors; 240 }; 241 242 #ifdef world_clip_planes_calc_clip_distance 243 # undef world_clip_planes_calc_clip_distance 244 # define world_clip_planes_calc_clip_distance(p) \ 245 _world_clip_planes_calc_clip_distance(p, clipPlanes) 246 #endif 247 248 #ifdef COMMON_GLOBALS_LIB 249 float mul_project_m4_v3_zfac(in vec3 co) 250 { 251 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 252 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 253 } 254 #endif 255 256 /* Not the right place but need to be common to all overlay's. 257 * TODO Split to an overlay lib. */ 258 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 259 { 260 const float div = 1.0 / 255.0; 261 int a = int(mat[0][3]); 262 int b = int(mat[1][3]); 263 int c = int(mat[2][3]); 264 int d = int(mat[3][3]); 265 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 266 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 267 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 268 mat[3][3] = 1.0; 269 return mat; 270 } 271 272 /* Same here, Not the right place but need to be common to all overlay's. 273 * TODO Split to an overlay lib. */ 274 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 275 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 276 { 277 vec2 edge = edge_start - edge_pos; 278 float len = length(edge); 279 if (len > 0.0) { 280 edge /= len; 281 vec2 perp = vec2(-edge.y, edge.x); 282 float dist = dot(perp, frag_co - edge_start); 283 /* Add 0.1 to diffenrentiate with cleared pixels. */ 284 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 285 } 286 else { 287 /* Default line if the origin is perfectly aligned with a pixel. */ 288 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 289 } 290 } 291 292 uniform int resourceChunk; 293 294 #ifdef GPU_VERTEX_SHADER 295 # ifdef GL_ARB_shader_draw_parameters 296 # define baseInstance gl_BaseInstanceARB 297 # else /* no ARB_shader_draw_parameters */ 298 uniform int baseInstance; 299 # endif 300 301 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 302 /* When drawing instances of an object at the same position. */ 303 # define instanceId 0 304 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 305 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 306 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 307 * Ignore gl_InstanceID then. */ 308 # define instanceId 0 309 # else 310 # define instanceId gl_InstanceID 311 # endif 312 313 # define resource_id (baseInstance + instanceId) 314 315 /* Use this to declare and pass the value if 316 * the fragment shader uses the resource_id. */ 317 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 318 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 319 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 320 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 321 #endif 322 323 /* If used in a fragment / geometry shader, we pass 324 * resource_id as varying. */ 325 #ifdef GPU_GEOMETRY_SHADER 326 # define RESOURCE_ID_VARYING \ 327 flat out int resourceIDFrag; \ 328 flat in int resourceIDGeom[]; 329 330 # define resource_id resourceIDGeom 331 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 332 #endif 333 334 #ifdef GPU_FRAGMENT_SHADER 335 flat in int resourceIDFrag; 336 # define resource_id resourceIDFrag 337 #endif 338 339 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 340 !defined(INSTANCED_ATTRIB) 341 struct ObjectMatrices { 342 mat4 drw_modelMatrix; 343 mat4 drw_modelMatrixInverse; 344 }; 345 346 layout(std140) uniform modelBlock 347 { 348 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 349 }; 350 351 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 352 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 353 354 #else /* GPU_INTEL */ 355 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 356 * So for now we just force using the legacy path. */ 357 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 358 * and older amd driver on windows. */ 359 uniform mat4 ModelMatrix; 360 uniform mat4 ModelMatrixInverse; 361 #endif 362 363 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 364 365 /** Transform shortcuts. */ 366 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 367 * will always be decomposed in at least 2 matrix operation. */ 368 369 /** 370 * Some clarification: 371 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 372 * 373 * But since it is slow to multiply matrices we decompose it. Decomposing 374 * inversion and transposition both invert the product order leaving us with 375 * the same original order: 376 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 377 * 378 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 379 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 380 * ViewMatrix * transpose(ModelMatrixInverse) 381 **/ 382 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 383 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 384 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 385 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 386 387 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 388 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 389 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 390 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 391 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 392 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 393 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 394 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 395 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 396 397 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 398 * to make vertex shaders work. even if it's actually dead code. */ 399 #ifdef GPU_INTEL 400 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 401 #else 402 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 403 #endif 404 405 #define DRW_BASE_SELECTED (1 << 1) 406 #define DRW_BASE_FROM_DUPLI (1 << 2) 407 #define DRW_BASE_FROM_SET (1 << 3) 408 #define DRW_BASE_ACTIVE (1 << 4) 409 410 #ifndef HAIR_SHADER 411 in vec3 pos; 412 in vec3 nor; 413 in vec2 au; /* active texture layer */ 414 # ifdef V3D_SHADING_VERTEX_COLOR 415 in vec3 ac; /* active color */ 416 # endif 417 # define uv au 418 #else /* HAIR_SHADER */ 419 # ifdef V3D_SHADING_TEXTURE_COLOR 420 uniform samplerBuffer au; /* active texture layer */ 421 # endif 422 flat out float hair_rand; 423 #endif /* HAIR_SHADER */ 424 425 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 426 out vec3 normal_viewport; 427 #endif 428 429 #ifdef V3D_SHADING_TEXTURE_COLOR 430 out vec2 uv_interp; 431 #endif 432 #ifdef V3D_SHADING_VERTEX_COLOR 433 out vec3 vertexColor; 434 #endif 435 436 #ifdef OBJECT_ID_PASS_ENABLED 437 RESOURCE_ID_VARYING 438 #endif 439 440 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 441 float integer_noise(int n) 442 { 443 n = (n >> 13) ^ n; 444 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 445 return (float(nn) / 1073741824.0); 446 } 447 448 #ifdef V3D_SHADING_VERTEX_COLOR 449 vec3 srgb_to_linear_attr(vec3 c) 450 { 451 c = max(c, vec3(0.0)); 452 vec3 c1 = c * (1.0 / 12.92); 453 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 454 return mix(c1, c2, step(vec3(0.04045), c)); 455 } 456 #endif 457 458 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 459 { 460 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 461 vec3 nor = cross(tan, binor); 462 nor = normalize(mix(nor, -tan, rand * 0.1)); 463 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 464 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 465 nor = nor * sin_theta + binor * cos_theta; 466 return nor; 467 } 468 469 void main() 470 { 471 #ifdef HAIR_SHADER 472 # ifdef V3D_SHADING_TEXTURE_COLOR 473 vec2 uv = hair_get_customdata_vec2(au); 474 # endif 475 float time, thick_time, thickness; 476 vec3 world_pos, tan, binor; 477 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 478 ModelMatrixInverse, 479 ViewMatrixInverse[3].xyz, 480 ViewMatrixInverse[2].xyz, 481 world_pos, 482 tan, 483 binor, 484 time, 485 thickness, 486 thick_time); 487 488 hair_rand = integer_noise(hair_get_strand_id()); 489 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 490 #else 491 vec3 world_pos = point_object_to_world(pos); 492 #endif 493 gl_Position = point_world_to_ndc(world_pos); 494 495 #ifdef V3D_SHADING_TEXTURE_COLOR 496 uv_interp = uv; 497 #endif 498 499 #ifdef V3D_SHADING_VERTEX_COLOR 500 # ifndef HAIR_SHADER 501 vertexColor = srgb_to_linear_attr(ac); 502 # endif 503 #endif 504 505 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 506 # ifndef HAIR_SHADER 507 normal_viewport = normal_object_to_view(nor); 508 normal_viewport = normalize(normal_viewport); 509 # else 510 normal_viewport = normal_world_to_view(nor); 511 # endif 512 #endif 513 514 #ifdef OBJECT_ID_PASS_ENABLED 515 PASS_RESOURCE_ID 516 #endif 517 518 #ifdef USE_WORLD_CLIP_PLANES 519 world_clip_planes_calc_clip_distance(world_pos); 520 #endif 521 } 0(339) : error C0105: Syntax error in #if 0(339) : error C0105: Syntax error in #if 0(340) : error C0000: syntax error, unexpected '!' at token "!" 0(344) : error C0000: syntax error, unexpected '}' at token "}" 0(348) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_VERTEX_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define V3D_SHADING_TEXTURE_COLOR 15 #define WORKBENCH_ENCODE_NORMALS ===== shader string 6 ==== 16 #define COMMON_VIEW_LIB 17 #define DRW_RESOURCE_CHUNK_LEN 512 18 19 /* keep in sync with DRWManager.view_data */ 20 layout(std140) uniform viewBlock 21 { 22 /* Same order as DRWViewportMatrixType */ 23 mat4 ViewProjectionMatrix; 24 mat4 ViewProjectionMatrixInverse; 25 mat4 ViewMatrix; 26 mat4 ViewMatrixInverse; 27 mat4 ProjectionMatrix; 28 mat4 ProjectionMatrixInverse; 29 30 vec4 clipPlanes[6]; 31 32 /* TODO move it elsewhere. */ 33 vec4 CameraTexCoFactors; 34 }; 35 36 #ifdef world_clip_planes_calc_clip_distance 37 # undef world_clip_planes_calc_clip_distance 38 # define world_clip_planes_calc_clip_distance(p) \ 39 _world_clip_planes_calc_clip_distance(p, clipPlanes) 40 #endif 41 42 #ifdef COMMON_GLOBALS_LIB 43 float mul_project_m4_v3_zfac(in vec3 co) 44 { 45 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 46 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 47 } 48 #endif 49 50 /* Not the right place but need to be common to all overlay's. 51 * TODO Split to an overlay lib. */ 52 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 53 { 54 const float div = 1.0 / 255.0; 55 int a = int(mat[0][3]); 56 int b = int(mat[1][3]); 57 int c = int(mat[2][3]); 58 int d = int(mat[3][3]); 59 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 60 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 61 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 62 mat[3][3] = 1.0; 63 return mat; 64 } 65 66 /* Same here, Not the right place but need to be common to all overlay's. 67 * TODO Split to an overlay lib. */ 68 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 69 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 70 { 71 vec2 edge = edge_start - edge_pos; 72 float len = length(edge); 73 if (len > 0.0) { 74 edge /= len; 75 vec2 perp = vec2(-edge.y, edge.x); 76 float dist = dot(perp, frag_co - edge_start); 77 /* Add 0.1 to diffenrentiate with cleared pixels. */ 78 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 79 } 80 else { 81 /* Default line if the origin is perfectly aligned with a pixel. */ 82 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 83 } 84 } 85 86 uniform int resourceChunk; 87 88 #ifdef GPU_VERTEX_SHADER 89 # ifdef GL_ARB_shader_draw_parameters 90 # define baseInstance gl_BaseInstanceARB 91 # else /* no ARB_shader_draw_parameters */ 92 uniform int baseInstance; 93 # endif 94 95 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 96 /* When drawing instances of an object at the same position. */ 97 # define instanceId 0 98 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 99 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 100 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 101 * Ignore gl_InstanceID then. */ 102 # define instanceId 0 103 # else 104 # define instanceId gl_InstanceID 105 # endif 106 107 # define resource_id (baseInstance + instanceId) 108 109 /* Use this to declare and pass the value if 110 * the fragment shader uses the resource_id. */ 111 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 112 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 113 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 114 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 115 #endif 116 117 /* If used in a fragment / geometry shader, we pass 118 * resource_id as varying. */ 119 #ifdef GPU_GEOMETRY_SHADER 120 # define RESOURCE_ID_VARYING \ 121 flat out int resourceIDFrag; \ 122 flat in int resourceIDGeom[]; 123 124 # define resource_id resourceIDGeom 125 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 126 #endif 127 128 #ifdef GPU_FRAGMENT_SHADER 129 flat in int resourceIDFrag; 130 # define resource_id resourceIDFrag 131 #endif 132 133 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 134 !defined(INSTANCED_ATTRIB) 135 struct ObjectMatrices { 136 mat4 drw_modelMatrix; 137 mat4 drw_modelMatrixInverse; 138 }; 139 140 layout(std140) uniform modelBlock 141 { 142 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 143 }; 144 145 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 146 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 147 148 #else /* GPU_INTEL */ 149 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 150 * So for now we just force using the legacy path. */ 151 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 152 * and older amd driver on windows. */ 153 uniform mat4 ModelMatrix; 154 uniform mat4 ModelMatrixInverse; 155 #endif 156 157 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 158 159 /** Transform shortcuts. */ 160 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 161 * will always be decomposed in at least 2 matrix operation. */ 162 163 /** 164 * Some clarification: 165 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 166 * 167 * But since it is slow to multiply matrices we decompose it. Decomposing 168 * inversion and transposition both invert the product order leaving us with 169 * the same original order: 170 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 171 * 172 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 173 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 174 * ViewMatrix * transpose(ModelMatrixInverse) 175 **/ 176 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 177 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 178 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 179 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 180 181 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 182 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 183 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 184 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 185 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 186 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 187 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 188 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 189 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 190 191 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 192 * to make vertex shaders work. even if it's actually dead code. */ 193 #ifdef GPU_INTEL 194 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 195 #else 196 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 197 #endif 198 199 #define DRW_BASE_SELECTED (1 << 1) 200 #define DRW_BASE_FROM_DUPLI (1 << 2) 201 #define DRW_BASE_FROM_SET (1 << 3) 202 #define DRW_BASE_ACTIVE (1 << 4) 203 204 #ifndef HAIR_SHADER 205 in vec3 pos; 206 in vec3 nor; 207 in vec2 au; /* active texture layer */ 208 # ifdef V3D_SHADING_VERTEX_COLOR 209 in vec3 ac; /* active color */ 210 # endif 211 # define uv au 212 #else /* HAIR_SHADER */ 213 # ifdef V3D_SHADING_TEXTURE_COLOR 214 uniform samplerBuffer au; /* active texture layer */ 215 # endif 216 flat out float hair_rand; 217 #endif /* HAIR_SHADER */ 218 219 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 220 out vec3 normal_viewport; 221 #endif 222 223 #ifdef V3D_SHADING_TEXTURE_COLOR 224 out vec2 uv_interp; 225 #endif 226 #ifdef V3D_SHADING_VERTEX_COLOR 227 out vec3 vertexColor; 228 #endif 229 230 #ifdef OBJECT_ID_PASS_ENABLED 231 RESOURCE_ID_VARYING 232 #endif 233 234 /* From http://libnoise.sourceforge.net/noisegen/index.html */ 235 float integer_noise(int n) 236 { 237 n = (n >> 13) ^ n; 238 int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; 239 return (float(nn) / 1073741824.0); 240 } 241 242 #ifdef V3D_SHADING_VERTEX_COLOR 243 vec3 srgb_to_linear_attr(vec3 c) 244 { 245 c = max(c, vec3(0.0)); 246 vec3 c1 = c * (1.0 / 12.92); 247 vec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4)); 248 return mix(c1, c2, step(vec3(0.04045), c)); 249 } 250 #endif 251 252 vec3 workbench_hair_hair_normal(vec3 tan, vec3 binor, float rand) 253 { 254 /* To "simulate" anisotropic shading, randomize hair normal per strand. */ 255 vec3 nor = cross(tan, binor); 256 nor = normalize(mix(nor, -tan, rand * 0.1)); 257 float cos_theta = (rand * 2.0 - 1.0) * 0.2; 258 float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta)); 259 nor = nor * sin_theta + binor * cos_theta; 260 return nor; 261 } 262 263 void main() 264 { 265 #ifdef HAIR_SHADER 266 # ifdef V3D_SHADING_TEXTURE_COLOR 267 vec2 uv = hair_get_customdata_vec2(au); 268 # endif 269 float time, thick_time, thickness; 270 vec3 world_pos, tan, binor; 271 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 272 ModelMatrixInverse, 273 ViewMatrixInverse[3].xyz, 274 ViewMatrixInverse[2].xyz, 275 world_pos, 276 tan, 277 binor, 278 time, 279 thickness, 280 thick_time); 281 282 hair_rand = integer_noise(hair_get_strand_id()); 283 vec3 nor = workbench_hair_hair_normal(tan, binor, hair_rand); 284 #else 285 vec3 world_pos = point_object_to_world(pos); 286 #endif 287 gl_Position = point_world_to_ndc(world_pos); 288 289 #ifdef V3D_SHADING_TEXTURE_COLOR 290 uv_interp = uv; 291 #endif 292 293 #ifdef V3D_SHADING_VERTEX_COLOR 294 # ifndef HAIR_SHADER 295 vertexColor = srgb_to_linear_attr(ac); 296 # endif 297 #endif 298 299 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 300 # ifndef HAIR_SHADER 301 normal_viewport = normal_object_to_view(nor); 302 normal_viewport = normalize(normal_viewport); 303 # else 304 normal_viewport = normal_world_to_view(nor); 305 # endif 306 #endif 307 308 #ifdef OBJECT_ID_PASS_ENABLED 309 PASS_RESOURCE_ID 310 #endif 311 312 #ifdef USE_WORLD_CLIP_PLANES 313 world_clip_planes_calc_clip_distance(world_pos); 314 #endif 315 } 0(133) : error C0105: Syntax error in #if 0(133) : error C0105: Syntax error in #if 0(134) : error C0000: syntax error, unexpected '!' at token "!" 0(138) : error C0000: syntax error, unexpected '}' at token "}" 0(142) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" GPUShader: compile error: ===== shader string 1 ==== 1 #version 330 ===== shader string 2 ==== 2 #define GPU_FRAGMENT_SHADER ===== shader string 3 ==== 3 #extension GL_ARB_texture_gather: enable 4 #define GPU_ARB_texture_gather 5 #extension GL_ARB_texture_query_lod: enable 6 #extension GL_ARB_shader_draw_parameters : enable ===== shader string 4 ==== 7 #define GPU_NVIDIA 8 #define OS_WIN 9 #define DFDX_SIGN 1.0 10 #define DFDY_SIGN 1.0 ===== shader string 5 ==== 11 #define V3D_LIGHTING_STUDIO 12 #define MATDATA_PASS_ENABLED 13 #define NORMAL_VIEWPORT_PASS_ENABLED 14 #define WORKBENCH_ENCODE_NORMALS ===== shader string 6 ==== 15 #define COMMON_VIEW_LIB 16 #define DRW_RESOURCE_CHUNK_LEN 512 17 18 /* keep in sync with DRWManager.view_data */ 19 layout(std140) uniform viewBlock 20 { 21 /* Same order as DRWViewportMatrixType */ 22 mat4 ViewProjectionMatrix; 23 mat4 ViewProjectionMatrixInverse; 24 mat4 ViewMatrix; 25 mat4 ViewMatrixInverse; 26 mat4 ProjectionMatrix; 27 mat4 ProjectionMatrixInverse; 28 29 vec4 clipPlanes[6]; 30 31 /* TODO move it elsewhere. */ 32 vec4 CameraTexCoFactors; 33 }; 34 35 #ifdef world_clip_planes_calc_clip_distance 36 # undef world_clip_planes_calc_clip_distance 37 # define world_clip_planes_calc_clip_distance(p) \ 38 _world_clip_planes_calc_clip_distance(p, clipPlanes) 39 #endif 40 41 #ifdef COMMON_GLOBALS_LIB 42 float mul_project_m4_v3_zfac(in vec3 co) 43 { 44 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 45 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 46 } 47 #endif 48 49 /* Not the right place but need to be common to all overlay's. 50 * TODO Split to an overlay lib. */ 51 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 52 { 53 const float div = 1.0 / 255.0; 54 int a = int(mat[0][3]); 55 int b = int(mat[1][3]); 56 int c = int(mat[2][3]); 57 int d = int(mat[3][3]); 58 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 59 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 60 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 61 mat[3][3] = 1.0; 62 return mat; 63 } 64 65 /* Same here, Not the right place but need to be common to all overlay's. 66 * TODO Split to an overlay lib. */ 67 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 68 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 69 { 70 vec2 edge = edge_start - edge_pos; 71 float len = length(edge); 72 if (len > 0.0) { 73 edge /= len; 74 vec2 perp = vec2(-edge.y, edge.x); 75 float dist = dot(perp, frag_co - edge_start); 76 /* Add 0.1 to diffenrentiate with cleared pixels. */ 77 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 78 } 79 else { 80 /* Default line if the origin is perfectly aligned with a pixel. */ 81 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 82 } 83 } 84 85 uniform int resourceChunk; 86 87 #ifdef GPU_VERTEX_SHADER 88 # ifdef GL_ARB_shader_draw_parameters 89 # define baseInstance gl_BaseInstanceARB 90 # else /* no ARB_shader_draw_parameters */ 91 uniform int baseInstance; 92 # endif 93 94 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 95 /* When drawing instances of an object at the same position. */ 96 # define instanceId 0 97 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 98 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 99 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 100 * Ignore gl_InstanceID then. */ 101 # define instanceId 0 102 # else 103 # define instanceId gl_InstanceID 104 # endif 105 106 # define resource_id (baseInstance + instanceId) 107 108 /* Use this to declare and pass the value if 109 * the fragment shader uses the resource_id. */ 110 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 111 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 112 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 113 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 114 #endif 115 116 /* If used in a fragment / geometry shader, we pass 117 * resource_id as varying. */ 118 #ifdef GPU_GEOMETRY_SHADER 119 # define RESOURCE_ID_VARYING \ 120 flat out int resourceIDFrag; \ 121 flat in int resourceIDGeom[]; 122 123 # define resource_id resourceIDGeom 124 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 125 #endif 126 127 #ifdef GPU_FRAGMENT_SHADER 128 flat in int resourceIDFrag; 129 # define resource_id resourceIDFrag 130 #endif 131 132 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && \ 133 !defined(INSTANCED_ATTRIB) 134 struct ObjectMatrices { 135 mat4 drw_modelMatrix; 136 mat4 drw_modelMatrixInverse; 137 }; 138 139 layout(std140) uniform modelBlock 140 { 141 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 142 }; 143 144 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 145 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 146 147 #else /* GPU_INTEL */ 148 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 149 * So for now we just force using the legacy path. */ 150 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 151 * and older amd driver on windows. */ 152 uniform mat4 ModelMatrix; 153 uniform mat4 ModelMatrixInverse; 154 #endif 155 156 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 157 158 /** Transform shortcuts. */ 159 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 160 * will always be decomposed in at least 2 matrix operation. */ 161 162 /** 163 * Some clarification: 164 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 165 * 166 * But since it is slow to multiply matrices we decompose it. Decomposing 167 * inversion and transposition both invert the product order leaving us with 168 * the same original order: 169 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 170 * 171 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 172 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 173 * ViewMatrix * transpose(ModelMatrixInverse) 174 **/ 175 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 176 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 177 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 178 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 179 180 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 181 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 182 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 183 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 184 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 185 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 186 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 187 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 188 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 189 190 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 191 * to make vertex shaders work. even if it's actually dead code. */ 192 #ifdef GPU_INTEL 193 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 194 #else 195 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 196 #endif 197 198 #define DRW_BASE_SELECTED (1 << 1) 199 #define DRW_BASE_FROM_DUPLI (1 << 2) 200 #define DRW_BASE_FROM_SET (1 << 3) 201 #define DRW_BASE_ACTIVE (1 << 4) 202 struct LightData { 203 vec4 direction; 204 vec4 specular_color; 205 vec4 diffuse_color_wrap; /* rgb: diffuse col a: wrapped lighting factor */ 206 }; 207 208 struct WorldData { 209 vec4 background_color_low; 210 vec4 background_color_high; 211 vec4 object_outline_color; 212 vec4 shadow_direction_vs; 213 LightData lights[4]; 214 vec4 ambient_color; 215 int num_lights; 216 int matcap_orientation; 217 float background_alpha; 218 float curvature_ridge; 219 float curvature_valley; 220 float background_dither_factor; 221 int pad[2]; 222 }; 223 #define NO_OBJECT_ID uint(0) 224 #define EPSILON 0.00001 225 #define M_PI 3.14159265358979323846 226 227 #define CAVITY_BUFFER_RANGE 4.0 228 229 /* 4x4 bayer matrix prepared for 8bit UNORM precision error. */ 230 #define P(x) (((x + 0.5) * (1.0 / 16.0) - 0.5) * (1.0 / 255.0)) 231 const vec4 dither_mat4x4[4] = vec4[4](vec4(P(0.0), P(8.0), P(2.0), P(10.0)), 232 vec4(P(12.0), P(4.0), P(14.0), P(6.0)), 233 vec4(P(3.0), P(11.0), P(1.0), P(9.0)), 234 vec4(P(15.0), P(7.0), P(13.0), P(5.0))); 235 236 float bayer_dither_noise() 237 { 238 ivec2 tx1 = ivec2(gl_FragCoord.xy) % 4; 239 ivec2 tx2 = ivec2(gl_FragCoord.xy) % 2; 240 return dither_mat4x4[tx1.x][tx1.y]; 241 } 242 243 #ifdef WORKBENCH_ENCODE_NORMALS 244 245 # define WB_Normal vec2 246 247 /* From http://aras-p.info/texts/CompactNormalStorage.html 248 * Using Method #4: Spheremap Transform */ 249 vec3 workbench_normal_decode(WB_Normal enc) 250 { 251 vec2 fenc = enc.xy * 4.0 - 2.0; 252 float f = dot(fenc, fenc); 253 float g = sqrt(1.0 - f / 4.0); 254 vec3 n; 255 n.xy = fenc * g; 256 n.z = 1 - f / 2; 257 return n; 258 } 259 260 /* From http://aras-p.info/texts/CompactNormalStorage.html 261 * Using Method #4: Spheremap Transform */ 262 WB_Normal workbench_normal_encode(vec3 n) 263 { 264 float p = sqrt(n.z * 8.0 + 8.0); 265 n.xy = clamp(n.xy / p + 0.5, 0.0, 1.0); 266 return n.xy; 267 } 268 269 #else 270 # define WB_Normal vec3 271 /* Well just do nothing... */ 272 # define workbench_normal_encode(a) (a) 273 # define workbench_normal_decode(a) (a) 274 #endif /* WORKBENCH_ENCODE_NORMALS */ 275 276 /* Encoding into the alpha of a RGBA8 UNORM texture. */ 277 #define TARGET_BITCOUNT 8u 278 #define METALLIC_BITS 3u /* Metallic channel is less important. */ 279 #define ROUGHNESS_BITS (TARGET_BITCOUNT - METALLIC_BITS) 280 #define TOTAL_BITS (METALLIC_BITS + ROUGHNESS_BITS) 281 282 /* Encode 2 float into 1 with the desired precision. */ 283 float workbench_float_pair_encode(float v1, float v2) 284 { 285 // const uint total_mask = ~(0xFFFFFFFFu << TOTAL_BITS); 286 // const uint v1_mask = ~(0xFFFFFFFFu << ROUGHNESS_BITS); 287 // const uint v2_mask = ~(0xFFFFFFFFu << METALLIC_BITS); 288 /* Same as above because some compiler are dumb af. and think we use mediump int. */ 289 const int total_mask = 0xFF; 290 const int v1_mask = 0x1F; 291 const int v2_mask = 0x7; 292 int iv1 = int(v1 * float(v1_mask)); 293 int iv2 = int(v2 * float(v2_mask)) << int(ROUGHNESS_BITS); 294 return float(iv1 | iv2) * (1.0 / float(total_mask)); 295 } 296 297 void workbench_float_pair_decode(float data, out float v1, out float v2) 298 { 299 // const uint total_mask = ~(0xFFFFFFFFu << TOTAL_BITS); 300 // const uint v1_mask = ~(0xFFFFFFFFu << ROUGHNESS_BITS); 301 // const uint v2_mask = ~(0xFFFFFFFFu << METALLIC_BITS); 302 /* Same as above because some compiler are dumb af. and think we use mediump int. */ 303 const int total_mask = 0xFF; 304 const int v1_mask = 0x1F; 305 const int v2_mask = 0x7; 306 int idata = int(data * float(total_mask)); 307 v1 = float(idata & v1_mask) * (1.0 / float(v1_mask)); 308 v2 = float(idata >> int(ROUGHNESS_BITS)) * (1.0 / float(v2_mask)); 309 } 310 311 float calculate_transparent_weight(float z, float alpha) 312 { 313 #if 0 314 /* Eq 10 : Good for surfaces with varying opacity (like particles) */ 315 float a = min(1.0, alpha * 10.0) + 0.01; 316 float b = -gl_FragCoord.z * 0.95 + 1.0; 317 float w = a * a * a * 3e2 * b * b * b; 318 #else 319 /* Eq 7 put more emphasis on surfaces closer to the view. */ 320 // float w = 10.0 / (1e-5 + pow(abs(z) / 5.0, 2.0) + pow(abs(z) / 200.0, 6.0)); /* Eq 7 */ 321 // float w = 10.0 / (1e-5 + pow(abs(z) / 10.0, 3.0) + pow(abs(z) / 200.0, 6.0)); /* Eq 8 */ 322 // float w = 10.0 / (1e-5 + pow(abs(z) / 200.0, 4.0)); /* Eq 9 */ 323 /* Same as eq 7, but optimized. */ 324 float a = abs(z) / 5.0; 325 float b = abs(z) / 200.0; 326 b *= b; 327 float w = 10.0 / ((1e-5 + a * a) + b * (b * b)); /* Eq 7 */ 328 #endif 329 return alpha * clamp(w, 1e-2, 3e2); 330 } 331 332 /* Special function only to be used with calculate_transparent_weight(). */ 333 float linear_zdepth(float depth, vec4 viewvecs[3], mat4 proj_mat) 334 { 335 if (proj_mat[3][3] == 0.0) { 336 float d = 2.0 * depth - 1.0; 337 return -proj_mat[3][2] / (d + proj_mat[2][2]); 338 } 339 else { 340 /* Return depth from near plane. */ 341 return depth * viewvecs[1].z; 342 } 343 } 344 345 vec3 view_vector_from_screen_uv(vec2 uv, vec4 viewvecs[3], mat4 proj_mat) 346 { 347 return (proj_mat[3][3] == 0.0) ? normalize(viewvecs[0].xyz + vec3(uv, 0.0) * viewvecs[1].xyz) : 348 vec3(0.0, 0.0, 1.0); 349 } 350 351 vec2 matcap_uv_compute(vec3 I, vec3 N, bool flipped) 352 { 353 /* Quick creation of an orthonormal basis */ 354 float a = 1.0 / (1.0 + I.z); 355 float b = -I.x * I.y * a; 356 vec3 b1 = vec3(1.0 - I.x * I.x * a, b, -I.x); 357 vec3 b2 = vec3(b, 1.0 - I.y * I.y * a, -I.y); 358 vec2 matcap_uv = vec2(dot(b1, N), dot(b2, N)); 359 if (flipped) { 360 matcap_uv.x = -matcap_uv.x; 361 } 362 return matcap_uv * 0.496 + 0.5; 363 } 364 365 vec4 workbench_sample_texture(sampler2D image, 366 vec2 coord, 367 bool nearest_sampling, 368 bool premultiplied) 369 { 370 vec2 tex_size = vec2(textureSize(image, 0).xy); 371 /* TODO(fclem) We could do the same with sampler objects. 372 * But this is a quick workaround instead of messing with the GPUTexture itself. */ 373 vec2 uv = nearest_sampling ? (floor(coord * tex_size) + 0.5) / tex_size : coord; 374 vec4 color = texture(image, uv); 375 376 /* Unpremultiply if stored multiplied, since straight alpha is expected by shaders. */ 377 if (premultiplied && !(color.a == 0.0 || color.a == 1.0)) { 378 color.rgb = color.rgb / color.a; 379 } 380 381 return color; 382 } 383 vec3 background_color(WorldData world_data, float y) 384 { 385 return mix(world_data.background_color_low, world_data.background_color_high, y).xyz + 386 (world_data.background_dither_factor * bayer_dither_noise()); 387 } 388 389 /* [Drobot2014a] Low Level Optimizations for GCN */ 390 vec4 fast_rcp(vec4 v) 391 { 392 return intBitsToFloat(0x7eef370b - floatBitsToInt(v)); 393 } 394 395 vec3 brdf_approx(vec3 spec_color, float roughness, float NV) 396 { 397 /* Very rough own approx. We don't need it to be correct, just fast. 398 * Just simulate fresnel effect with roughness attenuation. */ 399 float fresnel = exp2(-8.35 * NV) * (1.0 - roughness); 400 return mix(spec_color, vec3(1.0), fresnel); 401 } 402 403 void prep_specular( 404 vec3 L, vec3 I, vec3 N, vec3 R, out float NL, out float wrapped_NL, out float spec_angle) 405 { 406 wrapped_NL = dot(L, R); 407 vec3 half_dir = normalize(L + I); 408 spec_angle = clamp(dot(half_dir, N), 0.0, 1.0); 409 NL = clamp(dot(L, N), 0.0, 1.0); 410 } 411 412 /* Normalized Blinn shading */ 413 vec4 blinn_specular(vec4 shininess, vec4 spec_angle, vec4 NL) 414 { 415 /* Pi is already divided in the light power. 416 * normalization_factor = (shininess + 8.0) / (8.0 * M_PI) */ 417 vec4 normalization_factor = shininess * 0.125 + 1.0; 418 vec4 spec_light = pow(spec_angle, shininess) * NL * normalization_factor; 419 420 return spec_light; 421 } 422 423 /* NL need to be unclamped. w in [0..1] range. */ 424 vec4 wrapped_lighting(vec4 NL, vec4 w) 425 { 426 vec4 w_1 = w + 1.0; 427 vec4 denom = fast_rcp(w_1 * w_1); 428 return clamp((NL + w) * denom, 0.0, 1.0); 429 } 430 431 vec3 get_world_lighting( 432 WorldData world_data, vec3 diffuse_color, vec3 specular_color, float roughness, vec3 N, vec3 I) 433 { 434 vec3 specular_light = world_data.ambient_color.rgb; 435 vec3 diffuse_light = world_data.ambient_color.rgb; 436 vec4 wrap = vec4(world_data.lights[0].diffuse_color_wrap.a, 437 world_data.lights[1].diffuse_color_wrap.a, 438 world_data.lights[2].diffuse_color_wrap.a, 439 world_data.lights[3].diffuse_color_wrap.a); 440 441 #ifdef V3D_SHADING_SPECULAR_HIGHLIGHT 442 /* Prepare Specular computation. Eval 4 lights at once. */ 443 vec3 R = -reflect(I, N); 444 vec4 spec_angle, spec_NL, wrap_NL; 445 prep_specular(world_data.lights[0].direction.xyz, I, N, R, spec_NL.x, wrap_NL.x, spec_angle.x); 446 prep_specular(world_data.lights[1].direction.xyz, I, N, R, spec_NL.y, wrap_NL.y, spec_angle.y); 447 prep_specular(world_data.lights[2].direction.xyz, I, N, R, spec_NL.z, wrap_NL.z, spec_angle.z); 448 prep_specular(world_data.lights[3].direction.xyz, I, N, R, spec_NL.w, wrap_NL.w, spec_angle.w); 449 450 vec4 gloss = vec4(1.0 - roughness); 451 /* Reduce gloss for smooth light. (simulate bigger light) */ 452 gloss *= 1.0 - wrap; 453 vec4 shininess = exp2(10.0 * gloss + 1.0); 454 455 vec4 spec_light = blinn_specular(shininess, spec_angle, spec_NL); 456 457 /* Simulate Env. light. */ 458 vec4 w = mix(wrap, vec4(1.0), roughness); 459 vec4 spec_env = wrapped_lighting(wrap_NL, w); 460 461 spec_light = mix(spec_light, spec_env, wrap * wrap); 462 463 /* Multiply result by lights specular colors. */ 464 specular_light += spec_light.x * world_data.lights[0].specular_color.rgb; 465 specular_light += spec_light.y * world_data.lights[1].specular_color.rgb; 466 specular_light += spec_light.z * world_data.lights[2].specular_color.rgb; 467 specular_light += spec_light.w * world_data.lights[3].specular_color.rgb; 468 469 float NV = clamp(dot(N, I), 0.0, 1.0); 470 specular_color = brdf_approx(specular_color, roughness, NV); 471 #endif 472 specular_light *= specular_color; 473 474 /* Prepare diffuse computation. Eval 4 lights at once. */ 475 vec4 diff_NL; 476 diff_NL.x = dot(world_data.lights[0].direction.xyz, N); 477 diff_NL.y = dot(world_data.lights[1].direction.xyz, N); 478 diff_NL.z = dot(world_data.lights[2].direction.xyz, N); 479 diff_NL.w = dot(world_data.lights[3].direction.xyz, N); 480 481 vec4 diff_light = wrapped_lighting(diff_NL, wrap); 482 483 /* Multiply result by lights diffuse colors. */ 484 diffuse_light += diff_light.x * world_data.lights[0].diffuse_color_wrap.rgb; 485 diffuse_light += diff_light.y * world_data.lights[1].diffuse_color_wrap.rgb; 486 diffuse_light += diff_light.z * world_data.lights[2].diffuse_color_wrap.rgb; 487 diffuse_light += diff_light.w * world_data.lights[3].diffuse_color_wrap.rgb; 488 489 /* Energy conservation with colored specular look strange. 490 * Limit this strangeness by using mono-chromatic specular intensity. */ 491 float spec_energy = dot(specular_color, vec3(0.33333)); 492 493 diffuse_light *= diffuse_color * (1.0 - spec_energy); 494 495 return diffuse_light + specular_light; 496 } 497 out vec4 fragColor; 498 499 uniform usampler2D objectId; 500 uniform sampler2D materialBuffer; 501 uniform sampler2D normalBuffer; 502 /* normalBuffer contains viewport normals */ 503 uniform sampler2D cavityBuffer; 504 uniform sampler2D matcapDiffuseImage; 505 uniform sampler2D matcapSpecularImage; 506 507 uniform vec2 invertedViewportSize; 508 uniform vec4 viewvecs[3]; 509 uniform float shadowMultiplier; 510 uniform float lightMultiplier; 511 uniform float shadowShift = 0.1; 512 uniform float shadowFocus = 1.0; 513 514 uniform vec3 materialSingleColor; 515 516 layout(std140) uniform world_block 517 { 518 WorldData world_data; 519 }; 520 521 void main() 522 { 523 ivec2 texel = ivec2(gl_FragCoord.xy); 524 vec2 uv_viewport = gl_FragCoord.xy * invertedViewportSize; 525 526 float roughness, metallic; 527 vec3 base_color; 528 529 #ifndef MATDATA_PASS_ENABLED 530 base_color = materialSingleColor; 531 metallic = 0.0; 532 roughness = 0.5; 533 #else 534 vec4 material_data = texelFetch(materialBuffer, texel, 0); 535 base_color = material_data.rgb; 536 workbench_float_pair_decode(material_data.a, roughness, metallic); 537 #endif 538 539 /* Do we need normals */ 540 #ifdef NORMAL_VIEWPORT_PASS_ENABLED 541 vec3 normal_viewport = workbench_normal_decode(texelFetch(normalBuffer, texel, 0).rg); 542 #endif 543 544 vec3 I_vs = view_vector_from_screen_uv(uv_viewport, viewvecs, ProjectionMatrix); 545 546 /* -------- SHADING --------- */ 547 #ifdef V3D_LIGHTING_FLAT 548 vec3 shaded_color = base_color; 549 550 #elif defined(V3D_LIGHTING_MATCAP) 551 /* When using matcaps, the metallic is the backface sign. */ 552 normal_viewport = (metallic > 0.0) ? normal_viewport : -normal_viewport; 553 bool flipped = world_data.matcap_orientation != 0; 554 vec2 matcap_uv = matcap_uv_compute(I_vs, normal_viewport, flipped); 555 vec3 matcap_diffuse = textureLod(matcapDiffuseImage, matcap_uv, 0.0).rgb; 556 557 # ifdef V3D_SHADING_SPECULAR_HIGHLIGHT 558 vec3 matcap_specular = textureLod(matcapSpecularImage, matcap_uv, 0.0).rgb; 559 # else 560 vec3 matcap_specular = vec3(0.0); 561 # endif 562 563 vec3 shaded_color = matcap_diffuse * base_color + matcap_specular; 564 565 #elif defined(V3D_LIGHTING_STUDIO) 566 567 # ifdef V3D_SHADING_SPECULAR_HIGHLIGHT 568 vec3 specular_color = mix(vec3(0.05), base_color, metallic); 569 vec3 diffuse_color = mix(base_color, vec3(0.0), metallic); 570 # else 571 roughness = 0.0; 572 vec3 specular_color = vec3(0.0); 573 vec3 diffuse_color = base_color; 574 # endif 575 576 vec3 shaded_color = get_world_lighting( 577 world_data, diffuse_color, specular_color, roughness, normal_viewport, I_vs); 578 #endif 579 580 /* -------- POST EFFECTS --------- */ 581 #ifdef WB_CAVITY 582 /* Using UNORM texture so decompress the range */ 583 shaded_color *= texelFetch(cavityBuffer, texel, 0).r * CAVITY_BUFFER_RANGE; 584 #endif 585 586 #ifdef V3D_SHADING_SHADOW 587 float light_factor = -dot(normal_viewport, world_data.shadow_direction_vs.xyz); 588 float shadow_mix = smoothstep(shadowFocus, shadowShift, light_factor); 589 shaded_color *= mix(lightMultiplier, shadowMultiplier, shadow_mix); 590 #endif 591 592 #ifdef V3D_SHADING_OBJECT_OUTLINE 593 uint object_id = texelFetch(objectId, texel, 0).r; 594 float object_outline = calculate_object_outline(objectId, texel, object_id); 595 shaded_color = mix(world_data.object_outline_color.rgb, shaded_color, object_outline); 596 #endif 597 598 fragColor = vec4(shaded_color, 1.0); 599 } 0(132) : error C0105: Syntax error in #if 0(132) : error C0105: Syntax error in #if 0(133) : error C0000: syntax error, unexpected '!' at token "!" 0(137) : error C0000: syntax error, unexpected '}' at token "}" 0(141) : error C0000: syntax error, unexpected '[', expecting "::" at token "[" Error : EXCEPTION_ACCESS_VIOLATION Address : 0x0000000140DEE884 Module : c:\blender-2.82-5e96b860a372-windows64\blender.exe