~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/gallium/tests/graw/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
 
/* Display a cleared blue window.  This demo has no dependencies on
2
 
 * any utility code, just the graw interface and gallium.
3
 
 */
4
 
 
5
 
#include <stdio.h>
6
 
#include "graw_util.h"
7
 
 
8
 
static struct graw_info info;
9
 
 
10
 
static const int WIDTH = 300;
11
 
static const int HEIGHT = 300;
12
 
 
13
 
 
14
 
struct vertex {
15
 
   float position[4];
16
 
   float color[4];
17
 
};
18
 
 
19
 
static boolean FlatShade = FALSE;
20
 
 
21
 
 
22
 
static struct vertex vertices[3] =
23
 
{
24
 
   {
25
 
      { 0.0f, -0.9f, 0.0f, 1.0f },
26
 
      { 1.0f, 0.0f, 0.0f, 1.0f }
27
 
   },
28
 
   {
29
 
      { -0.9f, 0.9f, 0.0f, 1.0f },
30
 
      { 0.0f, 1.0f, 0.0f, 1.0f }
31
 
   },
32
 
   {
33
 
      { 0.9f, 0.9f, 0.0f, 1.0f },
34
 
      { 0.0f, 0.0f, 1.0f, 1.0f }
35
 
   }
36
 
};
37
 
 
38
 
 
39
 
static void set_vertices( void )
40
 
{
41
 
   struct pipe_vertex_element ve[2];
42
 
   struct pipe_vertex_buffer vbuf;
43
 
   void *handle;
44
 
 
45
 
   memset(ve, 0, sizeof ve);
46
 
 
47
 
   ve[0].src_offset = Offset(struct vertex, position);
48
 
   ve[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
49
 
   ve[1].src_offset = Offset(struct vertex, color);
50
 
   ve[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
51
 
 
52
 
   handle = info.ctx->create_vertex_elements_state(info.ctx, 2, ve);
53
 
   info.ctx->bind_vertex_elements_state(info.ctx, handle);
54
 
 
55
 
   memset(&vbuf, 0, sizeof vbuf);
56
 
 
57
 
   vbuf.stride = sizeof( struct vertex );
58
 
   vbuf.buffer_offset = 0;
59
 
   vbuf.buffer.resource = pipe_buffer_create_with_data(info.ctx,
60
 
                                              PIPE_BIND_VERTEX_BUFFER,
61
 
                                              PIPE_USAGE_DEFAULT,
62
 
                                              sizeof(vertices),
63
 
                                              vertices);
64
 
 
65
 
   info.ctx->set_vertex_buffers(info.ctx, 0, 1, 0, false, &vbuf);
66
 
}
67
 
 
68
 
 
69
 
static void set_vertex_shader( void )
70
 
{
71
 
   void *handle;
72
 
   const char *text =
73
 
      "VERT\n"
74
 
      "DCL IN[0]\n"
75
 
      "DCL IN[1]\n"
76
 
      "DCL OUT[0], POSITION\n"
77
 
      "DCL OUT[1], COLOR\n"
78
 
      "  0: MOV OUT[1], IN[1]\n"
79
 
      "  1: MOV OUT[0], IN[0]\n"
80
 
      "  2: END\n";
81
 
 
82
 
   handle = graw_parse_vertex_shader(info.ctx, text);
83
 
   info.ctx->bind_vs_state(info.ctx, handle);
84
 
}
85
 
 
86
 
 
87
 
static void set_fragment_shader( void )
88
 
{
89
 
   void *handle;
90
 
   const char *text =
91
 
      "FRAG\n"
92
 
      "DCL IN[0], COLOR, LINEAR\n"
93
 
      "DCL OUT[0], COLOR\n"
94
 
      "  0: MOV OUT[0], IN[0]\n"
95
 
      "  1: END\n";
96
 
 
97
 
   handle = graw_parse_fragment_shader(info.ctx, text);
98
 
   info.ctx->bind_fs_state(info.ctx, handle);
99
 
}
100
 
 
101
 
 
102
 
static void draw( void )
103
 
{
104
 
   union pipe_color_union clear_color = { {1,0,1,1} };
105
 
 
106
 
   info.ctx->clear(info.ctx, PIPE_CLEAR_COLOR, NULL, &clear_color, 0, 0);
107
 
   util_draw_arrays(info.ctx, PIPE_PRIM_TRIANGLES, 0, 3);
108
 
   info.ctx->flush(info.ctx, NULL, 0);
109
 
 
110
 
   graw_save_surface_to_file(info.ctx, info.color_surf[0], NULL);
111
 
 
112
 
   graw_util_flush_front(&info);
113
 
}
114
 
 
115
 
 
116
 
static void init( void )
117
 
{
118
 
   if (!graw_util_create_window(&info, WIDTH, HEIGHT, 1, FALSE))
119
 
      exit(1);
120
 
 
121
 
   graw_util_default_state(&info, FALSE);
122
 
 
123
 
   {
124
 
      struct pipe_rasterizer_state rasterizer;
125
 
      void *handle;
126
 
      memset(&rasterizer, 0, sizeof rasterizer);
127
 
      rasterizer.cull_face = PIPE_FACE_NONE;
128
 
      rasterizer.half_pixel_center = 1;
129
 
      rasterizer.bottom_edge_rule = 1;
130
 
      rasterizer.flatshade = FlatShade;
131
 
      rasterizer.depth_clip_near = 1;
132
 
      rasterizer.depth_clip_far = 1;
133
 
      handle = info.ctx->create_rasterizer_state(info.ctx, &rasterizer);
134
 
      info.ctx->bind_rasterizer_state(info.ctx, handle);
135
 
   }
136
 
 
137
 
 
138
 
   graw_util_viewport(&info, 0, 0, WIDTH, HEIGHT, 30, 1000);
139
 
 
140
 
   set_vertices();
141
 
   set_vertex_shader();
142
 
   set_fragment_shader();
143
 
}
144
 
 
145
 
static void args(int argc, char *argv[])
146
 
{
147
 
   int i;
148
 
 
149
 
   for (i = 1; i < argc; ) {
150
 
      if (graw_parse_args(&i, argc, argv)) {
151
 
         /* ok */
152
 
      }
153
 
      else if (strcmp(argv[i], "-f") == 0) {
154
 
         FlatShade = TRUE;
155
 
         i++;
156
 
      }
157
 
      else {
158
 
         printf("Invalid arg %s\n", argv[i]);
159
 
         exit(1);
160
 
      }
161
 
   }
162
 
}
163
 
 
164
 
int main( int argc, char *argv[] )
165
 
{
166
 
   args(argc, argv);
167
 
   init();
168
 
 
169
 
   graw_set_display_func( draw );
170
 
   graw_main_loop();
171
 
   return 0;
172
 
}