Merge branch 'lp-offset-twoside'
[mesa.git] / src / gallium / tests / graw / tri-instanced.c
1 /*
2 * Test draw instancing.
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "state_tracker/graw.h"
9 #include "pipe/p_screen.h"
10 #include "pipe/p_context.h"
11 #include "pipe/p_state.h"
12 #include "pipe/p_defines.h"
13
14 #include "util/u_memory.h" /* Offset() */
15 #include "util/u_draw_quad.h"
16
17
18 enum pipe_format formats[] = {
19 PIPE_FORMAT_R8G8B8A8_UNORM,
20 PIPE_FORMAT_B8G8R8A8_UNORM,
21 PIPE_FORMAT_NONE
22 };
23
24 static const int WIDTH = 300;
25 static const int HEIGHT = 300;
26
27 static struct pipe_screen *screen = NULL;
28 static struct pipe_context *ctx = NULL;
29 static struct pipe_surface *surf = NULL;
30 static void *window = NULL;
31
32 struct vertex {
33 float position[4];
34 float color[4];
35 };
36
37
38 static int draw_elements = 0;
39
40
41 /**
42 * Vertex data.
43 * Each vertex has three attributes: position, color and translation.
44 * The translation attribute is a per-instance attribute. See
45 * "instance_divisor" below.
46 */
47 static struct vertex vertices[4] =
48 {
49 {
50 { 0.0f, -0.3f, 0.0f, 1.0f }, /* pos */
51 { 1.0f, 0.0f, 0.0f, 1.0f } /* color */
52 },
53 {
54 { -0.2f, 0.3f, 0.0f, 1.0f },
55 { 0.0f, 1.0f, 0.0f, 1.0f }
56 },
57 {
58 { 0.2f, 0.3f, 0.0f, 1.0f },
59 { 0.0f, 0.0f, 1.0f, 1.0f }
60 }
61 };
62
63
64 #define NUM_INST 5
65
66 static float inst_data[NUM_INST][4] =
67 {
68 { -0.50f, 0.4f, 0.0f, 0.0f },
69 { -0.25f, 0.1f, 0.0f, 0.0f },
70 { 0.00f, 0.2f, 0.0f, 0.0f },
71 { 0.25f, 0.1f, 0.0f, 0.0f },
72 { 0.50f, 0.3f, 0.0f, 0.0f }
73 };
74
75
76 static ushort indices[3] = { 0, 2, 1 };
77
78
79 static void set_viewport( float x, float y,
80 float width, float height,
81 float near, float far)
82 {
83 float z = far;
84 float half_width = (float)width / 2.0f;
85 float half_height = (float)height / 2.0f;
86 float half_depth = ((float)far - (float)near) / 2.0f;
87 struct pipe_viewport_state vp;
88
89 vp.scale[0] = half_width;
90 vp.scale[1] = half_height;
91 vp.scale[2] = half_depth;
92 vp.scale[3] = 1.0f;
93
94 vp.translate[0] = half_width + x;
95 vp.translate[1] = half_height + y;
96 vp.translate[2] = half_depth + z;
97 vp.translate[3] = 0.0f;
98
99 ctx->set_viewport_state( ctx, &vp );
100 }
101
102
103 static void set_vertices( void )
104 {
105 struct pipe_vertex_element ve[3];
106 struct pipe_vertex_buffer vbuf[2];
107 struct pipe_index_buffer ibuf;
108 void *handle;
109
110 memset(ve, 0, sizeof ve);
111
112 /* pos */
113 ve[0].src_offset = Offset(struct vertex, position);
114 ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
115 ve[0].vertex_buffer_index = 0;
116
117 /* color */
118 ve[1].src_offset = Offset(struct vertex, color);
119 ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
120 ve[1].vertex_buffer_index = 0;
121
122 /* per-instance info */
123 ve[2].src_offset = 0;
124 ve[2].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
125 ve[2].vertex_buffer_index = 1;
126 ve[2].instance_divisor = 1;
127
128 handle = ctx->create_vertex_elements_state(ctx, 3, ve);
129 ctx->bind_vertex_elements_state(ctx, handle);
130
131
132 /* vertex data */
133 vbuf[0].stride = sizeof( struct vertex );
134 vbuf[0].max_index = sizeof(vertices) / vbuf[0].stride;
135 vbuf[0].buffer_offset = 0;
136 vbuf[0].buffer = screen->user_buffer_create(screen,
137 vertices,
138 sizeof(vertices),
139 PIPE_BIND_VERTEX_BUFFER);
140
141 /* instance data */
142 vbuf[1].stride = sizeof( inst_data[0] );
143 vbuf[1].max_index = sizeof(inst_data) / vbuf[1].stride;
144 vbuf[1].buffer_offset = 0;
145 vbuf[1].buffer = screen->user_buffer_create(screen,
146 inst_data,
147 sizeof(inst_data),
148 PIPE_BIND_VERTEX_BUFFER);
149
150
151 ctx->set_vertex_buffers(ctx, 2, vbuf);
152
153 /* index data */
154 ibuf.buffer = screen->user_buffer_create(screen,
155 indices,
156 sizeof(indices),
157 PIPE_BIND_VERTEX_BUFFER);
158 ibuf.offset = 0;
159 ibuf.index_size = 2;
160
161 ctx->set_index_buffer(ctx, &ibuf);
162
163 }
164
165 static void set_vertex_shader( void )
166 {
167 void *handle;
168 const char *text =
169 "VERT\n"
170 "DCL IN[0]\n"
171 "DCL IN[1]\n"
172 "DCL IN[2]\n"
173 "DCL OUT[0], POSITION\n"
174 "DCL OUT[1], COLOR\n"
175 " 0: MOV OUT[1], IN[1]\n"
176 " 1: ADD OUT[0], IN[0], IN[2]\n" /* add instance pos to vertex pos */
177 " 2: END\n";
178
179 handle = graw_parse_vertex_shader(ctx, text);
180 ctx->bind_vs_state(ctx, handle);
181 }
182
183 static void set_fragment_shader( void )
184 {
185 void *handle;
186 const char *text =
187 "FRAG\n"
188 "DCL IN[0], COLOR, LINEAR\n"
189 "DCL OUT[0], COLOR\n"
190 " 0: MOV OUT[0], IN[0]\n"
191 " 1: END\n";
192
193 handle = graw_parse_fragment_shader(ctx, text);
194 ctx->bind_fs_state(ctx, handle);
195 }
196
197
198 static void draw( void )
199 {
200 float clear_color[4] = {1,0,1,1};
201 struct pipe_draw_info info;
202
203 ctx->clear(ctx, PIPE_CLEAR_COLOR, clear_color, 0, 0);
204
205 util_draw_init_info(&info);
206 info.indexed = (draw_elements != 0);
207 info.mode = PIPE_PRIM_TRIANGLES;
208 info.start = 0;
209 info.count = 3;
210 /* draw NUM_INST triangles */
211 info.instance_count = NUM_INST;
212
213 ctx->draw_vbo(ctx, &info);
214
215 ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
216
217 graw_save_surface_to_file(ctx, surf, NULL);
218
219 screen->flush_frontbuffer(screen, surf, window);
220 }
221
222
223 static void init( void )
224 {
225 struct pipe_framebuffer_state fb;
226 struct pipe_resource *tex, templat;
227 int i;
228
229 /* It's hard to say whether window or screen should be created
230 * first. Different environments would prefer one or the other.
231 *
232 * Also, no easy way of querying supported formats if the screen
233 * cannot be created first.
234 */
235 for (i = 0;
236 window == NULL && formats[i] != PIPE_FORMAT_NONE;
237 i++) {
238
239 screen = graw_create_window_and_screen(0,0,300,300,
240 formats[i],
241 &window);
242 }
243
244 ctx = screen->context_create(screen, NULL);
245 if (ctx == NULL)
246 exit(3);
247
248 templat.target = PIPE_TEXTURE_2D;
249 templat.format = formats[i];
250 templat.width0 = WIDTH;
251 templat.height0 = HEIGHT;
252 templat.depth0 = 1;
253 templat.last_level = 0;
254 templat.nr_samples = 1;
255 templat.bind = (PIPE_BIND_RENDER_TARGET |
256 PIPE_BIND_DISPLAY_TARGET);
257
258 tex = screen->resource_create(screen,
259 &templat);
260 if (tex == NULL)
261 exit(4);
262
263 surf = screen->get_tex_surface(screen, tex, 0, 0, 0,
264 PIPE_BIND_RENDER_TARGET |
265 PIPE_BIND_DISPLAY_TARGET);
266 if (surf == NULL)
267 exit(5);
268
269 memset(&fb, 0, sizeof fb);
270 fb.nr_cbufs = 1;
271 fb.width = WIDTH;
272 fb.height = HEIGHT;
273 fb.cbufs[0] = surf;
274
275 ctx->set_framebuffer_state(ctx, &fb);
276
277 {
278 struct pipe_blend_state blend;
279 void *handle;
280 memset(&blend, 0, sizeof blend);
281 blend.rt[0].colormask = PIPE_MASK_RGBA;
282 handle = ctx->create_blend_state(ctx, &blend);
283 ctx->bind_blend_state(ctx, handle);
284 }
285
286 {
287 struct pipe_depth_stencil_alpha_state depthstencil;
288 void *handle;
289 memset(&depthstencil, 0, sizeof depthstencil);
290 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
291 ctx->bind_depth_stencil_alpha_state(ctx, handle);
292 }
293
294 {
295 struct pipe_rasterizer_state rasterizer;
296 void *handle;
297 memset(&rasterizer, 0, sizeof rasterizer);
298 rasterizer.cull_face = PIPE_FACE_NONE;
299 rasterizer.gl_rasterization_rules = 1;
300 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
301 ctx->bind_rasterizer_state(ctx, handle);
302 }
303
304 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
305 set_vertices();
306 set_vertex_shader();
307 set_fragment_shader();
308 }
309
310
311 static void options(int argc, char *argv[])
312 {
313 int i;
314
315 for (i = 1; i < argc;) {
316 if (graw_parse_args(&i, argc, argv)) {
317 continue;
318 }
319 if (strcmp(argv[i], "-e") == 0) {
320 draw_elements = 1;
321 i++;
322 }
323 else {
324 i++;
325 }
326 }
327 if (draw_elements)
328 printf("Using pipe_context::draw_elements_instanced()\n");
329 else
330 printf("Using pipe_context::draw_arrays_instanced()\n");
331 }
332
333
334 int main( int argc, char *argv[] )
335 {
336 options(argc, argv);
337
338 init();
339
340 graw_set_display_func( draw );
341 graw_main_loop();
342 return 0;
343 }