1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2015 VMware, Inc.
3bf215546Sopenharmony_ci * All Rights Reserved.
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
13bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "main/mtypes.h"
25bf215546Sopenharmony_ci#include "main/buffers.h"
26bf215546Sopenharmony_ci#include "main/errors.h"
27bf215546Sopenharmony_ci#include "main/fbobject.h"
28bf215546Sopenharmony_ci#include "main/get.h"
29bf215546Sopenharmony_ci#include "main/teximage.h"
30bf215546Sopenharmony_ci#include "main/texparam.h"
31bf215546Sopenharmony_ci#include "st_copytex.h"
32bf215546Sopenharmony_ci#include "api_exec_decl.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci/**
36bf215546Sopenharmony_ci * Copy a colorbuffer from the window system framebuffer (a window or
37bf215546Sopenharmony_ci * pbuffer) to a texture.
38bf215546Sopenharmony_ci * This is a helper used by the wglBindTexImageARB() function.
39bf215546Sopenharmony_ci *
40bf215546Sopenharmony_ci * \param srcBuffer  source buffer (GL_FRONT_LEFT, GL_BACK_LEFT, etc)
41bf215546Sopenharmony_ci * \param fbWidth  width of the source framebuffer
42bf215546Sopenharmony_ci * \param fbHeight  height of the source framebuffer
43bf215546Sopenharmony_ci * \param texTarget  which texture target to copy to (GL_TEXTURE_1D/2D/CUBE_MAP)
44bf215546Sopenharmony_ci * \param texLevel  which texture mipmap level to copy to
45bf215546Sopenharmony_ci * \param cubeFace  which cube face to copy to (in [0,5])
46bf215546Sopenharmony_ci * \param texFormat  what texture format to use, if texture doesn't exist
47bf215546Sopenharmony_ci */
48bf215546Sopenharmony_civoid
49bf215546Sopenharmony_cist_copy_framebuffer_to_texture(GLenum srcBuffer,
50bf215546Sopenharmony_ci                               GLint fbWidth, GLint fbHeight,
51bf215546Sopenharmony_ci                               GLenum texTarget, GLint texLevel,
52bf215546Sopenharmony_ci                               GLuint cubeFace, GLenum texFormat)
53bf215546Sopenharmony_ci{
54bf215546Sopenharmony_ci   GLint readFBOSave, readBufSave, width, height;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   assert(cubeFace < 6);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   /* Save current FBO / readbuffer */
59bf215546Sopenharmony_ci   _mesa_GetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &readFBOSave);
60bf215546Sopenharmony_ci   _mesa_GetIntegerv(GL_READ_BUFFER, &readBufSave);
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   /* Read from the winsys buffer */
63bf215546Sopenharmony_ci   _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, 0);
64bf215546Sopenharmony_ci   _mesa_ReadBuffer(srcBuffer);
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   /* copy image from pbuffer to texture */
67bf215546Sopenharmony_ci   switch (texTarget) {
68bf215546Sopenharmony_ci   case GL_TEXTURE_1D:
69bf215546Sopenharmony_ci      _mesa_GetTexLevelParameteriv(GL_TEXTURE_1D, texLevel,
70bf215546Sopenharmony_ci                                   GL_TEXTURE_WIDTH, &width);
71bf215546Sopenharmony_ci      if (width == fbWidth) {
72bf215546Sopenharmony_ci         /* replace existing texture */
73bf215546Sopenharmony_ci         _mesa_CopyTexSubImage1D(GL_TEXTURE_1D,
74bf215546Sopenharmony_ci                                 texLevel,
75bf215546Sopenharmony_ci                                 0,    /* xoffset */
76bf215546Sopenharmony_ci                                 0, 0, /* x, y */
77bf215546Sopenharmony_ci                                 fbWidth);
78bf215546Sopenharmony_ci      } else {
79bf215546Sopenharmony_ci         /* define initial texture */
80bf215546Sopenharmony_ci         _mesa_CopyTexImage1D(GL_TEXTURE_1D,
81bf215546Sopenharmony_ci                              texLevel,
82bf215546Sopenharmony_ci                              texFormat,
83bf215546Sopenharmony_ci                              0, 0, /* x, y */
84bf215546Sopenharmony_ci                              fbWidth, 0);
85bf215546Sopenharmony_ci      }
86bf215546Sopenharmony_ci      break;
87bf215546Sopenharmony_ci   case GL_TEXTURE_2D:
88bf215546Sopenharmony_ci      _mesa_GetTexLevelParameteriv(GL_TEXTURE_2D, texLevel,
89bf215546Sopenharmony_ci                                   GL_TEXTURE_WIDTH, &width);
90bf215546Sopenharmony_ci      _mesa_GetTexLevelParameteriv(GL_TEXTURE_2D, texLevel,
91bf215546Sopenharmony_ci                                   GL_TEXTURE_HEIGHT, &height);
92bf215546Sopenharmony_ci      if (width == fbWidth && height == fbHeight) {
93bf215546Sopenharmony_ci         /* replace existing texture */
94bf215546Sopenharmony_ci         _mesa_CopyTexSubImage2D(GL_TEXTURE_2D,
95bf215546Sopenharmony_ci                                 texLevel,
96bf215546Sopenharmony_ci                                 0, 0, /* xoffset, yoffset */
97bf215546Sopenharmony_ci                                 0, 0, /* x, y */
98bf215546Sopenharmony_ci                                 fbWidth, fbHeight);
99bf215546Sopenharmony_ci      } else {
100bf215546Sopenharmony_ci         /* define initial texture */
101bf215546Sopenharmony_ci         _mesa_CopyTexImage2D(GL_TEXTURE_2D,
102bf215546Sopenharmony_ci                              texLevel,
103bf215546Sopenharmony_ci                              texFormat,
104bf215546Sopenharmony_ci                              0, 0, /* x, y */
105bf215546Sopenharmony_ci                              fbWidth, fbHeight, 0);
106bf215546Sopenharmony_ci      }
107bf215546Sopenharmony_ci      break;
108bf215546Sopenharmony_ci   case GL_TEXTURE_CUBE_MAP:
109bf215546Sopenharmony_ci      {
110bf215546Sopenharmony_ci         const GLenum target =
111bf215546Sopenharmony_ci            GL_TEXTURE_CUBE_MAP_POSITIVE_X + cubeFace;
112bf215546Sopenharmony_ci         _mesa_GetTexLevelParameteriv(target, texLevel,
113bf215546Sopenharmony_ci                                      GL_TEXTURE_WIDTH, &width);
114bf215546Sopenharmony_ci         _mesa_GetTexLevelParameteriv(target, texLevel,
115bf215546Sopenharmony_ci                                      GL_TEXTURE_HEIGHT, &height);
116bf215546Sopenharmony_ci         if (width == fbWidth && height == fbHeight) {
117bf215546Sopenharmony_ci            /* replace existing texture */
118bf215546Sopenharmony_ci            _mesa_CopyTexSubImage2D(target,
119bf215546Sopenharmony_ci                                    texLevel,
120bf215546Sopenharmony_ci                                    0, 0, /* xoffset, yoffset */
121bf215546Sopenharmony_ci                                    0, 0, /* x, y */
122bf215546Sopenharmony_ci                                    fbWidth, fbHeight);
123bf215546Sopenharmony_ci         } else {
124bf215546Sopenharmony_ci            /* define new texture */
125bf215546Sopenharmony_ci            _mesa_CopyTexImage2D(target,
126bf215546Sopenharmony_ci                                 texLevel,
127bf215546Sopenharmony_ci                                 texFormat,
128bf215546Sopenharmony_ci                                 0, 0, /* x, y */
129bf215546Sopenharmony_ci                                 fbWidth, fbHeight, 0);
130bf215546Sopenharmony_ci         }
131bf215546Sopenharmony_ci      }
132bf215546Sopenharmony_ci      break;
133bf215546Sopenharmony_ci   default:
134bf215546Sopenharmony_ci      _mesa_problem(NULL,
135bf215546Sopenharmony_ci                    "unexpected target in st_copy_framebuffer_to_texture()\n");
136bf215546Sopenharmony_ci   }
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   /* restore readbuffer */
139bf215546Sopenharmony_ci   _mesa_ReadBuffer(readBufSave);
140bf215546Sopenharmony_ci   _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, readFBOSave);
141bf215546Sopenharmony_ci}
142