2 * Create shaders in a loop to test memory usage.
6 #include "state_tracker/graw.h"
7 #include "pipe/p_screen.h"
8 #include "pipe/p_context.h"
9 #include "pipe/p_state.h"
10 #include "pipe/p_defines.h"
12 #include "util/u_memory.h" /* Offset() */
13 #include "util/u_draw_quad.h"
16 static int num_iters
= 100;
19 enum pipe_format formats
[] = {
20 PIPE_FORMAT_R8G8B8A8_UNORM
,
21 PIPE_FORMAT_B8G8R8A8_UNORM
,
25 static const int WIDTH
= 300;
26 static const int HEIGHT
= 300;
28 static struct pipe_screen
*screen
= NULL
;
29 static struct pipe_context
*ctx
= NULL
;
30 static struct pipe_surface
*surf
= NULL
;
31 static void *window
= NULL
;
38 static struct vertex vertices
[1] =
41 { 0.0f
, -0.9f
, 0.0f
, 1.0f
},
42 { 1.0f
, 0.0f
, 0.0f
, 1.0f
}
49 static void set_viewport( float x
, float y
,
50 float width
, float height
,
51 float near
, float far
)
54 float half_width
= (float)width
/ 2.0f
;
55 float half_height
= (float)height
/ 2.0f
;
56 float half_depth
= ((float)far
- (float)near
) / 2.0f
;
57 struct pipe_viewport_state vp
;
59 vp
.scale
[0] = half_width
;
60 vp
.scale
[1] = half_height
;
61 vp
.scale
[2] = half_depth
;
64 vp
.translate
[0] = half_width
+ x
;
65 vp
.translate
[1] = half_height
+ y
;
66 vp
.translate
[2] = half_depth
+ z
;
67 vp
.translate
[3] = 0.0f
;
69 ctx
->set_viewport_state( ctx
, &vp
);
72 static void set_vertices( void )
74 struct pipe_vertex_element ve
[2];
75 struct pipe_vertex_buffer vbuf
;
78 memset(ve
, 0, sizeof ve
);
80 ve
[0].src_offset
= Offset(struct vertex
, position
);
81 ve
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
82 ve
[1].src_offset
= Offset(struct vertex
, color
);
83 ve
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
85 handle
= ctx
->create_vertex_elements_state(ctx
, 2, ve
);
86 ctx
->bind_vertex_elements_state(ctx
, handle
);
89 vbuf
.stride
= sizeof(struct vertex
);
90 vbuf
.max_index
= sizeof(vertices
) / vbuf
.stride
;
91 vbuf
.buffer_offset
= 0;
92 vbuf
.buffer
= screen
->user_buffer_create(screen
,
95 PIPE_BIND_VERTEX_BUFFER
);
97 ctx
->set_vertex_buffers(ctx
, 1, &vbuf
);
100 static void set_vertex_shader( void )
107 "DCL OUT[0], POSITION\n"
108 "DCL OUT[1], COLOR\n"
109 " 0: MOV OUT[1], IN[1]\n"
110 " 1: MOV OUT[0], IN[0]\n"
113 handle
= graw_parse_vertex_shader(ctx
, text
);
114 ctx
->bind_vs_state(ctx
, handle
);
120 set_fragment_shader( void )
125 "DCL IN[0], COLOR, LINEAR\n"
126 "DCL OUT[0], COLOR\n"
128 " 0: MUL TEMP[0], IN[0], IN[0]\n"
129 " 1: ADD TEMP[1], IN[0], IN[0]\n"
130 " 2: SUB OUT[0], TEMP[0], TEMP[1]\n"
133 handle
= graw_parse_fragment_shader(ctx
, text
);
138 static void draw( void )
140 float clear_color
[4] = {0, 0, 0, 1};
143 printf("Creating %d shaders\n", num_iters
);
145 for (i
= 0; i
< num_iters
; i
++) {
146 void *fs
= set_fragment_shader();
148 ctx
->bind_fs_state(ctx
, fs
);
150 ctx
->clear(ctx
, PIPE_CLEAR_COLOR
, clear_color
, 0, 0);
151 util_draw_arrays(ctx
, PIPE_PRIM_POINTS
, 0, 1);
152 ctx
->flush(ctx
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
154 ctx
->bind_fs_state(ctx
, NULL
);
155 ctx
->delete_fs_state(ctx
, fs
);
158 screen
->flush_frontbuffer(screen
, surf
, window
);
165 static void init( void )
167 struct pipe_framebuffer_state fb
;
168 struct pipe_resource
*tex
, templat
;
171 /* It's hard to say whether window or screen should be created
172 * first. Different environments would prefer one or the other.
174 * Also, no easy way of querying supported formats if the screen
175 * cannot be created first.
178 window
== NULL
&& formats
[i
] != PIPE_FORMAT_NONE
;
181 screen
= graw_create_window_and_screen(0,0,300,300,
186 ctx
= screen
->context_create(screen
, NULL
);
190 templat
.target
= PIPE_TEXTURE_2D
;
191 templat
.format
= formats
[i
];
192 templat
.width0
= WIDTH
;
193 templat
.height0
= HEIGHT
;
195 templat
.last_level
= 0;
196 templat
.nr_samples
= 1;
197 templat
.bind
= (PIPE_BIND_RENDER_TARGET
|
198 PIPE_BIND_DISPLAY_TARGET
);
200 tex
= screen
->resource_create(screen
, &templat
);
202 fprintf(stderr
, "Unable to create screen texture!\n");
206 surf
= screen
->get_tex_surface(screen
, tex
, 0, 0, 0,
207 PIPE_BIND_RENDER_TARGET
|
208 PIPE_BIND_DISPLAY_TARGET
);
212 memset(&fb
, 0, sizeof fb
);
218 ctx
->set_framebuffer_state(ctx
, &fb
);
221 struct pipe_blend_state blend
;
223 memset(&blend
, 0, sizeof blend
);
224 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
225 handle
= ctx
->create_blend_state(ctx
, &blend
);
226 ctx
->bind_blend_state(ctx
, handle
);
230 struct pipe_depth_stencil_alpha_state depthstencil
;
232 memset(&depthstencil
, 0, sizeof depthstencil
);
233 handle
= ctx
->create_depth_stencil_alpha_state(ctx
, &depthstencil
);
234 ctx
->bind_depth_stencil_alpha_state(ctx
, handle
);
238 struct pipe_rasterizer_state rasterizer
;
240 memset(&rasterizer
, 0, sizeof rasterizer
);
241 rasterizer
.cull_face
= PIPE_FACE_NONE
;
242 rasterizer
.gl_rasterization_rules
= 1;
243 handle
= ctx
->create_rasterizer_state(ctx
, &rasterizer
);
244 ctx
->bind_rasterizer_state(ctx
, handle
);
247 set_viewport(0, 0, WIDTH
, HEIGHT
, 30, 1000);
251 set_fragment_shader();
255 int main( int argc
, char *argv
[] )
258 num_iters
= atoi(argv
[1]);
262 graw_set_display_func( draw
);