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_debug.h" /* debug_dump_surface_bmp() */
15 #include "util/u_memory.h" /* Offset() */
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
*indexBuffer
= 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];
110 memset(ve
, 0, sizeof ve
);
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;
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;
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;
128 handle
= ctx
->create_vertex_elements_state(ctx
, 3, ve
);
129 ctx
->bind_vertex_elements_state(ctx
, handle
);
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
,
139 PIPE_BIND_VERTEX_BUFFER
);
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
,
148 PIPE_BIND_VERTEX_BUFFER
);
151 ctx
->set_vertex_buffers(ctx
, 2, vbuf
);
154 indexBuffer
= screen
->user_buffer_create(screen
,
157 PIPE_BIND_VERTEX_BUFFER
);
162 static void set_vertex_shader( void )
170 "DCL OUT[0], POSITION\n"
171 "DCL OUT[1], COLOR\n"
172 " 0: MOV OUT[1], IN[1]\n"
173 " 1: ADD OUT[0], IN[0], IN[2]\n" /* add instance pos to vertex pos */
176 handle
= graw_parse_vertex_shader(ctx
, text
);
177 ctx
->bind_vs_state(ctx
, handle
);
180 static void set_fragment_shader( void )
185 "DCL IN[0], COLOR, LINEAR\n"
186 "DCL OUT[0], COLOR\n"
187 " 0: MOV OUT[0], IN[0]\n"
190 handle
= graw_parse_fragment_shader(ctx
, text
);
191 ctx
->bind_fs_state(ctx
, handle
);
195 static void draw( void )
197 float clear_color
[4] = {1,0,1,1};
199 ctx
->clear(ctx
, PIPE_CLEAR_COLOR
, clear_color
, 0, 0);
201 /* draw NUM_INST triangles */
203 ctx
->draw_elements_instanced(ctx
, indexBuffer
, 2,
206 0, 3, /* start, count */
207 0, NUM_INST
); /* startInst, instCount */
209 ctx
->draw_arrays_instanced(ctx
, PIPE_PRIM_TRIANGLES
, 0, 3, 0, NUM_INST
);
211 ctx
->flush(ctx
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
214 /* At the moment, libgraw leaks out/makes available some of the
215 * symbols from gallium/auxiliary, including these debug helpers.
216 * Will eventually want to bless some of these paths, and lock the
217 * others down so they aren't accessible from test programs.
219 * This currently just happens to work on debug builds - a release
220 * build will probably fail to link here:
222 debug_dump_surface_bmp(ctx
, "result.bmp", surf
);
225 screen
->flush_frontbuffer(screen
, surf
, window
);
229 static void init( void )
231 struct pipe_framebuffer_state fb
;
232 struct pipe_resource
*tex
, templat
;
235 /* It's hard to say whether window or screen should be created
236 * first. Different environments would prefer one or the other.
238 * Also, no easy way of querying supported formats if the screen
239 * cannot be created first.
242 window
== NULL
&& formats
[i
] != PIPE_FORMAT_NONE
;
245 screen
= graw_create_window_and_screen(0,0,300,300,
250 ctx
= screen
->context_create(screen
, NULL
);
254 templat
.target
= PIPE_TEXTURE_2D
;
255 templat
.format
= formats
[i
];
256 templat
.width0
= WIDTH
;
257 templat
.height0
= HEIGHT
;
259 templat
.last_level
= 0;
260 templat
.nr_samples
= 1;
261 templat
.bind
= (PIPE_BIND_RENDER_TARGET
|
262 PIPE_BIND_DISPLAY_TARGET
);
264 tex
= screen
->resource_create(screen
,
269 surf
= screen
->get_tex_surface(screen
, tex
, 0, 0, 0,
270 PIPE_BIND_RENDER_TARGET
|
271 PIPE_BIND_DISPLAY_TARGET
);
275 memset(&fb
, 0, sizeof fb
);
281 ctx
->set_framebuffer_state(ctx
, &fb
);
284 struct pipe_blend_state blend
;
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
);
293 struct pipe_depth_stencil_alpha_state depthstencil
;
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
);
301 struct pipe_rasterizer_state rasterizer
;
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
);
310 set_viewport(0, 0, WIDTH
, HEIGHT
, 30, 1000);
313 set_fragment_shader();
317 static void options(int argc
, char *argv
[])
320 for (i
= 1; i
< argc
; i
++) {
321 if (strcmp(argv
[i
], "-e") == 0)
325 printf("Using pipe_context::draw_elements_instanced()\n");
327 printf("Using pipe_context::draw_arrays_instanced()\n");
331 int main( int argc
, char *argv
[] )
337 graw_set_display_func( draw
);