Merge remote branch 'origin/master' into glsl2
[mesa.git] / src / gallium / tests / trivial / quad-tex.c
1 /**************************************************************************
2 *
3 * Copyright © 2010 Jakob Bornecrantz
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 **************************************************************************/
25
26
27 #define USE_TRACE 0
28 #define WIDTH 300
29 #define HEIGHT 300
30 #define NEAR 30
31 #define FAR 1000
32 #define FLIP 0
33
34 /* pipe_*_state structs */
35 #include "pipe/p_state.h"
36 /* pipe_context */
37 #include "pipe/p_context.h"
38 /* pipe_screen */
39 #include "pipe/p_screen.h"
40 /* PIPE_* */
41 #include "pipe/p_defines.h"
42 /* TGSI_SEMANTIC_{POSITION|GENERIC} */
43 #include "pipe/p_shader_tokens.h"
44 /* pipe_buffer_* helpers */
45 #include "util/u_inlines.h"
46
47 /* constant state object helper */
48 #include "cso_cache/cso_context.h"
49
50 /* u_sampler_view_default_template */
51 #include "util/u_sampler.h"
52 /* debug_dump_surface_bmp */
53 #include "util/u_debug.h"
54 /* util_draw_vertex_buffer helper */
55 #include "util/u_draw_quad.h"
56 /* FREE & CALLOC_STRUCT */
57 #include "util/u_memory.h"
58 /* util_make_[fragment|vertex]_passthrough_shader */
59 #include "util/u_simple_shaders.h"
60
61 /* sw_screen_create: to get a software pipe driver */
62 #include "target-helpers/inline_sw_helper.h"
63 /* debug_screen_wrap: to wrap with debug pipe drivers */
64 #include "target-helpers/inline_debug_helper.h"
65 /* null software winsys */
66 #include "sw/null/null_sw_winsys.h"
67
68 struct program
69 {
70 struct pipe_screen *screen;
71 struct pipe_context *pipe;
72 struct cso_context *cso;
73
74 struct pipe_blend_state blend;
75 struct pipe_depth_stencil_alpha_state depthstencil;
76 struct pipe_rasterizer_state rasterizer;
77 struct pipe_sampler_state sampler;
78 struct pipe_viewport_state viewport;
79 struct pipe_framebuffer_state framebuffer;
80 struct pipe_vertex_element velem[2];
81
82 void *vs;
83 void *fs;
84
85 float clear_color[4];
86
87 struct pipe_resource *vbuf;
88 struct pipe_resource *target;
89 struct pipe_resource *tex;
90 struct pipe_sampler_view *view;
91 };
92
93 static void init_prog(struct program *p)
94 {
95 /* create the software rasterizer */
96 p->screen = sw_screen_create(null_sw_create());
97 /* wrap the screen with any debugger */
98 p->screen = debug_screen_wrap(p->screen);
99
100 /* create the pipe driver context and cso context */
101 p->pipe = p->screen->context_create(p->screen, NULL);
102 p->cso = cso_create_context(p->pipe);
103
104 /* set clear color */
105 p->clear_color[0] = 0.3;
106 p->clear_color[1] = 0.1;
107 p->clear_color[2] = 0.3;
108 p->clear_color[3] = 1.0;
109
110 /* vertex buffer */
111 {
112 float vertices[4][2][4] = {
113 {
114 { 0.9f, 0.9f, 0.0f, 1.0f },
115 { 1.0f, 1.0f, 0.0f, 1.0f }
116 },
117 {
118 { -0.9f, 0.9f, 0.0f, 1.0f },
119 { 0.0f, 1.0f, 0.0f, 1.0f }
120 },
121 {
122 { -0.9f, -0.9f, 0.0f, 1.0f },
123 { 0.0f, 0.0f, 1.0f, 1.0f }
124 },
125 {
126 { 0.9f, -0.9f, 0.0f, 1.0f },
127 { 1.0f, 0.0f, 1.0f, 1.0f }
128 }
129 };
130
131 p->vbuf = pipe_buffer_create(p->screen, PIPE_BIND_VERTEX_BUFFER, sizeof(vertices));
132 pipe_buffer_write(p->pipe, p->vbuf, 0, sizeof(vertices), vertices);
133 }
134
135 /* render target texture */
136 {
137 struct pipe_resource tmplt;
138 memset(&tmplt, 0, sizeof(tmplt));
139 tmplt.target = PIPE_TEXTURE_2D;
140 tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */
141 tmplt.width0 = WIDTH;
142 tmplt.height0 = HEIGHT;
143 tmplt.depth0 = 1;
144 tmplt.last_level = 0;
145 tmplt.bind = PIPE_BIND_RENDER_TARGET;
146
147 p->target = p->screen->resource_create(p->screen, &tmplt);
148 }
149
150 /* sampler texture */
151 {
152 uint32_t *ptr;
153 struct pipe_transfer *t;
154 struct pipe_resource t_tmplt;
155 struct pipe_sampler_view v_tmplt;
156 struct pipe_subresource sub;
157 struct pipe_box box;
158
159 memset(&t_tmplt, 0, sizeof(t_tmplt));
160 t_tmplt.target = PIPE_TEXTURE_2D;
161 t_tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */
162 t_tmplt.width0 = 2;
163 t_tmplt.height0 = 2;
164 t_tmplt.depth0 = 1;
165 t_tmplt.last_level = 0;
166 t_tmplt.bind = PIPE_BIND_RENDER_TARGET;
167
168 p->tex = p->screen->resource_create(p->screen, &t_tmplt);
169
170 memset(&sub, 0, sizeof(sub));
171 memset(&box, 0, sizeof(box));
172 box.width = 2;
173 box.height = 2;
174
175 t = p->pipe->get_transfer(p->pipe, p->tex, sub, PIPE_TRANSFER_WRITE, &box);
176
177 ptr = p->pipe->transfer_map(p->pipe, t);
178 ptr[0] = 0xffff0000;
179 ptr[1] = 0xff0000ff;
180 ptr[2] = 0xff00ff00;
181 ptr[3] = 0xffffff00;
182 p->pipe->transfer_unmap(p->pipe, t);
183
184 p->pipe->transfer_destroy(p->pipe, t);
185
186 u_sampler_view_default_template(&v_tmplt, p->tex, p->tex->format);
187
188 p->view = p->pipe->create_sampler_view(p->pipe, p->tex, &v_tmplt);
189 }
190
191 /* disabled blending/masking */
192 memset(&p->blend, 0, sizeof(p->blend));
193 p->blend.rt[0].colormask = PIPE_MASK_RGBA;
194
195 /* no-op depth/stencil/alpha */
196 memset(&p->depthstencil, 0, sizeof(p->depthstencil));
197
198 /* rasterizer */
199 memset(&p->rasterizer, 0, sizeof(p->rasterizer));
200 p->rasterizer.cull_face = PIPE_FACE_NONE;
201 p->rasterizer.gl_rasterization_rules = 1;
202
203 /* sampler */
204 memset(&p->sampler, 0, sizeof(p->sampler));
205 p->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
206 p->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
207 p->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
208 p->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
209 p->sampler.min_img_filter = PIPE_TEX_MIPFILTER_LINEAR;
210 p->sampler.mag_img_filter = PIPE_TEX_MIPFILTER_LINEAR;
211 p->sampler.normalized_coords = 1;
212
213 /* drawing destination */
214 memset(&p->framebuffer, 0, sizeof(p->framebuffer));
215 p->framebuffer.width = WIDTH;
216 p->framebuffer.height = HEIGHT;
217 p->framebuffer.nr_cbufs = 1;
218 p->framebuffer.cbufs[0] = p->screen->get_tex_surface(p->screen, p->target, 0, 0, 0, PIPE_BIND_RENDER_TARGET);
219
220 /* viewport, depth isn't really needed */
221 {
222 float x = 0;
223 float y = 0;
224 float z = FAR;
225 float half_width = (float)WIDTH / 2.0f;
226 float half_height = (float)HEIGHT / 2.0f;
227 float half_depth = ((float)FAR - (float)NEAR) / 2.0f;
228 float scale, bias;
229
230 if (FLIP) {
231 scale = -1.0f;
232 bias = (float)HEIGHT;
233 } else {
234 scale = 1.0f;
235 bias = 0.0f;
236 }
237
238 p->viewport.scale[0] = half_width;
239 p->viewport.scale[1] = half_height * scale;
240 p->viewport.scale[2] = half_depth;
241 p->viewport.scale[3] = 1.0f;
242
243 p->viewport.translate[0] = half_width + x;
244 p->viewport.translate[1] = (half_height + y) * scale + bias;
245 p->viewport.translate[2] = half_depth + z;
246 p->viewport.translate[3] = 0.0f;
247 }
248
249 /* vertex elements state */
250 memset(p->velem, 0, sizeof(p->velem));
251 p->velem[0].src_offset = 0 * 4 * sizeof(float); /* offset 0, first element */
252 p->velem[0].instance_divisor = 0;
253 p->velem[0].vertex_buffer_index = 0;
254 p->velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
255
256 p->velem[1].src_offset = 1 * 4 * sizeof(float); /* offset 16, second element */
257 p->velem[1].instance_divisor = 0;
258 p->velem[1].vertex_buffer_index = 0;
259 p->velem[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
260
261 /* vertex shader */
262 {
263 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
264 TGSI_SEMANTIC_GENERIC };
265 const uint semantic_indexes[] = { 0, 0 };
266 p->vs = util_make_vertex_passthrough_shader(p->pipe, 2, semantic_names, semantic_indexes);
267 }
268
269 /* fragment shader */
270 p->fs = util_make_fragment_tex_shader(p->pipe, TGSI_TEXTURE_2D, TGSI_INTERPOLATE_LINEAR);
271 }
272
273 static void close_prog(struct program *p)
274 {
275 /* unset bound textures as well */
276 cso_set_fragment_sampler_views(p->cso, 0, NULL);
277
278 /* unset all state */
279 cso_release_all(p->cso);
280
281 p->pipe->delete_vs_state(p->pipe, p->vs);
282 p->pipe->delete_fs_state(p->pipe, p->fs);
283
284 pipe_surface_reference(&p->framebuffer.cbufs[0], NULL);
285 pipe_sampler_view_reference(&p->view, NULL);
286 pipe_resource_reference(&p->target, NULL);
287 pipe_resource_reference(&p->tex, NULL);
288 pipe_resource_reference(&p->vbuf, NULL);
289
290 cso_destroy_context(p->cso);
291 p->pipe->destroy(p->pipe);
292 p->screen->destroy(p->screen);
293
294 FREE(p);
295 }
296
297 static void draw(struct program *p)
298 {
299 /* set the render target */
300 cso_set_framebuffer(p->cso, &p->framebuffer);
301
302 /* clear the render target */
303 p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR, p->clear_color, 0, 0);
304
305 /* set misc state we care about */
306 cso_set_blend(p->cso, &p->blend);
307 cso_set_depth_stencil_alpha(p->cso, &p->depthstencil);
308 cso_set_rasterizer(p->cso, &p->rasterizer);
309 cso_set_viewport(p->cso, &p->viewport);
310
311 /* sampler */
312 cso_single_sampler(p->cso, 0, &p->sampler);
313 cso_single_sampler_done(p->cso);
314
315 /* texture sampler view */
316 cso_set_fragment_sampler_views(p->cso, 1, &p->view);
317
318 /* shaders */
319 cso_set_fragment_shader_handle(p->cso, p->fs);
320 cso_set_vertex_shader_handle(p->cso, p->vs);
321
322 /* vertex element data */
323 cso_set_vertex_elements(p->cso, 2, p->velem);
324
325 util_draw_vertex_buffer(p->pipe,
326 p->vbuf, 0,
327 PIPE_PRIM_QUADS,
328 4, /* verts */
329 2); /* attribs/vert */
330
331 p->pipe->flush(p->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
332
333 debug_dump_surface_bmp(p->pipe, "result.bmp", p->framebuffer.cbufs[0]);
334 }
335
336 int main(int argc, char** argv)
337 {
338 struct program *p = CALLOC_STRUCT(program);
339
340 init_prog(p);
341 draw(p);
342 close_prog(p);
343
344 return 0;
345 }