Merge branch 'mesa_7_5_branch'
[mesa.git] / progs / rbug / tex_dump.c
1 /*
2 * Copyright 2009 VMware, Inc.
3 * All Rights Reserved.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "pipe/p_compiler.h"
26 #include "pipe/p_format.h"
27 #include "pipe/p_state.h"
28 #include "util/u_memory.h"
29 #include "util/u_debug.h"
30 #include "util/u_network.h"
31 #include "util/u_tile.h"
32 #include "rbug/rbug.h"
33
34 static void dump(rbug_texture_t tex,
35 struct rbug_proto_texture_info_reply *info,
36 struct rbug_proto_texture_read_reply *read,
37 int mip)
38 {
39 enum pipe_format format = info->format;
40 uint8_t *data = read->data;
41 unsigned width = info->width[mip];
42 unsigned height = info->height[mip];
43 unsigned dst_stride = width * 4 * 4;
44 unsigned src_stride = read->stride;
45 float *rgba = MALLOC(dst_stride * height);
46 int i;
47 char filename[512];
48
49 util_snprintf(filename, 512, "%llu_%s_%u.bmp",
50 (unsigned long long)tex, pf_name(info->format), mip);
51
52 if (pf_is_compressed(info->format)) {
53 debug_printf("skipping: %s\n", filename);
54 return;
55 }
56
57 debug_printf("saving: %s\n", filename);
58
59 for (i = 0; i < height; i++) {
60 pipe_tile_raw_to_rgba(format, data + src_stride * i,
61 width, 1,
62 &rgba[width*4*i], dst_stride);
63 }
64
65 debug_dump_float_rgba_bmp(filename, width, height, rgba, width);
66
67 FREE(rgba);
68 }
69
70 static void talk()
71 {
72 int c = u_socket_connect("localhost", 13370);
73 struct rbug_connection *con = rbug_from_socket(c);
74 struct rbug_header *header;
75 struct rbug_header *header2;
76 struct rbug_proto_texture_list_reply *list;
77 struct rbug_proto_texture_info_reply *info;
78 struct rbug_proto_texture_read_reply *read;
79 int i, j;
80
81 assert(c >= 0);
82 assert(con);
83 debug_printf("Connection get!\n");
84
85 debug_printf("Sending get textures\n");
86 rbug_send_texture_list(con, NULL);
87
88 debug_printf("Waiting for textures\n");
89 header = rbug_get_message(con, NULL);
90 assert(header->opcode == RBUG_OP_TEXTURE_LIST_REPLY);
91 list = (struct rbug_proto_texture_list_reply *)header;
92
93 debug_printf("Got textures:\n");
94 for (i = 0; i < list->textures_len; i++) {
95 rbug_send_texture_info(con, list->textures[i], NULL);
96
97 header = rbug_get_message(con, NULL);
98 assert(header->opcode == RBUG_OP_TEXTURE_INFO_REPLY);
99 info = (struct rbug_proto_texture_info_reply *)header;
100
101 for (j = 0; j <= info->last_level; j++) {
102 rbug_send_texture_read(con, list->textures[i],
103 0, j, 0,
104 0, 0, info->width[j], info->height[j],
105 NULL);
106
107 header2 = rbug_get_message(con, NULL);
108 assert(header2->opcode == RBUG_OP_TEXTURE_READ_REPLY);
109 read = (struct rbug_proto_texture_read_reply *)header2;
110
111 dump(list->textures[i], info, read, j);
112
113 rbug_free_header(header2);
114 }
115
116 rbug_free_header(header);
117
118 }
119 rbug_free_header(&list->header);
120 rbug_disconnect(con);
121 }
122
123 int main(int argc, char** argv)
124 {
125 talk();
126 return 0;
127 }