beer, cl_stub: Add OpenCL context creation to client code and stub list.
[libale.git] / client / beer.c
blobe66a7317aba172382ebc46a3a18bfa7039b2fc1e
1 /*
2 * Copyright 2008 David Hilvert <dhilvert@gmail.com>
4 * This file is part of libale.
6 * libale is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU Affero General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
9 * any later version.
11 * libale is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
14 * more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with libale. If not, see <http://www.gnu.org/licenses/>.
21 * beer: a simple client program for libale.
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <assert.h>
29 #include <ale.h>
31 const char *ale_translate_glsl(const char *);
33 static void beer_info_printf(const char *c, ...) {
34 va_list ap;
36 fprintf(stderr, "beer: ");
38 va_start(ap, c);
39 vfprintf(stderr, c, ap);
40 va_end(ap);
42 fprintf(stderr, "\n");
45 static void beer_error_printf(const char *c, ...) {
46 va_list ap;
48 fprintf(stderr, "beer: ");
50 va_start(ap, c);
51 vfprintf(stderr, c, ap);
52 va_end(ap);
54 fprintf(stderr, "\n");
55 fprintf(stderr, "beer: Exiting.\n");
56 exit(1);
59 int main(int argc, const char *argv[]) {
62 * Obtain a context.
65 ale_context ac;
67 cl_context cc = clCreateContextFromType(0, CL_DEVICE_TYPE_GPU, NULL, NULL, NULL);
69 if (cc == ((cl_context) 0)) {
70 beer_error_printf("Failed to create an OpenCL context.");
73 ac = ale_new_context(cc);
75 if (ac == NULL) {
76 beer_error_printf("Failed to create an ALE context.");
80 * Create a frame from a trivial image array.
83 unsigned char domain_data[27] =
84 { 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 1, 1, 1, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0 };
88 float *domain_data2;
90 ale_image ai = ale_new_image(ac, ALE_IMAGE_RGB, ALE_TYPE_UINT_8);
92 ale_image_set_host_static(ai, 3, 3, domain_data, NULL, NULL);
94 ale_render ar = ale_new_render_incremental(ac, ALE_TYPE_FLOAT_32, ALE_INVARIANT_MEAN);
96 ale_render_resize(ar, 0, 0, 3, 3);
98 ale_render_merge(ar, ai);
100 ale_image ari = ale_render_data(ar);
102 domain_data2 = ale_return_host(ari);
104 int di;
106 printf("Input data:");
107 for (di = 0; di < 27; di++) {
108 unsigned int i = domain_data[di];
109 printf(" %u", i);
111 printf("\n");
113 printf("Output data:");
114 for (di = 0; di < 27; di++) {
115 float f = domain_data2[di];
116 printf(" %f", f);
118 printf("\n");
120 ale_return_release_host(domain_data2);
121 ale_image_release(ai);
122 ale_image_release(ari);
123 ale_render_release(ar);
124 ale_context_release(ac);
126 return 0;