fbobind.c revision 32001f49
1/*
2 * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * VMWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22/**
23 * Measure rate of binding/switching between FBO targets.
24 * Create two framebuffer objects for rendering to two textures.
25 * Ping pong between texturing from one and drawing into the other.
26 *
27 * Brian Paul
28 * 22 Sep 2009
29 */
30
31#include <string.h>
32#include "glmain.h"
33#include "common.h"
34
35int WinWidth = 100, WinHeight = 100;
36
37static GLuint VBO;
38
39static GLuint FBO[2], Tex[2];
40
41static const GLsizei TexSize = 512;
42
43static const GLboolean DrawPoint = GL_TRUE;
44
45struct vertex
46{
47   GLfloat x, y, s, t;
48};
49
50static const struct vertex vertices[1] = {
51   { 0.0, 0.0, 0.5, 0.5 },
52};
53
54#define VOFFSET(F) ((void *) offsetof(struct vertex, F))
55
56
57/** Called from test harness/main */
58void
59PerfInit(void)
60{
61   const GLenum filter = GL_LINEAR;
62   GLenum stat;
63   int i;
64
65   if (!PerfExtensionSupported("GL_EXT_framebuffer_object")) {
66      perf_printf("fboswitch: GL_EXT_framebuffer_object not supported\n");
67      exit(0);
68   }
69
70   /* setup VBO */
71   glGenBuffersARB(1, &VBO);
72   glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBO);
73   glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vertices),
74                   vertices, GL_STATIC_DRAW_ARB);
75
76   glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), VOFFSET(x));
77   glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), VOFFSET(s));
78   glEnableClientState(GL_VERTEX_ARRAY);
79   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
80
81   glGenFramebuffersEXT(2, FBO);
82   glGenTextures(2, Tex);
83
84   for (i = 0; i < 2; i++) {
85      /* setup texture */
86      glBindTexture(GL_TEXTURE_2D, Tex[i]);
87      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
88                   TexSize, TexSize, 0,
89                   GL_RGBA, GL_UNSIGNED_BYTE, NULL);
90      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
91      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
92
93
94      /* setup fbo */
95      glBindFramebufferEXT(GL_FRAMEBUFFER, FBO[i]);
96      glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
97                                GL_COLOR_ATTACHMENT0_EXT,
98                                GL_TEXTURE_2D, Tex[i], 0/*level*/);
99      stat = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
100      if (stat != GL_FRAMEBUFFER_COMPLETE_EXT) {
101         perf_printf("fboswitch: Error: incomplete FBO!\n");
102         exit(1);
103      }
104
105      /* clear the FBO */
106      glClear(GL_COLOR_BUFFER_BIT);
107   }
108
109   glEnable(GL_TEXTURE_2D);
110}
111
112
113static void
114FBOBind(unsigned count)
115{
116   unsigned i;
117   for (i = 1; i < count; i++) {
118      const GLuint dst = i & 1;
119      const GLuint src = 1 - dst;
120
121      /* bind src texture */
122      glBindTexture(GL_TEXTURE_2D, Tex[src]);
123
124      /* bind dst fbo */
125      glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO[dst]);
126      glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
127
128      /* draw something */
129      if (DrawPoint)
130         glDrawArrays(GL_POINTS, 0, 1);
131   }
132   glFinish();
133}
134
135
136/** Called from test harness/main */
137void
138PerfNextRound(void)
139{
140}
141
142
143/** Called from test harness/main */
144void
145PerfDraw(void)
146{
147   double rate;
148
149   rate = PerfMeasureRate(FBOBind);
150   perf_printf("  FBO Binding: %1.f binds/sec\n", rate);
151
152   exit(0);
153}
154