graw_util.c revision 848b8605
1
2#include "pipe/p_compiler.h"
3#include "pipe/p_context.h"
4#include "pipe/p_shader_tokens.h"
5#include "pipe/p_state.h"
6#include "tgsi/tgsi_text.h"
7#include "util/u_debug.h"
8#include "util/u_memory.h"
9#include "state_tracker/graw.h"
10
11
12/* Helper functions.  These are the same for all graw implementations.
13 */
14PUBLIC void *
15graw_parse_geometry_shader(struct pipe_context *pipe,
16                           const char *text)
17{
18   struct tgsi_token tokens[1024];
19   struct pipe_shader_state state;
20
21   if (!tgsi_text_translate(text, tokens, Elements(tokens)))
22      return NULL;
23
24   memset(&state, 0, sizeof state);
25   state.tokens = tokens;
26   return pipe->create_gs_state(pipe, &state);
27}
28
29PUBLIC void *
30graw_parse_vertex_shader(struct pipe_context *pipe,
31                         const char *text)
32{
33   struct tgsi_token tokens[1024];
34   struct pipe_shader_state state;
35
36   if (!tgsi_text_translate(text, tokens, Elements(tokens)))
37      return NULL;
38
39   memset(&state, 0, sizeof state);
40   state.tokens = tokens;
41   return pipe->create_vs_state(pipe, &state);
42}
43
44PUBLIC void *
45graw_parse_fragment_shader(struct pipe_context *pipe,
46                           const char *text)
47{
48   struct tgsi_token tokens[1024];
49   struct pipe_shader_state state;
50
51   if (!tgsi_text_translate(text, tokens, Elements(tokens)))
52      return NULL;
53
54   memset(&state, 0, sizeof state);
55   state.tokens = tokens;
56   return pipe->create_fs_state(pipe, &state);
57}
58
59static char out_filename[256] = "";
60
61PUBLIC boolean
62graw_parse_args(int *argi,
63                int argc,
64                char *argv[])
65{
66   if (strcmp(argv[*argi], "-o") == 0) {
67      if (*argi + 1 >= argc) {
68         return FALSE;
69      }
70
71      strncpy(out_filename, argv[*argi + 1], sizeof(out_filename) - 1);
72      out_filename[sizeof(out_filename) - 1] = '\0';
73      *argi += 2;
74      return TRUE;
75   }
76
77   return FALSE;
78}
79
80PUBLIC boolean
81graw_save_surface_to_file(struct pipe_context *pipe,
82                          struct pipe_surface *surface,
83                          const char *filename)
84{
85   if (!filename || !*filename) {
86      filename = out_filename;
87      if (!filename || !*filename) {
88         return FALSE;
89      }
90   }
91
92   /* XXX: Make that working in release builds.
93    */
94   debug_dump_surface_bmp(pipe, filename, surface);
95   return TRUE;
96}
97