vertex/varying: Add symbol_print_varying function.
[lima:aesok-lima.git] / vertex / varying / varying.c
1 /*
2  * Copyright 2011-2012 Luc Verhaegen <libv@codethink.co.uk>
3  *           2012 Anatoly Sokolov <aesok@post.ru>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sub license,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  */
25
26 /*
27  * Template file for replaying a dumped stream.
28  */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdbool.h>
33
34 #include <GLES2/gl2.h>
35
36 #include "limare.h"
37 #include "bmp.h"
38 #include "fb.h"
39 #include "symbols.h"
40 #include "gp.h"
41 #include "pp.h"
42 #include "program.h"
43
44 #define WIDTH 800
45 #define HEIGHT 480
46
47 struct symbol *
48 symbol_find (struct symbol **symbols, int count, const char *name)
49 {
50   int i;
51
52   for (i = 0; i < count; i++, symbols++)
53     {
54       if (!strcmp((*symbols)->name, name))
55         {
56           return *symbols;
57         }
58     }
59
60   return NULL;
61 }
62
63 void
64 symbol_print_varying (struct symbol *symbol, bool is_data_float)
65 {
66   char *type;
67   int i;
68
69   switch(symbol->type)
70    {
71      case SYMBOL_UNIFORM:
72        type = "uniform";
73        break;
74
75      case SYMBOL_ATTRIBUTE:
76        type = "attribute";
77        break;
78
79      case SYMBOL_VARYING:
80        type = "varying";
81        break;
82   }
83
84   printf("Symbol %s (%s) = {\n", symbol->name, type);
85   printf("\t.component_size = %d,\n", symbol->component_size);
86   printf("\t.component_count = %d,\n", symbol->component_count);
87   printf("\t.entry_count = %d,\n", symbol->entry_count);
88   printf("\t.src_stride = %d,\n", symbol->src_stride);
89   printf("\t.dst_stride = %d,\n", symbol->dst_stride);
90   printf("\t.size = %d,\n", symbol->size);
91   printf("\t.offset = %d,\n", symbol->offset);
92   printf("\t.address = %p,\n", symbol->address);
93   printf("\t.physical = 0x%x,\n", symbol->physical);
94
95   if (symbol->data)
96     {
97       float *data = symbol->data;
98
99       for (i = 0; i < (symbol->size / 4); i++)
100         printf("\t.data[0x%02x] = %f (0x%x)\n",
101                i, data[i], *((int *)(&data[i])));
102     }
103
104
105   if (symbol->address)
106     {
107       if (is_data_float)
108         {
109           float *data = symbol->address;
110
111           for (i = 0; i < (symbol->component_count * symbol->entry_count); i++)
112             printf("\t.address[0x%02x] = %f (0x%x)\n",
113                    i, data[i], *((int *)(&data[i])));
114         }
115       else
116         {
117           __fp16 *data = symbol->address;
118
119           for (i = 0; i < (symbol->component_count * symbol->entry_count); i++)
120             printf("\t.address[0x%02x] = %f (0x%x)\n",
121                    i, (float) data[i], *((unsigned short *)(&data[i])));
122         }
123     }
124
125
126   printf("};\n");
127 }
128
129 int
130 main(int argc, char *argv[])
131 {
132         struct limare_state *state;
133         int ret;
134
135         float vertices[] = { -0.45, -0.75, 0.0,
136                                  0.45, -0.75, 0.0,
137                                 -0.45,  0.75, 0.0,
138                                  0.45,  0.75, 0.0 };
139         float test_a[] = {1.0, 0.0, 0.0, 1.0,
140                           0.0, 1.0, 1.0, 0.0,
141                           0.0, 1.0, 0.0, 1.0,
142                           1.0, 1.0, 1.0, 1.0};
143         float color[] = {1.0, 0.0, 0.0, 1.0 };
144
145         const char *vertex_shader_source =
146                 "precision mediump float;     \n"
147                 "attribute vec4 aPosition;    \n"
148                 "attribute vec4 aTestA;       \n"
149                 "                             \n"
150                 "varying vec4 vTestA;         \n"
151                 "varying vec4 vTestC;         \n"
152                 "                             \n"
153                 "void main()                  \n"
154                 "{                            \n"
155                 "    vTestA = aTestA;         \n"
156                 "    vTestC = vec4 (1.0, 2.0, 3.0, 4.0); \n"
157                 "                             \n"
158                 "    gl_Position = aPosition; \n"
159                 "}                            \n";
160
161         const char *fragment_shader_source =
162                 "precision mediump float;     \n"
163                 "uniform vec4 uColor;         \n"
164                 "                             \n"
165                 "varying vec4 vTestA;         \n"
166                 "varying vec4 vTestC;         \n"
167                 "                             \n"
168                 "void main()                  \n"
169                 "{                            \n"
170                 "    gl_FragColor = uColor + vTestA + vTestC;   \n"
171                 "}                            \n";
172
173         fb_clear();
174
175         state = limare_init();
176         if (!state)
177                 return -1;
178
179         ret = limare_state_setup(state, WIDTH, HEIGHT, 0xFF505050);
180         if (ret)
181                 return ret;
182
183         vertex_shader_attach(state, vertex_shader_source);
184         fragment_shader_attach(state, fragment_shader_source);
185
186         limare_link(state);
187
188         limare_attribute_pointer(state, "aPosition", 4, 3, vertices);
189         limare_attribute_pointer(state, "aTestA", 4, 4, test_a);
190
191         limare_uniform_attach(state, "uColor", 4, 4, color);
192
193         ret = limare_draw_arrays(state, GL_TRIANGLE_STRIP, 0, 4);
194         if (ret)
195                 return ret;
196
197         ret = limare_flush(state);
198         if (ret)
199                 return ret;
200
201         bmp_dump(state->pp->frame_address, state, "/mnt/extsd/limare/varying.bmp");
202
203         fb_dump(state->pp->frame_address, 0, state->width, state->height);
204
205         {
206           struct symbol * varying;
207
208           varying = symbol_find (&state->draws[0]->vs[0].varyings[0],
209                                  state->draws[0]->vs[0].varying_count,
210                                  "gl_Position");
211           if (varying != NULL)
212             {
213               symbol_print_varying (varying, true);
214             }
215
216           varying = symbol_find (&state->draws[0]->vs[0].varyings[0],
217                                  state->draws[0]->vs[0].varying_count,
218                                  "vTestA");
219           if (varying != NULL)
220             {
221               symbol_print_varying (varying, false);
222             }
223
224           varying = symbol_find (&state->draws[0]->vs[0].varyings[0],
225                                  state->draws[0]->vs[0].varying_count,
226                                  "vTestC");
227           if (varying != NULL)
228             {
229               symbol_print_varying (varying, false);
230             }
231         }
232
233         limare_finish();
234
235         return 0;
236 }
237