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