validate-shader-uniforms.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/bash
  2. # Script to validate shader uniform naming consistency
  3. # Helps catch camelCase vs snake_case mismatches between shaders and C++ code
  4. set -e
  5. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6. PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
  7. echo "=== Shader Uniform Validation ==="
  8. echo "Checking for naming inconsistencies between shaders and backend code..."
  9. echo ""
  10. # Colors for output
  11. RED='\033[0;31m'
  12. GREEN='\033[0;32m'
  13. YELLOW='\033[1;33m'
  14. NC='\033[0m' # No Color
  15. ERRORS=0
  16. WARNINGS=0
  17. # Common uniform naming patterns that should be consistent
  18. # Format: "camelCase|snake_case|shader_pattern"
  19. declare -a UNIFORM_PATTERNS=(
  20. "viewProj|view_proj|u_viewProj"
  21. "lightDir|light_dir|u_lightDir"
  22. "tileSize|tile_size|u_tileSize"
  23. "detailNoiseScale|detail_noiseScale|u_detailNoiseScale"
  24. "cameraRight|camera_right|u_cameraRight"
  25. "cameraForward|camera_forward|u_cameraForward"
  26. )
  27. echo "Checking shader files in assets/shaders/..."
  28. # Find all fragment and vertex shaders
  29. SHADER_FILES=$(find "$PROJECT_ROOT/assets/shaders" -type f \( -name "*.frag" -o -name "*.vert" \))
  30. for shader_file in $SHADER_FILES; do
  31. shader_name=$(basename "$shader_file")
  32. # Extract uniform declarations from shader
  33. uniforms=$(grep -oP 'uniform\s+\w+\s+\K\w+' "$shader_file" 2>/dev/null || true)
  34. if [ -z "$uniforms" ]; then
  35. continue
  36. fi
  37. echo "Checking: $shader_name"
  38. # Check each uniform against common patterns
  39. while IFS= read -r uniform; do
  40. # Check if this uniform follows a known pattern
  41. for pattern in "${UNIFORM_PATTERNS[@]}"; do
  42. IFS='|' read -r camel snake shader_expected <<< "$pattern"
  43. # If uniform contains the pattern
  44. if [[ "$uniform" == *"$camel"* ]] || [[ "$uniform" == *"$snake"* ]]; then
  45. # Check if it matches the expected shader pattern
  46. if [[ "$uniform" != "$shader_expected" ]]; then
  47. echo -e " ${YELLOW}WARNING${NC}: Uniform '$uniform' might not match expected pattern '$shader_expected'"
  48. ((WARNINGS++))
  49. fi
  50. # Now check if backend.cpp is using the correct name
  51. if grep -q "uniformHandle(\"$uniform\")" "$PROJECT_ROOT/render/gl/backend.cpp" 2>/dev/null; then
  52. echo -e " ${GREEN}✓${NC} Found correct usage: uniformHandle(\"$uniform\")"
  53. else
  54. # Check if backend is using wrong naming convention
  55. if [[ "$uniform" == "$shader_expected" ]]; then
  56. # Check for common wrong patterns
  57. if [[ "$shader_expected" == "u_viewProj" ]] && grep -q "uniformHandle(\"u_view_proj\")" "$PROJECT_ROOT/render/gl/backend.cpp"; then
  58. echo -e " ${RED}ERROR${NC}: backend.cpp uses 'u_view_proj' but shader has 'u_viewProj'"
  59. ((ERRORS++))
  60. elif [[ "$shader_expected" == "u_lightDir" ]] && grep -q "uniformHandle(\"u_light_dir\")" "$PROJECT_ROOT/render/gl/backend.cpp"; then
  61. echo -e " ${RED}ERROR${NC}: backend.cpp uses 'u_light_dir' but shader has 'u_lightDir'"
  62. ((ERRORS++))
  63. elif [[ "$shader_expected" == "u_tileSize" ]] && grep -q "uniformHandle(\"u_tile_size\")" "$PROJECT_ROOT/render/gl/backend.cpp"; then
  64. echo -e " ${RED}ERROR${NC}: backend.cpp uses 'u_tile_size' but shader has 'u_tileSize'"
  65. ((ERRORS++))
  66. elif [[ "$shader_expected" == "u_cameraRight" ]] && grep -q "uniformHandle(\"u_camera_right\")" "$PROJECT_ROOT/render/gl/backend.cpp"; then
  67. echo -e " ${RED}ERROR${NC}: backend.cpp uses 'u_camera_right' but shader has 'u_cameraRight'"
  68. ((ERRORS++))
  69. fi
  70. fi
  71. fi
  72. fi
  73. done
  74. done <<< "$uniforms"
  75. done
  76. echo ""
  77. echo "=== Validation Summary ==="
  78. echo -e "Errors: ${RED}$ERRORS${NC}"
  79. echo -e "Warnings: ${YELLOW}$WARNINGS${NC}"
  80. if [ $ERRORS -gt 0 ]; then
  81. echo -e "${RED}Validation FAILED - please fix the errors above${NC}"
  82. exit 1
  83. elif [ $WARNINGS -gt 0 ]; then
  84. echo -e "${YELLOW}Validation passed with warnings${NC}"
  85. exit 0
  86. else
  87. echo -e "${GREEN}Validation PASSED - all uniforms look correct${NC}"
  88. exit 0
  89. fi