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