1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Provide a class (SSE/SIMD only) holding a 2d matrix of class FourVectors,
// for high speed processing in tools.
//
// $NoKeywords: $
//
//=============================================================================//
#ifndef SIMDVECTORMATRIX_H
#define SIMDVECTORMATRIX_H
#ifdef _WIN32
#pragma once
#endif
#include <string.h>
#include "tier0/platform.h"
#include "tier0/dbg.h"
#include "tier1/utlsoacontainer.h"
#include "mathlib/ssemath.h"
class CSIMDVectorMatrix
{
public:
int m_nWidth; // in actual vectors
int m_nHeight;
int m_nPaddedWidth; // # of 4x wide elements
FourVectors *m_pData;
protected:
void Init( void )
{
m_pData = NULL;
m_nWidth = 0;
m_nHeight = 0;
m_nPaddedWidth = 0;
}
int NVectors( void ) const
{
return m_nHeight * m_nPaddedWidth;
}
public:
// constructors and destructors
CSIMDVectorMatrix( void )
{
Init();
}
~CSIMDVectorMatrix( void )
{
if ( m_pData )
delete[] m_pData;
}
// set up storage and fields for m x n matrix. destroys old data
void SetSize( int width, int height )
{
if ( ( ! m_pData ) || ( width != m_nWidth ) || ( height != m_nHeight ) )
{
if ( m_pData )
delete[] m_pData;
m_nWidth = width;
m_nHeight = height;
m_nPaddedWidth = ( m_nWidth + 3) >> 2;
m_pData = NULL;
if ( width && height )
m_pData = new FourVectors[ m_nPaddedWidth * m_nHeight ];
}
}
CSIMDVectorMatrix( int width, int height )
{
Init();
SetSize( width, height );
}
CSIMDVectorMatrix &operator=( CSIMDVectorMatrix const &src )
{
SetSize( src.m_nWidth, src.m_nHeight );
if ( m_pData )
memcpy( m_pData, src.m_pData, m_nHeight*m_nPaddedWidth*sizeof(m_pData[0]) );
return *this;
}
CSIMDVectorMatrix &operator+=( CSIMDVectorMatrix const &src );
CSIMDVectorMatrix &operator*=( Vector const &src );
// create from an RGBA float bitmap. alpha ignored.
void CreateFromRGBA_FloatImageData(int srcwidth, int srcheight, float const *srcdata );
// create from 3 fields in a csoa
void CreateFromCSOAAttributes( CSOAContainer const *pSrc,
int nAttrIdx0, int nAttrIdx1, int nAttrIdx2 );
// Element access. If you are calling this a lot, you don't want to use this class, because
// you're not getting the sse advantage
Vector Element(int x, int y) const
{
Assert( m_pData );
Assert( x < m_nWidth );
Assert( y < m_nHeight );
Vector ret;
FourVectors const *pData=m_pData+y*m_nPaddedWidth+(x >> 2);
int xo=(x & 3);
ret.x=pData->X( xo );
ret.y=pData->Y( xo );
ret.z=pData->Z( xo );
return ret;
}
//addressing the individual fourvectors elements
FourVectors &CompoundElement(int x, int y)
{
Assert( m_pData );
Assert( y < m_nHeight );
Assert( x < m_nPaddedWidth );
return m_pData[x + m_nPaddedWidth*y ];
}
// math operations on the whole image
void Clear( void )
{
Assert( m_pData );
memset( m_pData, 0, m_nHeight*m_nPaddedWidth*sizeof(m_pData[0]) );
}
void RaiseToPower( float power );
};
#endif
|