~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/gallium/tests/trivial/tri.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

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