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