-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathtest.py
More file actions
186 lines (153 loc) · 5.94 KB
/
Copy pathtest.py
File metadata and controls
186 lines (153 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
"""
CrossGL Example Conversion Test
Comprehensive test to verify translation functionality across all backends and example categories.
"""
import sys
import os
from pathlib import Path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import crosstl
def main():
"""Test comprehensive translation functionality across all example categories."""
# Define the organized example structure
examples_by_category = {
"graphics": ["SimpleShader.cgl", "PerlinNoise.cgl", "ComplexShader.cgl"],
"advanced": ["ArrayTest.cgl", "GenericPatternMatching.cgl"],
"compute": ["ParticleSimulation.cgl"],
"cross_platform": ["UniversalPBRShader.cgl"],
"gpu_computing": ["MatrixMultiplication.cgl"],
}
# Define backend mappings with appropriate extensions
backends = {
"metal": ".metal",
"directx": ".hlsl",
"opengl": ".glsl",
"vulkan": ".spirv",
"rust": ".rs",
"mojo": ".mojo",
"cuda": ".cu",
"hip": ".hip",
"slang": ".slang",
}
# Backend compatibility matrix - some examples work better with certain backends
backend_compatibility = {
"graphics": [
"metal",
"directx",
"opengl",
"vulkan",
"rust",
"mojo",
"cuda",
"hip",
"slang",
],
"advanced": [
"metal",
"directx",
"opengl",
"vulkan",
"rust",
"mojo",
"cuda",
"hip",
"slang",
],
"compute": ["metal", "directx", "opengl", "vulkan", "cuda", "hip"],
"cross_platform": ["metal", "directx", "opengl", "vulkan", "rust"],
"gpu_computing": ["cuda", "hip", "mojo", "rust"],
}
print("[CROSSGL] CrossGL Comprehensive Translation Test")
print("=" * 60)
# Ensure output directories exist
for backend in backends:
os.makedirs(f"output/{backend}", exist_ok=True)
total_tests = 0
successful_tests = 0
failed_tests = []
# Test each category
for category, examples in examples_by_category.items():
print(f"\n[TESTING] {category.upper()} examples:")
print("-" * 40)
for example in examples:
example_path = f"{category}/{example}"
example_name = Path(example).stem
if not Path(example_path).exists():
print(f"[WARNING] Skipping {example} (not found)")
continue
print(f"\n[TRANSLATING] {example_name}:")
# Get compatible backends for this category
compatible_backends = backend_compatibility.get(
category, list(backends.keys())
)
for backend in compatible_backends:
if backend not in backends:
continue
total_tests += 1
try:
# Create organized output structure: output/backend/category/
backend_output_dir = f"output/{backend}/{category}"
os.makedirs(backend_output_dir, exist_ok=True)
output_file = (
f"{backend_output_dir}/{example_name}{backends[backend]}"
)
# Perform translation
result = crosstl.translate(
example_path, backend=backend, save_shader=output_file
)
# Verify the output file was created and has content
if (
Path(output_file).exists()
and Path(output_file).stat().st_size > 100
):
print(f" [SUCCESS] {backend:8} -> {output_file}")
successful_tests += 1
else:
print(f" [WARNING] {backend:8} -> Output too small or missing")
failed_tests.append(
(example_name, backend, "Output file too small")
)
except Exception as e:
print(f" [ERROR] {backend:8} -> Error: {str(e)[:50]}...")
failed_tests.append((example_name, backend, str(e)))
# Summary
print("\n" + "=" * 60)
print("[SUMMARY] TRANSLATION TEST SUMMARY")
print("=" * 60)
print(f"Total tests: {total_tests}")
print(f"Successful: {successful_tests}")
print(f"Failed: {len(failed_tests)}")
print(f"Success rate: {(successful_tests/total_tests)*100:.1f}%")
if failed_tests:
print(f"\n[FAILED] Failed translations:")
for example, backend, error in failed_tests:
print(f" - {example} -> {backend}: {error[:60]}...")
# Test cross-backend consistency
print(f"\n[TESTING] Testing cross-backend consistency...")
test_cross_backend_consistency()
print(f"\n[COMPLETE] Translation test complete!")
print(
f"[OUTPUT] Check output/ directory for organized results by backend and category."
)
def test_cross_backend_consistency():
"""Test that the same shader produces valid output across multiple backends."""
test_shader = "graphics/SimpleShader.cgl"
if not Path(test_shader).exists():
print("[WARNING] SimpleShader.cgl not found for consistency test")
return
consistency_backends = ["metal", "directx", "opengl", "vulkan"]
outputs = {}
for backend in consistency_backends:
try:
result = crosstl.translate(test_shader, backend=backend)
outputs[backend] = len(result) if result else 0
except Exception:
outputs[backend] = 0
print(f" Output sizes: {outputs}")
non_zero_outputs = [k for k, v in outputs.items() if v > 100]
print(
f" [SUCCESS] {len(non_zero_outputs)}/{len(consistency_backends)} backends produced substantial output"
)
if __name__ == "__main__":
main()