summaryrefslogtreecommitdiffstats
path: root/tools/guest-diag/gl_frames.c
blob: d442d66fb094f8f491474cde77eb0f70490ca558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// SPDX-License-Identifier: BSD-3-Clause
/*
 * gl_frames.c - render animated frames with direct GL (GLX pbuffer) on the
 * guest's NVIDIA vGPU and publish them into the ivshmem (kvmfr) BAR so the
 * host can see what the GL client actually rendered.
 *
 * This is the GL sibling of tools/cuda-ab/frame_producer.cu: the transport
 * (guest GPU -> guest sysmem -> ivshmem -> host frame_reader.py) is identical,
 * but the pixels come from the direct-GL client channel instead of CUDA.
 * The pbuffer avoids the X window draw path that wedges the X server.
 *
 * Layout at BAR offset 0x100000 (same as frame_producer.cu):
 *   struct frame_hdr { magic, version, width, height, stride, format, seq }
 *   then width*height*4 bytes of RGBA8.
 *
 * Usage: sudo ./gl_frames [frames] [width] [height] [fps]
 * Prints: FRAME seq=<n> ms=<render+readback ms>
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <glob.h>
#include <math.h>
#include <X11/Xlib.h>
#include <GL/glx.h>
#include <GL/gl.h>

#define FRAME_OFFSET  0x100000UL
#define HDR_MAGIC     0x4b564d46u	/* "FMVK" little-endian-ish */
#define BAR_BYTES     (128UL << 20)
#define BAR_PATH_DEF  "/sys/bus/pci/devices/0000:00:04.0/resource2"

struct frame_hdr {
	uint32_t magic;
	uint32_t version;
	uint32_t width;
	uint32_t height;
	uint32_t stride;
	uint32_t format;	/* 0 = RGBA8 */
	uint32_t seq;
	uint32_t reserved;
	uint64_t timestamp_ns;
};

/*
 * The ivshmem kvmfr device moves with the QEMU topology (it was 00:03.0 in an
 * older launch, 00:04.0 in the current one).  Discover the 128 MiB BAR instead
 * of hardcoding a slot; GL_FRAMES_BAR overrides for debugging.
 */
static const char *find_bar(char *buf, size_t len)
{
	glob_t g;
	size_t i;
	const char *hit = NULL;

	if (glob("/sys/bus/pci/devices/*/resource2", 0, NULL, &g) != 0)
		return NULL;
	for (i = 0; i < g.gl_pathc; i++) {
		struct stat st;

		if (stat(g.gl_pathv[i], &st) == 0 &&
		    (unsigned long long)st.st_size == BAR_BYTES) {
			snprintf(buf, len, "%s", g.gl_pathv[i]);
			hit = buf;
			break;
		}
	}
	globfree(&g);
	return hit;
}

static uint64_t now_ns(void)
{
	struct timespec ts;

	clock_gettime(CLOCK_MONOTONIC, &ts);
	return (uint64_t)ts.tv_sec * 1000000000ull + ts.tv_nsec;
}

static void draw_frame(int f, int w, int h)
{
	float a = (float)f * 0.15f;
	int i;

	glViewport(0, 0, w, h);
	glClearColor(0.05f, 0.05f, 0.10f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	for (i = 0; i < 8; i++) {
		float t = a + (float)i * 0.78539816339f;

		glBegin(GL_TRIANGLES);
		glColor3f(1.0f, 0.2f + 0.09f * i, 0.1f);
		glVertex2f(0.0f, 0.0f);
		glColor3f(0.1f, 0.9f, 0.3f);
		glVertex2f(0.75f * cosf(t), 0.75f * sinf(t));
		glColor3f(0.2f, 0.3f, 1.0f);
		glVertex2f(0.55f * cosf(t + 0.5f),
			   0.55f * sinf(t + 0.5f));
		glEnd();
	}
	glFinish();
}

int main(int argc, char **argv)
{
	int frames = argc > 1 ? atoi(argv[1]) : 30;
	int w = argc > 2 ? atoi(argv[2]) : 640;
	int h = argc > 3 ? atoi(argv[3]) : 480;
	int fps = argc > 4 ? atoi(argv[4]) : 5;
	static int attribs[] = {
		GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
		GLX_RENDER_TYPE, GLX_RGBA_BIT,
		GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8,
		GLX_ALPHA_SIZE, 8,
		None
	};
	GLXFBConfig *fb;
	GLXPbuffer pb;
	XVisualInfo *vi;
	GLXContext ctx;
	Display *d;
	int scr, n = 0, fd, i;
	unsigned char *bar, *px;
	struct frame_hdr *hdr;
	volatile uint32_t *seqp;

	char found[256];
	const char *bar_path = getenv("GL_FRAMES_BAR");

	setvbuf(stdout, NULL, _IONBF, 0);
	if (!bar_path)
		bar_path = find_bar(found, sizeof(found));
	if (!bar_path)
		bar_path = BAR_PATH_DEF;
	printf("BAR %s\n", bar_path);
	fd = open(bar_path, O_RDWR | O_SYNC);
	if (fd < 0) {
		printf("OPEN_FAIL %s\n", bar_path);
		return 1;
	}
	bar = (unsigned char *)mmap(NULL, BAR_BYTES, PROT_READ | PROT_WRITE,
				    MAP_SHARED, fd, 0);
	if (bar == MAP_FAILED) {
		printf("MMAP_FAIL\n");
		return 1;
	}
	hdr = (struct frame_hdr *)(bar + FRAME_OFFSET);
	seqp = &hdr->seq;

	d = XOpenDisplay(NULL);
	if (!d) {
		printf("NODISPLAY\n");
		return 2;
	}
	scr = DefaultScreen(d);
	fb = glXChooseFBConfig(d, scr, attribs, &n);
	if (!fb || n < 1) {
		printf("NOFBCONFIG\n");
		return 3;
	}
	pb = glXCreatePbuffer(d, fb[0], (int[]){
		GLX_PBUFFER_WIDTH, w, GLX_PBUFFER_HEIGHT, h, None });
	if (!pb) {
		printf("NOPBUFFER\n");
		return 4;
	}
	vi = glXGetVisualFromFBConfig(d, fb[0]);
	ctx = glXCreateContext(d, vi, NULL, True);
	if (!ctx) {
		printf("NOCTX\n");
		return 5;
	}
	if (!glXMakeCurrent(d, pb, ctx)) {
		printf("MAKECURRENT_FAIL\n");
		return 6;
	}
	printf("GL %s | %s\n", (const char *)glGetString(GL_VENDOR),
	       (const char *)glGetString(GL_RENDERER));

	px = (unsigned char *)malloc((size_t)w * h * 4);
	if (!px) {
		printf("HOST_ALLOC_FAIL\n");
		return 7;
	}

	hdr->magic = HDR_MAGIC;
	hdr->version = 1;
	hdr->width = (uint32_t)w;
	hdr->height = (uint32_t)h;
	hdr->stride = (uint32_t)w * 4;
	hdr->format = 0;

	for (i = 0; i < frames; i++) {
		uint64_t t0 = now_ns();
		uint32_t seq;

		draw_frame(i, w, h);
		glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, px);
		if (glGetError() != GL_NO_ERROR)
			printf("GL_ERROR frame=%d\n", i);
		memcpy(bar + FRAME_OFFSET + sizeof(struct frame_hdr), px,
		       (size_t)w * h * 4);
		hdr->timestamp_ns = now_ns();
		__sync_synchronize();
		seq = (uint32_t)(i + 1);
		*seqp = seq;
		__sync_synchronize();
		printf("FRAME seq=%u ms=%.1f px0=%02x%02x%02x%02x\n", seq,
		       (double)(now_ns() - t0) / 1e6,
		       px[0], px[1], px[2], px[3]);
		if (fps > 0)
			usleep((useconds_t)(1000000 / fps));
	}
	printf("FRAME_DONE frames=%d %dx%d\n", frames, w, h);

	glXMakeCurrent(d, None, NULL);
	glXDestroyContext(d, ctx);
	glXDestroyPbuffer(d, pb);
	XCloseDisplay(d);
	free(px);
	munmap(bar, BAR_BYTES);
	close(fd);
	return 0;
}