xref: /aosp_15_r20/external/deqp/framework/opengl/gluPixelTransfer.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program OpenGL ES Utilities
3  * ------------------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief OpenGL ES Pixel Transfer Utilities.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "gluPixelTransfer.hpp"
25 #include "gluTextureUtil.hpp"
26 #include "gluRenderContext.hpp"
27 #include "glwEnums.hpp"
28 #include "glwFunctions.hpp"
29 #include "tcuTexture.hpp"
30 #include "tcuSurface.hpp"
31 #include "deMemory.h"
32 
33 namespace glu
34 {
35 
getTransferAlignment(tcu::TextureFormat format)36 static inline int getTransferAlignment(tcu::TextureFormat format)
37 {
38     int pixelSize = format.getPixelSize();
39     if (deIsPowerOfTwo32(pixelSize))
40         return de::min(pixelSize, 8);
41     else
42         return 1;
43 }
44 
45 /*--------------------------------------------------------------------*//*!
46  * \brief Read pixels to pixel buffer access.
47  * \note Stride must be default stride for format.
48  *//*--------------------------------------------------------------------*/
readPixels(const RenderContext & context,int x,int y,const tcu::PixelBufferAccess & dst)49 void readPixels(const RenderContext &context, int x, int y, const tcu::PixelBufferAccess &dst)
50 {
51     const glw::Functions &gl = context.getFunctions();
52 
53     TCU_CHECK_INTERNAL(dst.getDepth() == 1);
54     TCU_CHECK_INTERNAL(dst.getRowPitch() == dst.getFormat().getPixelSize() * dst.getWidth());
55 
56     int width             = dst.getWidth();
57     int height            = dst.getHeight();
58     TransferFormat format = getTransferFormat(dst.getFormat());
59 
60     gl.pixelStorei(GL_PACK_ALIGNMENT, getTransferAlignment(dst.getFormat()));
61     gl.readPixels(x, y, width, height, format.format, format.dataType, dst.getDataPtr());
62 }
63 
64 /*--------------------------------------------------------------------*//*!
65  * \brief Upload pixels from pixel buffer access.
66  * \note Stride must be default stride for format.
67  *//*--------------------------------------------------------------------*/
texImage2D(const RenderContext & context,uint32_t target,int level,uint32_t internalFormat,const tcu::ConstPixelBufferAccess & src)68 void texImage2D(const RenderContext &context, uint32_t target, int level, uint32_t internalFormat,
69                 const tcu::ConstPixelBufferAccess &src)
70 {
71     const glw::Functions &gl = context.getFunctions();
72 
73     TCU_CHECK_INTERNAL(src.getDepth() == 1);
74     TCU_CHECK_INTERNAL(src.getRowPitch() == src.getFormat().getPixelSize() * src.getWidth());
75 
76     int width             = src.getWidth();
77     int height            = src.getHeight();
78     TransferFormat format = getTransferFormat(src.getFormat());
79 
80     gl.pixelStorei(GL_UNPACK_ALIGNMENT, getTransferAlignment(src.getFormat()));
81     gl.texImage2D(target, level, internalFormat, width, height, 0, format.format, format.dataType, src.getDataPtr());
82 }
83 
84 /*--------------------------------------------------------------------*//*!
85  * \brief Upload pixels from pixel buffer access.
86  * \note Stride must be default stride for format.
87  *//*--------------------------------------------------------------------*/
texImage3D(const RenderContext & context,uint32_t target,int level,uint32_t internalFormat,const tcu::ConstPixelBufferAccess & src)88 void texImage3D(const RenderContext &context, uint32_t target, int level, uint32_t internalFormat,
89                 const tcu::ConstPixelBufferAccess &src)
90 {
91     const glw::Functions &gl = context.getFunctions();
92 
93     TCU_CHECK_INTERNAL(src.getRowPitch() == src.getFormat().getPixelSize() * src.getWidth());
94     TCU_CHECK_INTERNAL(src.getSlicePitch() == src.getRowPitch() * src.getHeight());
95 
96     int width             = src.getWidth();
97     int height            = src.getHeight();
98     int depth             = src.getDepth();
99     TransferFormat format = getTransferFormat(src.getFormat());
100 
101     gl.pixelStorei(GL_UNPACK_ALIGNMENT, getTransferAlignment(src.getFormat()));
102     gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format.format, format.dataType,
103                   src.getDataPtr());
104 }
105 
106 /*--------------------------------------------------------------------*//*!
107  * \brief Upload pixels from pixel buffer access.
108  * \note Stride must be default stride for format.
109  *//*--------------------------------------------------------------------*/
texSubImage2D(const RenderContext & context,uint32_t target,int level,int x,int y,const tcu::ConstPixelBufferAccess & src)110 void texSubImage2D(const RenderContext &context, uint32_t target, int level, int x, int y,
111                    const tcu::ConstPixelBufferAccess &src)
112 {
113     const glw::Functions &gl = context.getFunctions();
114 
115     TCU_CHECK_INTERNAL(src.getDepth() == 1);
116     TCU_CHECK_INTERNAL(src.getRowPitch() == src.getFormat().getPixelSize() * src.getWidth());
117 
118     int width             = src.getWidth();
119     int height            = src.getHeight();
120     TransferFormat format = getTransferFormat(src.getFormat());
121 
122     gl.pixelStorei(GL_UNPACK_ALIGNMENT, getTransferAlignment(src.getFormat()));
123     gl.texSubImage2D(target, level, x, y, width, height, format.format, format.dataType, src.getDataPtr());
124 }
125 
126 /*--------------------------------------------------------------------*//*!
127  * \brief Upload pixels from pixel buffer access.
128  * \note Stride must be default stride for format.
129  *//*--------------------------------------------------------------------*/
texSubImage3D(const RenderContext & context,uint32_t target,int level,int x,int y,int z,const tcu::ConstPixelBufferAccess & src)130 void texSubImage3D(const RenderContext &context, uint32_t target, int level, int x, int y, int z,
131                    const tcu::ConstPixelBufferAccess &src)
132 {
133     const glw::Functions &gl = context.getFunctions();
134 
135     TCU_CHECK_INTERNAL(src.getRowPitch() == src.getFormat().getPixelSize() * src.getWidth());
136     TCU_CHECK_INTERNAL(src.getSlicePitch() == src.getRowPitch() * src.getHeight());
137 
138     int width             = src.getWidth();
139     int height            = src.getHeight();
140     int depth             = src.getDepth();
141     TransferFormat format = getTransferFormat(src.getFormat());
142 
143     gl.pixelStorei(GL_UNPACK_ALIGNMENT, getTransferAlignment(src.getFormat()));
144     gl.texSubImage3D(target, level, x, y, z, width, height, depth, format.format, format.dataType, src.getDataPtr());
145 }
146 
147 } // namespace glu
148