Skip to content

Commit 5ff0653

Browse files
committed
Add ashader.c
1 parent 67c5f5d commit 5ff0653

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

tools/amake/ashader.c

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
2+
// Simple text processing tool for converting shaders at runtime.
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <stdbool.h>
7+
#include <string.h>
8+
#include "iron_string.h"
9+
10+
char out[128 * 1024];
11+
char line[1024];
12+
13+
char *buffer;
14+
int pos = 0;
15+
16+
const char *header_glsl = "#version 450\n\
17+
#define GLSL\n\
18+
#define textureArg(tex) sampler2D tex \n\
19+
#define texturePass(tex) tex \n\
20+
#define mul(a, b) b * a \n\
21+
#define textureShared texture \n\
22+
#define textureLodShared textureLod \n\
23+
#define atan2(x, y) atan(y, x) \n\
24+
";
25+
26+
// shared_sampler
27+
const char *header_hlsl = "#define HLSL\n\
28+
#define textureArg(tex) Texture2D tex,SamplerState tex ## _sampler\n\
29+
#define texturePass(tex) tex,tex ## _sampler\n\
30+
#define sampler2D Texture2D\n\
31+
#define sampler3D Texture3D\n\
32+
#define texture(tex, coord) tex.Sample(tex ## _sampler, coord)\n\
33+
#define textureShared(tex, coord) tex.Sample(\" + shared_sampler + \", coord)\n\
34+
#define textureLod(tex, coord, lod) tex.SampleLevel(tex ## _sampler, coord, lod)\n\
35+
#define textureLodShared(tex, coord, lod) tex.SampleLevel(\" + shared_sampler + \", coord, lod)\n\
36+
#define texelFetch(tex, coord, lod) tex.Load(float3(coord.xy, lod))\n\
37+
uint2 _GetDimensions(Texture2D tex, uint lod) { uint x, y; tex.GetDimensions(x, y); return uint2(x, y); }\n\
38+
#define textureSize _GetDimensions\n\
39+
#define mod(a, b) (a % b)\n\
40+
#define vec2 float2\n\
41+
#define vec3 float3\n\
42+
#define vec4 float4\n\
43+
#define ivec2 int2\n\
44+
#define ivec3 int3\n\
45+
#define ivec4 int4\n\
46+
#define mat2 float2x2\n\
47+
#define mat3 float3x3\n\
48+
#define mat4 float4x4\n\
49+
#define dFdx ddx\n\
50+
#define dFdy ddy\n\
51+
#define inversesqrt rsqrt\n\
52+
#define fract frac\n\
53+
#define mix lerp\n\
54+
";
55+
56+
// shared_sampler
57+
const char *header_msl = "#define METAL\n\
58+
#include <metal_stdlib>\n\
59+
#include <simd/simd.h>\n\
60+
using namespace metal;\n\
61+
#define textureArg(tex) texture2d<float> tex,sampler tex ## _sampler\n\
62+
#define texturePass(tex) tex,tex ## _sampler\n\
63+
#define sampler2D texture2d<float>\n\
64+
#define sampler3D texture3d<float>\n\
65+
#define texture(tex, coord) tex.sample(tex ## _sampler, coord)\n\
66+
#define textureShared(tex, coord) tex.sample(\" + shared_sampler + \", coord)\n\
67+
#define textureLod(tex, coord, lod) tex.sample(tex ## _sampler, coord, level(lod))\n\
68+
#define textureLodShared(tex, coord, lod) tex.sample(\" + shared_sampler + \", coord, level(lod))\n\
69+
#define texelFetch(tex, coord, lod) tex.read(uint2(coord), uint(lod))\n\
70+
float2 _getDimensions(texture2d<float> tex, uint lod) { return float2(tex.get_width(lod), tex.get_height(lod)); }\n\
71+
#define textureSize _getDimensions\n\
72+
#define mod(a, b) fmod(a, b)\n\
73+
#define vec2 float2\n\
74+
#define vec3 float3\n\
75+
#define vec4 float4\n\
76+
#define ivec2 int2\n\
77+
#define ivec3 int3\n\
78+
#define ivec4 int4\n\
79+
#define mat2 float2x2\n\
80+
#define mat3 float3x3\n\
81+
#define mat4 float4x4\n\
82+
#define dFdx dfdx\n\
83+
#define dFdy dfdy\n\
84+
#define inversesqrt rsqrt\n\
85+
#define mul(a, b) b * a\n\
86+
#define discard discard_fragment()\n\
87+
";
88+
89+
char *read_line() {
90+
int i = 0;
91+
while (true) {
92+
if (buffer[pos] == '\0') {
93+
return NULL;
94+
}
95+
96+
line[i] = buffer[pos];
97+
i++;
98+
pos++;
99+
100+
if (buffer[pos - 1] == '\n') {
101+
break;
102+
}
103+
}
104+
line[i] = '\0';
105+
return &line[0];
106+
}
107+
108+
void process_file(char *file, int off) {
109+
char *_buffer = buffer;
110+
int _pos = pos;
111+
pos = off;
112+
113+
FILE *fp = fopen(file, "rb");
114+
fseek(fp , 0, SEEK_END);
115+
int size = ftell(fp);
116+
rewind(fp);
117+
buffer = malloc(size + 1);
118+
buffer[size] = '\0';
119+
fread(buffer, size, 1, fp);
120+
fclose(fp);
121+
122+
while (true) {
123+
char *line = read_line();
124+
if (line == NULL) {
125+
break;
126+
}
127+
128+
if (starts_with(line, "#include ")) {
129+
char *rel = line + 10; // #include "
130+
rel[strlen(rel) - 1 - 1] = '\0'; // trailing "
131+
132+
char path[512];
133+
int last = string_last_index_of(file, "/") + 1;
134+
strncpy(path, file, last);
135+
path[last] = '\0';
136+
strcat(path, rel);
137+
138+
process_file(path, 0);
139+
line[0] = '\0';
140+
}
141+
142+
strcat(out, line);
143+
}
144+
145+
free(buffer);
146+
buffer = _buffer;
147+
pos = _pos;
148+
}
149+
150+
// glsl from to
151+
int ashader_compile(int argc, char **argv) {
152+
strcpy(out, header_glsl);
153+
154+
process_file(argv[2], 13); // Skip #version 450\n
155+
156+
FILE *fp = fopen(argv[3], "wb");
157+
fwrite(out, 1, strlen(out), fp);
158+
159+
return 0;
160+
}

tools/amake/project.js

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ project.add_include_dir("../../sources/libs");
2222
project.add_cfiles("../../sources/libs/quickjs/*.c");
2323
project.add_cfiles("main.c");
2424

25+
{
26+
project.add_cfiles("ashader.c");
27+
// if (platform === 'linux') {
28+
// project.add_project("../to_spirv"); // Replace with https://github.com/Kode/Kongruent
29+
// }
30+
}
31+
2532
if (platform === 'linux') {
2633
// quickjs
2734
project.add_lib("m");

0 commit comments

Comments
 (0)