summaryrefslogtreecommitdiff
path: root/test/cmain.c
blob: 76e3c71d7181aa933efec9e87eeb3dadc05884db (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <stdlib.h>

#include <pixelflinger2/pixelflinger2_interface.h>

int ApproximatelyEqual(const Vector4 lhs, const Vector4 rhs, const float t)
{
   if (fabs(lhs.x - rhs.x) > t)
      return 0;
   if (fabs(lhs.y - rhs.y) > t)
      return 0;
   if (fabs(lhs.z - rhs.z) > t)
      return 0;
   if (fabs(lhs.w - rhs.w) > t)
      return 0;
   return 1;
}

extern void * llvmCtx;

void contextless_test()
{
   static const char vsGLSL [] =
      "uniform vec4 uVec4; \n"
      "uniform sampler2D sampler2d; \n"
      "attribute vec4 aPosition; \n"
      "attribute vec4 aTexCoord; \n"
      "varying vec4 vTexCoord; \n"
      "varying vec4 vTexColor; \n"
      "void main() { \n"
      "   gl_Position = aPosition; \n"
      "   vTexCoord = aTexCoord; \n"
      "   vTexColor = texture2D(sampler2d, aTexCoord.zw); \n"
      "   gl_PointSize = 432.0; \n"
      "}";
   gl_shader_t * vs = GGLShaderCreate(GL_VERTEX_SHADER);
   const char * infoLog = NULL;
   if (!GGLShaderCompile(vs, vsGLSL, &infoLog)) {
      printf("GGLShaderCompile vs failed:\n%s\n", infoLog);
      assert(0);
   }
   static const char fsGLSL [] =
      "uniform vec4 uVec4; \n"
      "uniform sampler2D sampler2d; \n"
      "varying vec4 vTexCoord; \n"
      "varying vec4 vTexColor; \n"
      "void main() { \n"
      "   gl_FragColor = texture2D(sampler2d, vTexCoord.zw); \n"
      "}";
   gl_shader_t * fs = GGLShaderCreate(GL_FRAGMENT_SHADER);
   if (!GGLShaderCompile(fs, fsGLSL, &infoLog)) {
      printf("GGLShaderCompile fs failed:\n%s\n", infoLog);
      assert(0);
   }
   gl_shader_program_t * prog = GGLShaderProgramCreate();
   unsigned glError = GL_NO_ERROR;
   glError = GGLShaderAttach(prog, vs);
   assert(GL_NO_ERROR == glError);
   glError = GGLShaderAttach(prog, fs);
   assert(GL_NO_ERROR == glError);
   GGLShaderAttributeBind(prog, 4, "aPosition");
   GGLShaderAttributeBind(prog, 5, "aTexCoord");
   if (!GGLShaderProgramLink(prog, &infoLog)) {
      printf("GGLShaderProgramLink failed:\n%s\n", infoLog);
      assert(0);
   }
//   llvm::LLVMContext * llvmCtx = new llvm::LLVMContext();
   GGLState_t gglState = {0};
   unsigned texels0 [] = {0xff10ffff, 0x22222222, 0x66666666, 0xffffffff};
   GGLTexture_t texture0 = {GL_TEXTURE_2D, GGL_PIXEL_FORMAT_RGBA_8888,
                            2, 2, 1, // width, height, levelCount
                            texels0, GGL_CLAMP_TO_EDGE, GGL_CLAMP_TO_EDGE,
                            GGL_NEAREST, GGL_NEAREST
                           };
   gglState.textureState.textures[0] = texture0;
   gglState.textureState.textureData[0] = gglState.textureState.textures[0].levels;
   gglState.textureState.textureDimensions[0 * 2 + 0] = gglState.textureState.textures[0].width;
   gglState.textureState.textureDimensions[0 * 2 + 1] = gglState.textureState.textures[0].height;
   GGLShaderUse(llvmCtx, &gglState, prog);

   VertexInput_t input = {0, 0, 0, 0};
   input.attributes[4] = VECTOR4_CTR(0,0,0,1);
   input.attributes[5] = VECTOR4_CTR(0,0,0,0);
   VertexOutput_t output = {0};
   GGLProcessVertex(prog, &input, &output, NULL);
   int vTexColor = -1;
   GGLShaderVaryingLocation(prog, "vTexColor", &vTexColor);
   if (vTexColor >= 0) {
      if (memcmp(((Vector4 *)&output) + vTexColor, &VECTOR4_CTR(1,1,16/255.0f,1), sizeof(Vector4))) {
         puts("((Vector4 *)&output)[vTexColor] != Vector4(1,1,0,1)");
         assert(0);
      }
   } else {
      puts("vTexColor < 0");
      assert(0);
   }

   static const char fsGLSL1 [] =
      "uniform vec4 uVec4; \n"
      "uniform sampler2D sampler2d; \n"
      "varying vec4 vTexCoord; \n"
      "varying vec4 vTexColor; \n"
      "void main() { \n"
      "   gl_FragColor = vTexColor; \n"
      "}";
   gl_shader_t * fs1 = GGLShaderCreate(GL_FRAGMENT_SHADER);
   if (!GGLShaderCompile(fs1, fsGLSL1, &infoLog)) {
      printf("GGLShaderCompile fs failed:\n%s\n", infoLog);
      assert(0);
   }
   gl_shader_program_t * prog1 = GGLShaderProgramCreate();
   glError = GGLShaderAttach(prog1, vs);
   assert(GL_NO_ERROR == glError);
   glError = GGLShaderAttach(prog1, fs1);
   assert(GL_NO_ERROR == glError);
   GGLShaderAttributeBind(prog1, 1, "aPosition");
   GGLShaderAttributeBind(prog1, 2, "aTexCoord");
   if (!GGLShaderProgramLink(prog1, &infoLog)) {
      printf("GGLShaderProgramLink failed:\n%s\n", infoLog);
      assert(0);
   }

   GGLShaderUse(llvmCtx, &gglState, prog1);
   VertexInput_t input1 = {0};
   input1.attributes[1] = VECTOR4_CTR(1,1,0,1);
   input1.attributes[2] = VECTOR4_CTR(1,1,0,0);
   VertexOutput_t output1 = {0};
   GGLProcessVertex(prog1, &input1, &output1, NULL);
   int vTexCoord = -1;
   assert(2 == GGLShaderAttributeLocation(prog1, "aTexCoord"));
   GGLShaderVaryingLocation(prog1, "vTexCoord", &vTexCoord);
   if (vTexCoord >= 0) {
      if (memcmp(((Vector4 *)&output1) + vTexCoord, input1.attributes + 2, sizeof(Vector4))) {
         puts("((Vector4 *)&output1)[vTexCoord] != input1.attributes[1]");
         assert(0);
      }
   } else {
      puts("vTexCoord < 0");
      assert(0);
   }

   puts("***\n finished contextless_test \n***");

   GGLShaderProgramDelete(prog);
   GGLShaderProgramDelete(prog1);
   
   GLContextDctr();
}

int cmain(int argc, char **argv)
{
   contextless_test();
   
   const char * infoLog = NULL;

   GGLInterface_t * ggl = CreateGGLInterface();

   gl_shader_t * shader0 = ggl->ShaderCreate(ggl, GL_VERTEX_SHADER);
   assert(shader0);
   const char * glsl0 =
      "uniform vec4 uVec4; \n"
      "uniform sampler2D sampler2d; \n"
      "attribute vec4 aPosition; \n"
      "attribute vec4 aTexCoord; \n"
      "varying vec4 vTexCoord; \n"
      "varying vec4 vTexColor; \n"
      "void main() { \n"
      "   gl_Position = aPosition; \n"
      "   vTexCoord = aTexCoord + uVec4; \n"
      "   vTexColor = texture2D(sampler2d, aTexCoord.zw); \n"
      "   gl_PointSize = 432; \n"
      "}";
   puts(glsl0);
   GLboolean compileStatus = ggl->ShaderCompile(ggl, shader0, glsl0, &infoLog);
   if (!compileStatus)
      fprintf(stderr, "failed to compile vertex shader 0, infoLog: \n %s \n", infoLog);
   assert(compileStatus);

   gl_shader_t * shader1 = ggl->ShaderCreate(ggl, GL_FRAGMENT_SHADER);
   assert(shader1);
   const char * glsl1 =
      "uniform vec4 uVec4; \n"
      "uniform sampler2D sampler2d; \n"
      "varying vec4 vTexCoord; \n"
      "varying vec4 vTexColor; \n"
      "void main() { \n"
      "   gl_FragColor = vTexCoord + vTexColor; \n"
      "}";
   puts(glsl1);
   compileStatus = ggl->ShaderCompile(ggl, shader1, glsl1, &infoLog);
   if (!compileStatus)
      fprintf(stderr, "failed to compile fragment shader 0, infoLog: \n %s \n", infoLog);
   assert(compileStatus);

   gl_shader_program_t * program0 = ggl->ShaderProgramCreate(ggl);
   assert(program0);

   ggl->ShaderAttach(ggl, program0, shader0);
   ggl->ShaderAttach(ggl, program0, shader1);
   ggl->ShaderAttributeBind(program0, 2, "aTexCoord");
   ggl->ShaderAttributeBind(program0, 3, "aPosition");

   GLboolean linkStatus = ggl->ShaderProgramLink(program0, &infoLog);
   if (!linkStatus)
      fprintf(stderr, "failed to link program 0, infoLog: \n %s \n", infoLog);
   assert(linkStatus);

   ggl->ShaderUse(ggl, program0);

   unsigned texels0 [] = {0xffffffff, 0x22222222, 0x66666666, 0xffffffff};
   GGLTexture_t texture0 = {GL_TEXTURE_2D, GGL_PIXEL_FORMAT_RGBA_8888,
                            2, 2, 1, // width, height, levelCount
                            texels0, GGL_CLAMP_TO_EDGE, GGL_MIRRORED_REPEAT, GGL_LINEAR, GGL_LINEAR
                           }; // levels, wrapS, wrapT, minFilter, magFilter

   int sampler2dLoc = ggl->ShaderUniformLocation(program0, "sampler2d");
   if (0 <= sampler2dLoc) {
      int samplerUnit = -1;
      //ggl->ShaderUniformGetiv(ggl, program0, sampler2dLoc, &samplerUnit);
      samplerUnit = sampler2dLoc;
      ggl->SetSampler(ggl, samplerUnit, &texture0);
   }

   Vector4 uVec4 = {1.125f, 1.5f, 1.75f, 1.75f};
   int uVec4Loc = ggl->ShaderUniformLocation(program0, "uVec4");
   ggl->ShaderUniform(program0, uVec4Loc, 1, &uVec4, GL_FLOAT_VEC4);

   VertexInput_t v0 = {0};
   v0.attributes[2] = VECTOR4_CTR(0,0,1,1); // aTexCoord
   v0.attributes[3] = VECTOR4_CTR(0.25f, 0.25f, 0.5f,1); // aPosition

   VertexOutput_t vout0 = {0};
   ggl->ProcessVertex(ggl, &v0, &vout0);
   if (memcmp(&vout0.position,&v0.attributes[3],sizeof(vout0.position))) {
      fprintf(stderr, "gl_Position != aPosition \n");
      assert(0);
   }

   int vTexCoordIndex = ggl->ShaderVaryingLocation(program0, "vTexCoord", NULL) - 2;
   VECTOR4_OP_UNARY(vout0.varyings[vTexCoordIndex],-=,uVec4);
   if (memcmp(&vout0.varyings[vTexCoordIndex],&v0.attributes[2],sizeof uVec4)) {
      fprintf(stderr, "vTexCoord != aTexCoord + uVec4 \n");
      assert(0);
   }
   Vector4 ones = {1,1,1,1};
   int vTexColorIndex = ggl->ShaderVaryingLocation(program0, "vTexColor", NULL) - 2;
   if (memcmp(&vout0.varyings[vTexColorIndex],&ones,sizeof ones)) { // should be the last texel color
      fprintf(stderr, "vTexColor != Vector4(1,1,1,1) \n");
      assert(0);
   }
   if (vout0.pointSize.x != 432) {
      fprintf(stderr, "gl_PointSize != 432 \n");
      assert(0);
   }

   v0.attributes[2] = VECTOR4_CTR(0,0, 1.5f, 1.5f);
   texture0.wrapS = GGL_REPEAT;
   texture0.wrapT = GGL_REPEAT;

   sampler2dLoc = ggl->ShaderUniformLocation(program0, "sampler2d");
   if (0 <= sampler2dLoc) {
      int samplerUnit = -1;
      //ggl->ShaderUniformGetiv(ggl, program0, sampler2dLoc, &samplerUnit);
      samplerUnit = sampler2dLoc;
      ggl->SetSampler(ggl, samplerUnit, &texture0);
   }
   ggl->ShaderUse(ggl, program0);
   ggl->ProcessVertex(ggl, &v0, &vout0);
   const float filtered = (float)(0xff + 0x22 + 0x66 + 0xff) / (4 * 0xff);
   if (!ApproximatelyEqual(vout0.varyings[vTexColorIndex],
                           VECTOR4_CTR(filtered, filtered, filtered, filtered), 1.0f / 255)) {
      fprintf(stderr, "failed linear filter and/or wrapS and wrapT test");
      assert(0);
   }

   const unsigned width = 60, height = 100;

   GGLSurface_t colorSurface = {width, height, GGL_PIXEL_FORMAT_RGBA_8888, malloc(width * height * 4)};
   assert(colorSurface.data);
   ggl->SetBuffer(ggl, GL_COLOR_BUFFER_BIT, &colorSurface);

   GGLSurface_t depthSurface = {width, height, GGL_PIXEL_FORMAT_Z_32, malloc(width * height * 4)};
   assert(depthSurface.data);
   ggl->SetBuffer(ggl, GL_DEPTH_BUFFER_BIT, &depthSurface);

   GGLSurface_t stencilSurface = {width, height, GGL_PIXEL_FORMAT_S_8, malloc(width * height * 1)};
   assert(stencilSurface.data);
   ggl->SetBuffer(ggl, GL_STENCIL_BUFFER_BIT, &stencilSurface);

   ggl->ClearColor(ggl, 0.1f, 0.1f, 0.1f, 1.0f);
   ggl->ClearDepthf(ggl, 0.5f);

// TODO DXL test scanline and fs

   free(colorSurface.data);
   colorSurface.data = NULL;
   free(depthSurface.data);
   depthSurface.data = NULL;
   free(stencilSurface.data);
   stencilSurface.data = NULL;

   ggl->ShaderProgramDelete(ggl, program0);

   puts("*******************");
   puts("*** end of test ***");
   puts("*******************");

   DestroyGGLInterface(ggl);
   return 0;
}