gallium: support for array textures and related changes
[mesa.git] / src / gallium / tests / trivial / tri.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 /* debug_dump_surface_bmp */
51 #include "util/u_debug.h"
52 /* util_draw_vertex_buffer helper */
53 #include "util/u_draw_quad.h"
54 /* FREE & CALLOC_STRUCT */
55 #include "util/u_memory.h"
56 /* util_make_[fragment|vertex]_passthrough_shader */
57 #include "util/u_simple_shaders.h"
58
59 /* sw_screen_create: to get a software pipe driver */
60 #include "target-helpers/inline_sw_helper.h"
61 /* debug_screen_wrap: to wrap with debug pipe drivers */
62 #include "target-helpers/inline_debug_helper.h"
63 /* null software winsys */
64 #include "sw/null/null_sw_winsys.h"
65
66 struct program
67 {
68 struct pipe_screen *screen;
69 struct pipe_context *pipe;
70 struct cso_context *cso;
71
72 struct pipe_blend_state blend;
73 struct pipe_depth_stencil_alpha_state depthstencil;
74 struct pipe_rasterizer_state rasterizer;
75 struct pipe_viewport_state viewport;
76 struct pipe_framebuffer_state framebuffer;
77 struct pipe_vertex_element velem[2];
78
79 void *vs;
80 void *fs;
81
82 float clear_color[4];
83
84 struct pipe_resource *vbuf;
85 struct pipe_resource *target;
86 };
87
88 static void init_prog(struct program *p)
89 {
90 struct pipe_surface surf_tmpl;
91 /* create the software rasterizer */
92 p->screen = sw_screen_create(null_sw_create());
93 /* wrap the screen with any debugger */
94 p->screen = debug_screen_wrap(p->screen);
95
96 /* create the pipe driver context and cso context */
97 p->pipe = p->screen->context_create(p->screen, NULL);
98 p->cso = cso_create_context(p->pipe);
99
100 /* set clear color */
101 p->clear_color[0] = 0.3;
102 p->clear_color[1] = 0.1;
103 p->clear_color[2] = 0.3;
104 p->clear_color[3] = 1.0;
105
106 /* vertex buffer */
107 {
108 float vertices[4][2][4] = {
109 {
110 { 0.0f, -0.9f, 0.0f, 1.0f },
111 { 1.0f, 0.0f, 0.0f, 1.0f }
112 },
113 {
114 { -0.9f, 0.9f, 0.0f, 1.0f },
115 { 0.0f, 1.0f, 0.0f, 1.0f }
116 },
117 {
118 { 0.9f, 0.9f, 0.0f, 1.0f },
119 { 0.0f, 0.0f, 1.0f, 1.0f }
120 }
121 };
122
123 p->vbuf = pipe_buffer_create(p->screen, PIPE_BIND_VERTEX_BUFFER, sizeof(vertices));
124 pipe_buffer_write(p->pipe, p->vbuf, 0, sizeof(vertices), vertices);
125 }
126
127 /* render target texture */
128 {
129 struct pipe_resource tmplt;
130 memset(&tmplt, 0, sizeof(tmplt));
131 tmplt.target = PIPE_TEXTURE_2D;
132 tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM; /* All drivers support this */
133 tmplt.width0 = WIDTH;
134 tmplt.height0 = HEIGHT;
135 tmplt.depth0 = 1;
136 tmplt.array_size = 1;
137 tmplt.last_level = 0;
138 tmplt.bind = PIPE_BIND_RENDER_TARGET;
139
140 p->target = p->screen->resource_create(p->screen, &tmplt);
141 }
142
143 /* disabled blending/masking */
144 memset(&p->blend, 0, sizeof(p->blend));
145 p->blend.rt[0].colormask = PIPE_MASK_RGBA;
146
147 /* no-op depth/stencil/alpha */
148 memset(&p->depthstencil, 0, sizeof(p->depthstencil));
149
150 /* rasterizer */
151 memset(&p->rasterizer, 0, sizeof(p->rasterizer));
152 p->rasterizer.cull_face = PIPE_FACE_NONE;
153 p->rasterizer.gl_rasterization_rules = 1;
154
155 surf_tmpl.format = templat.format;
156 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
157 surf_tmpl.u.tex.level = 0;
158 surf_tmpl.u.tex.first_layer = 0;
159 surf_tmpl.u.tex.last_layer = 0;
160 /* drawing destination */
161 memset(&p->framebuffer, 0, sizeof(p->framebuffer));
162 p->framebuffer.width = WIDTH;
163 p->framebuffer.height = HEIGHT;
164 p->framebuffer.nr_cbufs = 1;
165 p->framebuffer.cbufs[0] = p->pipe->create_surface(p->pipe, p->target, &surf_tmpl);
166
167 /* viewport, depth isn't really needed */
168 {
169 float x = 0;
170 float y = 0;
171 float z = FAR;
172 float half_width = (float)WIDTH / 2.0f;
173 float half_height = (float)HEIGHT / 2.0f;
174 float half_depth = ((float)FAR - (float)NEAR) / 2.0f;
175 float scale, bias;
176
177 if (FLIP) {
178 scale = -1.0f;
179 bias = (float)HEIGHT;
180 } else {
181 scale = 1.0f;
182 bias = 0.0f;
183 }
184
185 p->viewport.scale[0] = half_width;
186 p->viewport.scale[1] = half_height * scale;
187 p->viewport.scale[2] = half_depth;
188 p->viewport.scale[3] = 1.0f;
189
190 p->viewport.translate[0] = half_width + x;
191 p->viewport.translate[1] = (half_height + y) * scale + bias;
192 p->viewport.translate[2] = half_depth + z;
193 p->viewport.translate[3] = 0.0f;
194 }
195
196 /* vertex elements state */
197 memset(p->velem, 0, sizeof(p->velem));
198 p->velem[0].src_offset = 0 * 4 * sizeof(float); /* offset 0, first element */
199 p->velem[0].instance_divisor = 0;
200 p->velem[0].vertex_buffer_index = 0;
201 p->velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
202
203 p->velem[1].src_offset = 1 * 4 * sizeof(float); /* offset 16, second element */
204 p->velem[1].instance_divisor = 0;
205 p->velem[1].vertex_buffer_index = 0;
206 p->velem[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
207
208 /* vertex shader */
209 {
210 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
211 TGSI_SEMANTIC_COLOR };
212 const uint semantic_indexes[] = { 0, 0 };
213 p->vs = util_make_vertex_passthrough_shader(p->pipe, 2, semantic_names, semantic_indexes);
214 }
215
216 /* fragment shader */
217 p->fs = util_make_fragment_passthrough_shader(p->pipe);
218 }
219
220 static void close_prog(struct program *p)
221 {
222 /* unset all state */
223 cso_release_all(p->cso);
224
225 p->pipe->delete_vs_state(p->pipe, p->vs);
226 p->pipe->delete_fs_state(p->pipe, p->fs);
227
228 pipe_surface_reference(&p->framebuffer.cbufs[0], NULL);
229 pipe_resource_reference(&p->target, NULL);
230 pipe_resource_reference(&p->vbuf, NULL);
231
232 cso_destroy_context(p->cso);
233 p->pipe->destroy(p->pipe);
234 p->screen->destroy(p->screen);
235
236 FREE(p);
237 }
238
239 static void draw(struct program *p)
240 {
241 /* set the render target */
242 cso_set_framebuffer(p->cso, &p->framebuffer);
243
244 /* clear the render target */
245 p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR, p->clear_color, 0, 0);
246
247 /* set misc state we care about */
248 cso_set_blend(p->cso, &p->blend);
249 cso_set_depth_stencil_alpha(p->cso, &p->depthstencil);
250 cso_set_rasterizer(p->cso, &p->rasterizer);
251 cso_set_viewport(p->cso, &p->viewport);
252
253 /* shaders */
254 cso_set_fragment_shader_handle(p->cso, p->fs);
255 cso_set_vertex_shader_handle(p->cso, p->vs);
256
257 /* vertex element data */
258 cso_set_vertex_elements(p->cso, 2, p->velem);
259
260 util_draw_vertex_buffer(p->pipe,
261 p->vbuf, 0,
262 PIPE_PRIM_TRIANGLES,
263 3, /* verts */
264 2); /* attribs/vert */
265
266 p->pipe->flush(p->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
267
268 debug_dump_surface_bmp(p->pipe, "result.bmp", p->framebuffer.cbufs[0]);
269 }
270
271 int main(int argc, char** argv)
272 {
273 struct program *p = CALLOC_STRUCT(program);
274
275 init_prog(p);
276 draw(p);
277 close_prog(p);
278
279 return 0;
280 }