2 * Test draw instancing.
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"
14 #include "util/u_memory.h" /* Offset() */
15 #include "util/u_draw_quad.h"
18 enum pipe_format formats[] = {
19 PIPE_FORMAT_R8G8B8A8_UNORM,
20 PIPE_FORMAT_B8G8R8A8_UNORM,
24 static const int WIDTH = 300;
25 static const int HEIGHT = 300;
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;
39 static int draw_elements = 0;
44 * Each vertex has three attributes: position, color and translation.
45 * The translation attribute is a per-instance attribute. See
46 * "instance_divisor" below.
48 static struct vertex vertices[4] =
51 { 0.0f, -0.3f, 0.0f, 1.0f }, /* pos */
52 { 1.0f, 0.0f, 0.0f, 1.0f } /* color */
55 { -0.2f, 0.3f, 0.0f, 1.0f },
56 { 0.0f, 1.0f, 0.0f, 1.0f }
59 { 0.2f, 0.3f, 0.0f, 1.0f },
60 { 0.0f, 0.0f, 1.0f, 1.0f }
67 static float inst_data[NUM_INST][4] =
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 }
77 static ushort indices[3] = { 0, 2, 1 };
80 static void set_viewport( float x, float y,
81 float width, float height,
82 float near, float 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;
90 vp.scale[0] = half_width;
91 vp.scale[1] = half_height;
92 vp.scale[2] = half_depth;
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;
100 ctx->set_viewport_state( ctx, &vp );
104 static void set_vertices( void )
106 struct pipe_vertex_element ve[3];
107 struct pipe_vertex_buffer vbuf[2];
108 struct pipe_index_buffer ibuf;
111 memset(ve, 0, sizeof ve);
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;
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;
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;
129 handle = ctx->create_vertex_elements_state(ctx, 3, ve);
130 ctx->bind_vertex_elements_state(ctx, handle);
134 vbuf[0].stride = sizeof( struct vertex );
135 vbuf[0].buffer_offset = 0;
136 vbuf[0].buffer = screen->user_buffer_create(screen,
139 PIPE_BIND_VERTEX_BUFFER);
142 vbuf[1].stride = sizeof( inst_data[0] );
143 vbuf[1].buffer_offset = 0;
144 vbuf[1].buffer = screen->user_buffer_create(screen,
147 PIPE_BIND_VERTEX_BUFFER);
150 ctx->set_vertex_buffers(ctx, 2, vbuf);
153 ibuf.buffer = screen->user_buffer_create(screen,
156 PIPE_BIND_VERTEX_BUFFER);
160 ctx->set_index_buffer(ctx, &ibuf);
164 static void set_vertex_shader( void )
172 "DCL OUT[0], POSITION\n"
173 "DCL OUT[1], COLOR\n"
174 " 0: MOV OUT[1], IN[1]\n"
175 " 1: ADD OUT[0], IN[0], IN[2]\n" /* add instance pos to vertex pos */
178 handle = graw_parse_vertex_shader(ctx, text);
179 ctx->bind_vs_state(ctx, handle);
182 static void set_fragment_shader( void )
187 "DCL IN[0], COLOR, LINEAR\n"
188 "DCL OUT[0], COLOR\n"
189 " 0: MOV OUT[0], IN[0]\n"
192 handle = graw_parse_fragment_shader(ctx, text);
193 ctx->bind_fs_state(ctx, handle);
197 static void draw( void )
199 union pipe_color_union clear_color = { {1,0,1,1} };
200 struct pipe_draw_info info;
202 ctx->clear(ctx, PIPE_CLEAR_COLOR, &clear_color, 0, 0);
204 util_draw_init_info(&info);
205 info.indexed = (draw_elements != 0);
206 info.mode = PIPE_PRIM_TRIANGLES;
209 /* draw NUM_INST triangles */
210 info.instance_count = NUM_INST;
212 ctx->draw_vbo(ctx, &info);
214 ctx->flush(ctx, NULL);
216 graw_save_surface_to_file(ctx, surf, NULL);
218 screen->flush_frontbuffer(screen, tex, 0, 0, window);
222 static void init( void )
224 struct pipe_framebuffer_state fb;
225 struct pipe_resource templat;
226 struct pipe_surface surf_tmpl;
229 /* It's hard to say whether window or screen should be created
230 * first. Different environments would prefer one or the other.
232 * Also, no easy way of querying supported formats if the screen
233 * cannot be created first.
235 for (i = 0; formats[i] != PIPE_FORMAT_NONE; i++) {
236 screen = graw_create_window_and_screen(0, 0, 300, 300,
239 if (window && screen)
242 if (!screen || !window) {
243 fprintf(stderr, "Unable to create window\n");
247 ctx = screen->context_create(screen, NULL);
251 templat.target = PIPE_TEXTURE_2D;
252 templat.format = formats[i];
253 templat.width0 = WIDTH;
254 templat.height0 = HEIGHT;
256 templat.array_size = 1;
257 templat.last_level = 0;
258 templat.nr_samples = 1;
259 templat.bind = (PIPE_BIND_RENDER_TARGET |
260 PIPE_BIND_DISPLAY_TARGET);
262 tex = screen->resource_create(screen,
267 surf_tmpl.format = templat.format;
268 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
269 surf_tmpl.u.tex.level = 0;
270 surf_tmpl.u.tex.first_layer = 0;
271 surf_tmpl.u.tex.last_layer = 0;
272 surf = ctx->create_surface(ctx, tex, &surf_tmpl);
276 memset(&fb, 0, sizeof fb);
282 ctx->set_framebuffer_state(ctx, &fb);
285 struct pipe_blend_state blend;
287 memset(&blend, 0, sizeof blend);
288 blend.rt[0].colormask = PIPE_MASK_RGBA;
289 handle = ctx->create_blend_state(ctx, &blend);
290 ctx->bind_blend_state(ctx, handle);
294 struct pipe_depth_stencil_alpha_state depthstencil;
296 memset(&depthstencil, 0, sizeof depthstencil);
297 handle = ctx->create_depth_stencil_alpha_state(ctx, &depthstencil);
298 ctx->bind_depth_stencil_alpha_state(ctx, handle);
302 struct pipe_rasterizer_state rasterizer;
304 memset(&rasterizer, 0, sizeof rasterizer);
305 rasterizer.cull_face = PIPE_FACE_NONE;
306 rasterizer.gl_rasterization_rules = 1;
307 rasterizer.depth_clip = 1;
308 handle = ctx->create_rasterizer_state(ctx, &rasterizer);
309 ctx->bind_rasterizer_state(ctx, handle);
312 set_viewport(0, 0, WIDTH, HEIGHT, 30, 1000);
315 set_fragment_shader();
319 static void options(int argc, char *argv[])
323 for (i = 1; i < argc;) {
324 if (graw_parse_args(&i, argc, argv)) {
327 if (strcmp(argv[i], "-e") == 0) {
336 printf("Using pipe_context::draw_elements_instanced()\n");
338 printf("Using pipe_context::draw_arrays_instanced()\n");
342 int main( int argc, char *argv[] )
348 graw_set_display_func( draw );