José Fonseca | fc550c5 | 2012-11-18 11:57:56 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 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 Fonseca | 9653f95 | 2015-05-19 16:32:43 +0100 | [diff] [blame] | 33 | #pragma once |
José Fonseca | fc550c5 | 2012-11-18 11:57:56 +0000 | [diff] [blame] | 34 | |
| 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 | |
| 41 | #include "os.hpp" |
| 42 | |
| 43 | |
| 44 | static inline size_t |
| 45 | _vertexCount(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount) |
| 46 | { |
| 47 | switch (PrimitiveType) { |
| 48 | case D3DPT_POINTLIST: |
| 49 | return PrimitiveCount; |
| 50 | case D3DPT_LINELIST: |
| 51 | return PrimitiveCount*2; |
| 52 | case D3DPT_LINESTRIP: |
| 53 | return PrimitiveCount + 1; |
| 54 | case D3DPT_TRIANGLELIST: |
| 55 | return PrimitiveCount * 3; |
| 56 | case D3DPT_TRIANGLESTRIP: |
| 57 | return PrimitiveCount + 2; |
| 58 | case D3DPT_TRIANGLEFAN: |
José Fonseca | 2b36b81 | 2013-05-03 10:47:39 +0100 | [diff] [blame] | 59 | return PrimitiveCount + 2; |
José Fonseca | fc550c5 | 2012-11-18 11:57:56 +0000 | [diff] [blame] | 60 | default: |
| 61 | os::log("apitrace: warning: %s: unknown D3DPRIMITIVETYPE %u\n", __FUNCTION__, PrimitiveType); |
| 62 | return 0; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | static inline size_t |
| 68 | _vertexDataSize(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, UINT VertexStride) { |
| 69 | return _vertexCount(PrimitiveType, PrimitiveCount) * VertexStride; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static inline size_t |
| 74 | _indexDataSize(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, D3DFORMAT IndexDataFormat) { |
| 75 | UINT IndexStride; |
| 76 | switch (IndexDataFormat) { |
| 77 | case D3DFMT_INDEX16: |
| 78 | IndexStride = 2; |
| 79 | break; |
| 80 | case D3DFMT_INDEX32: |
| 81 | IndexStride = 4; |
| 82 | break; |
| 83 | default: |
| 84 | os::log("apitrace: warning: %s: unexpected index D3DFORMAT %u\n", __FUNCTION__, IndexDataFormat); |
| 85 | return 0; |
| 86 | } |
| 87 | return _vertexCount(PrimitiveType, PrimitiveCount) * IndexStride; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | * Return the number of tokens for a given shader. |
| 93 | */ |
| 94 | static inline size_t |
| 95 | _shaderSize(const DWORD *pFunction) |
| 96 | { |
| 97 | DWORD dwLength = 0; |
| 98 | |
| 99 | while (true) { |
| 100 | DWORD dwToken = pFunction[dwLength++]; |
| 101 | |
| 102 | switch (dwToken & D3DSI_OPCODE_MASK) { |
| 103 | case D3DSIO_COMMENT: |
| 104 | dwLength += (dwToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT; |
| 105 | break; |
| 106 | |
| 107 | case D3DSIO_END: |
José Fonseca | aacd567 | 2013-06-26 21:13:13 +0100 | [diff] [blame] | 108 | if (dwToken == D3DSIO_END) { |
| 109 | return dwLength * sizeof *pFunction; |
José Fonseca | fc550c5 | 2012-11-18 11:57:56 +0000 | [diff] [blame] | 110 | } |
José Fonseca | aacd567 | 2013-06-26 21:13:13 +0100 | [diff] [blame] | 111 | break; |
José Fonseca | fc550c5 | 2012-11-18 11:57:56 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
Jose Fonseca | 127cc41 | 2015-07-06 12:33:41 +0100 | [diff] [blame] | 117 | static inline void |
| 118 | _getFormatSize(D3DFORMAT Format, size_t & BlockSize, UINT & BlockWidth, UINT & BlockHeight); |
| 119 | |
José Fonseca | fc550c5 | 2012-11-18 11:57:56 +0000 | [diff] [blame] | 120 | static inline size_t |
Jose Fonseca | 127cc41 | 2015-07-06 12:33:41 +0100 | [diff] [blame] | 121 | _getLockSize(D3DFORMAT Format, bool Partial, UINT Width, UINT Height, INT RowPitch, UINT Depth = 1, INT SlicePitch = 0) { |
José Fonseca | fc550c5 | 2012-11-18 11:57:56 +0000 | [diff] [blame] | 122 | if (Width == 0 || Height == 0 || Depth == 0) { |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | if (RowPitch < 0) { |
| 127 | os::log("apitrace: warning: %s: negative row pitch %i\n", __FUNCTION__, RowPitch); |
| 128 | return 0; |
| 129 | } |
| 130 | |
Jose Fonseca | 127cc41 | 2015-07-06 12:33:41 +0100 | [diff] [blame] | 131 | size_t size; |
| 132 | if (Format == MAKEFOURCC('N','V','1','2') || |
| 133 | Format == MAKEFOURCC('Y','V','1','2')) { |
| 134 | // Planar YUV |
| 135 | size = (Height + (Height + 1)/2) * RowPitch; |
| 136 | } else { |
| 137 | size = Height * RowPitch; |
| 138 | |
| 139 | if (Partial || Height == 1) { |
| 140 | // Must take pixel size in consideration |
| 141 | |
| 142 | size_t BlockSize; |
| 143 | UINT BlockWidth; |
| 144 | UINT BlockHeight; |
| 145 | _getFormatSize(Format, BlockSize, BlockWidth, BlockHeight); |
| 146 | |
| 147 | if (BlockWidth && BlockHeight) { |
| 148 | Width = (Width + BlockWidth - 1) / BlockWidth; |
| 149 | Height = (Height + BlockHeight - 1) / BlockHeight; |
| 150 | size = (Width * BlockSize + 7)/ 8; |
| 151 | if (Height > 1) { |
| 152 | size += (Height - 1) * RowPitch; |
| 153 | } |
| 154 | } |
| 155 | } |
José Fonseca | fc550c5 | 2012-11-18 11:57:56 +0000 | [diff] [blame] | 156 | } |
| 157 | |
José Fonseca | fc550c5 | 2012-11-18 11:57:56 +0000 | [diff] [blame] | 158 | if (Depth > 1) { |
Jose Fonseca | 127cc41 | 2015-07-06 12:33:41 +0100 | [diff] [blame] | 159 | if (SlicePitch < 0) { |
| 160 | os::log("apitrace: warning: %s: negative slice pitch %i\n", __FUNCTION__, SlicePitch); |
| 161 | return 0; |
| 162 | } |
| 163 | |
José Fonseca | fc550c5 | 2012-11-18 11:57:56 +0000 | [diff] [blame] | 164 | size += (Depth - 1) * SlicePitch; |
| 165 | } |
| 166 | |
| 167 | return size; |
| 168 | } |
| 169 | |
| 170 | |