blob: 0b45086356043fccb023930bdbd538dbd7ce2fa4 [file] [log] [blame]
José Fonsecafc550c52012-11-18 11:57:56 +00001/**************************************************************************
2 *
3 * Copyright 2012 Jose Fonseca
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sub license,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * AUTHORS,
21 * AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 **************************************************************************/
27
28
29/*
30 * Auxiliary functions to compute the size of array/blob arguments.
31 */
32
33#ifndef _D3DCOMMONSIZE_HPP_
34#define _D3DCOMMONSIZE_HPP_
35
36
37/* We purposedly don't include any D3D header, so that this header can be used
38 * with all D3D versions. */
39
40#include <assert.h>
41
42#include "os.hpp"
43
44
45static inline size_t
46_vertexCount(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount)
47{
48 switch (PrimitiveType) {
49 case D3DPT_POINTLIST:
50 return PrimitiveCount;
51 case D3DPT_LINELIST:
52 return PrimitiveCount*2;
53 case D3DPT_LINESTRIP:
54 return PrimitiveCount + 1;
55 case D3DPT_TRIANGLELIST:
56 return PrimitiveCount * 3;
57 case D3DPT_TRIANGLESTRIP:
58 return PrimitiveCount + 2;
59 case D3DPT_TRIANGLEFAN:
José Fonseca2b36b812013-05-03 10:47:39 +010060 return PrimitiveCount + 2;
José Fonsecafc550c52012-11-18 11:57:56 +000061 default:
62 os::log("apitrace: warning: %s: unknown D3DPRIMITIVETYPE %u\n", __FUNCTION__, PrimitiveType);
63 return 0;
64 }
65}
66
67
68static inline size_t
69_vertexDataSize(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, UINT VertexStride) {
70 return _vertexCount(PrimitiveType, PrimitiveCount) * VertexStride;
71}
72
73
74static inline size_t
75_indexDataSize(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, D3DFORMAT IndexDataFormat) {
76 UINT IndexStride;
77 switch (IndexDataFormat) {
78 case D3DFMT_INDEX16:
79 IndexStride = 2;
80 break;
81 case D3DFMT_INDEX32:
82 IndexStride = 4;
83 break;
84 default:
85 os::log("apitrace: warning: %s: unexpected index D3DFORMAT %u\n", __FUNCTION__, IndexDataFormat);
86 return 0;
87 }
88 return _vertexCount(PrimitiveType, PrimitiveCount) * IndexStride;
89}
90
91
92/*
93 * Return the number of tokens for a given shader.
94 */
95static inline size_t
96_shaderSize(const DWORD *pFunction)
97{
98 DWORD dwLength = 0;
99
100 while (true) {
101 DWORD dwToken = pFunction[dwLength++];
102
103 switch (dwToken & D3DSI_OPCODE_MASK) {
104 case D3DSIO_COMMENT:
105 dwLength += (dwToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
106 break;
107
108 case D3DSIO_END:
109 if (dwToken != D3DSIO_END) {
110 os::log("apitrace: warning: %s: malformed END token\n", __FUNCTION__);
111 }
112 return dwLength * sizeof *pFunction;
113 }
114 }
115}
116
117
118static inline size_t
119_getLockSize(D3DFORMAT Format, UINT Width, UINT Height, INT RowPitch, UINT Depth = 1, INT SlicePitch = 0) {
120 if (Width == 0 || Height == 0 || Depth == 0) {
121 return 0;
122 }
123
124 if (RowPitch < 0) {
125 os::log("apitrace: warning: %s: negative row pitch %i\n", __FUNCTION__, RowPitch);
126 return 0;
127 }
128
129 if (SlicePitch < 0) {
130 os::log("apitrace: warning: %s: negative slice pitch %i\n", __FUNCTION__, SlicePitch);
131 return 0;
132 }
133
134 switch ((DWORD)Format) {
135 case D3DFMT_DXT1:
136 case D3DFMT_DXT2:
137 case D3DFMT_DXT3:
138 case D3DFMT_DXT4:
139 case D3DFMT_DXT5:
140 Width = (Width + 3) / 4;
141 Height = (Height + 3) / 4;
142 break;
143
144#if DIRECT3D_VERSION >= 0x900
145 case D3DFMT_ATI1N:
146 case D3DFMT_ATI2N:
147 /*
148 * Because these are unsupported formats, RowPitch is not set to the
149 * number of bytes between row of blocks, but instead in such way that
150 * Height * RowPitch will match the expected size.
151 */
152 break;
153#endif /* DIRECT3D_VERSION >= 0x900 */
154
155 case D3DFMT_UYVY:
156 case D3DFMT_YUY2:
157#if DIRECT3D_VERSION >= 0x900
158 case D3DFMT_R8G8_B8G8:
159 case D3DFMT_G8R8_G8B8:
160#endif /* DIRECT3D_VERSION >= 0x900 */
161 Width = (Width + 1) / 2;
162 break;
163
164#if DIRECT3D_VERSION >= 0x900
165 case D3DFMT_NV12:
José Fonsecad5422a42013-02-06 11:36:59 +0000166 case D3DFMT_YV12:
José Fonsecafc550c52012-11-18 11:57:56 +0000167 return (Height + ((Height + 1) / 2)) * RowPitch;
168
169 case D3DFMT_NULL:
170 return 0;
171#endif /* DIRECT3D_VERSION >= 0x900 */
172
173 default:
174 break;
175 }
176
177 (void)Width;
178
179 size_t size = Height * RowPitch;
180
181 if (Depth > 1) {
182 size += (Depth - 1) * SlicePitch;
183 }
184
185 return size;
186}
187
188
189#endif /* _D3DCOMMONSIZE_HPP_ */