blob: a1d2a3439bf6284e7eb122e1c39adc3430d4f104 [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
Jose Fonseca9653f952015-05-19 16:32:43 +010033#pragma once
José Fonsecafc550c52012-11-18 11:57:56 +000034
35
36/* We purposedly don't include any D3D header, so that this header can be used
37 * with all D3D versions. */
38
39#include <assert.h>
40
Jose Fonseca595728c2016-08-26 16:06:24 +010041#include <algorithm>
42
José Fonsecafc550c52012-11-18 11:57:56 +000043#include "os.hpp"
44
45
46static inline size_t
47_vertexCount(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount)
48{
49 switch (PrimitiveType) {
50 case D3DPT_POINTLIST:
51 return PrimitiveCount;
52 case D3DPT_LINELIST:
53 return PrimitiveCount*2;
54 case D3DPT_LINESTRIP:
55 return PrimitiveCount + 1;
56 case D3DPT_TRIANGLELIST:
57 return PrimitiveCount * 3;
58 case D3DPT_TRIANGLESTRIP:
59 return PrimitiveCount + 2;
60 case D3DPT_TRIANGLEFAN:
José Fonseca2b36b812013-05-03 10:47:39 +010061 return PrimitiveCount + 2;
José Fonsecafc550c52012-11-18 11:57:56 +000062 default:
63 os::log("apitrace: warning: %s: unknown D3DPRIMITIVETYPE %u\n", __FUNCTION__, PrimitiveType);
64 return 0;
65 }
66}
67
68
69static inline size_t
70_vertexDataSize(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, UINT VertexStride) {
71 return _vertexCount(PrimitiveType, PrimitiveCount) * VertexStride;
72}
73
74
75static inline size_t
76_indexDataSize(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, D3DFORMAT IndexDataFormat) {
77 UINT IndexStride;
78 switch (IndexDataFormat) {
79 case D3DFMT_INDEX16:
80 IndexStride = 2;
81 break;
82 case D3DFMT_INDEX32:
83 IndexStride = 4;
84 break;
85 default:
86 os::log("apitrace: warning: %s: unexpected index D3DFORMAT %u\n", __FUNCTION__, IndexDataFormat);
87 return 0;
88 }
89 return _vertexCount(PrimitiveType, PrimitiveCount) * IndexStride;
90}
91
92
93/*
94 * Return the number of tokens for a given shader.
95 */
96static inline size_t
97_shaderSize(const DWORD *pFunction)
98{
99 DWORD dwLength = 0;
100
101 while (true) {
102 DWORD dwToken = pFunction[dwLength++];
103
104 switch (dwToken & D3DSI_OPCODE_MASK) {
105 case D3DSIO_COMMENT:
106 dwLength += (dwToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
107 break;
108
109 case D3DSIO_END:
José Fonsecaaacd5672013-06-26 21:13:13 +0100110 if (dwToken == D3DSIO_END) {
111 return dwLength * sizeof *pFunction;
José Fonsecafc550c52012-11-18 11:57:56 +0000112 }
José Fonsecaaacd5672013-06-26 21:13:13 +0100113 break;
José Fonsecafc550c52012-11-18 11:57:56 +0000114 }
115 }
116}
117
118
Jose Fonseca127cc412015-07-06 12:33:41 +0100119static inline void
120_getFormatSize(D3DFORMAT Format, size_t & BlockSize, UINT & BlockWidth, UINT & BlockHeight);
121
Jose Fonseca614c5da2016-02-19 12:32:45 +0000122#define PACKED_PITCH 0x7ffffff
123
José Fonsecafc550c52012-11-18 11:57:56 +0000124static inline size_t
Jose Fonseca614c5da2016-02-19 12:32:45 +0000125_getLockSize(D3DFORMAT Format, bool Partial, UINT Width, UINT Height, INT RowPitch = PACKED_PITCH, UINT Depth = 1, INT SlicePitch = 0) {
José Fonsecafc550c52012-11-18 11:57:56 +0000126 if (Width == 0 || Height == 0 || Depth == 0) {
127 return 0;
128 }
129
130 if (RowPitch < 0) {
131 os::log("apitrace: warning: %s: negative row pitch %i\n", __FUNCTION__, RowPitch);
132 return 0;
133 }
134
Jose Fonseca127cc412015-07-06 12:33:41 +0100135 size_t size;
136 if (Format == MAKEFOURCC('N','V','1','2') ||
137 Format == MAKEFOURCC('Y','V','1','2')) {
138 // Planar YUV
Jose Fonseca614c5da2016-02-19 12:32:45 +0000139
140 if (RowPitch == PACKED_PITCH) {
141 RowPitch = Width;
142 }
143
Jose Fonseca127cc412015-07-06 12:33:41 +0100144 size = (Height + (Height + 1)/2) * RowPitch;
Jose Fonseca595728c2016-08-26 16:06:24 +0100145 } else if (Format == MAKEFOURCC('A','T','I','1')) {
146 // 64 bits per 4x4 block, but limited to height*pitch
147
148 if (RowPitch == PACKED_PITCH) {
149 RowPitch = Width;
150 }
151
152 size = std::min(Height * RowPitch, ((Height + 3)/4) * ((Width + 3)/4) * (64 / 8));
153 } else if (Format == MAKEFOURCC('A','T','I','2')) {
154 // 128 bits per 4x4 block, but limited to height*pitch
155
156 if (RowPitch == PACKED_PITCH) {
157 RowPitch = Width;
158 }
159
160 size = std::min(Height * RowPitch, ((Height + 3)/4) * ((Width + 3)/4) * (128 / 8));
Jose Fonseca127cc412015-07-06 12:33:41 +0100161 } else {
Jose Fonseca2ec73fa2015-07-13 15:34:04 +0100162 size_t BlockSize;
163 UINT BlockWidth;
164 UINT BlockHeight;
165 _getFormatSize(Format, BlockSize, BlockWidth, BlockHeight);
166 assert(BlockHeight);
167 Height = (Height + BlockHeight - 1) / BlockHeight;
168
Jose Fonseca614c5da2016-02-19 12:32:45 +0000169 if (RowPitch == PACKED_PITCH) {
170 RowPitch = ((Width + BlockWidth - 1) / BlockWidth * BlockSize + 7) / 8;
171 }
172
Jose Fonseca127cc412015-07-06 12:33:41 +0100173 size = Height * RowPitch;
174
175 if (Partial || Height == 1) {
176 // Must take pixel size in consideration
Jose Fonseca2ec73fa2015-07-13 15:34:04 +0100177 if (BlockWidth) {
Jose Fonseca614c5da2016-02-19 12:32:45 +0000178 Width = (Width + BlockWidth - 1) / BlockWidth;
179 size = (Width * BlockSize + 7) / 8;
Jose Fonseca127cc412015-07-06 12:33:41 +0100180 if (Height > 1) {
181 size += (Height - 1) * RowPitch;
182 }
183 }
184 }
José Fonsecafc550c52012-11-18 11:57:56 +0000185 }
186
José Fonsecafc550c52012-11-18 11:57:56 +0000187 if (Depth > 1) {
Jose Fonseca127cc412015-07-06 12:33:41 +0100188 if (SlicePitch < 0) {
189 os::log("apitrace: warning: %s: negative slice pitch %i\n", __FUNCTION__, SlicePitch);
190 return 0;
191 }
192
José Fonsecafc550c52012-11-18 11:57:56 +0000193 size += (Depth - 1) * SlicePitch;
194 }
195
196 return size;
197}
198
199