1 // license:BSD-3-Clause
2 // copyright-holders:Aaron Giles
3 //============================================================
4 //
5 //  d3dcomm.h - Common Win32 Direct3D structures
6 //
7 //============================================================
8 
9 #ifndef __WIN_D3DCOMM__
10 #define __WIN_D3DCOMM__
11 
12 //============================================================
13 //  CONSTANTS
14 //============================================================
15 
16 #define MAX_BLOOM_COUNT 15 // shader model 3.0 support up to 16 samplers, but we need the last for the original texture
17 #define HALF_BLOOM_COUNT 8
18 
19 //============================================================
20 //  FORWARD DECLARATIONS
21 //============================================================
22 
23 class texture_info;
24 class renderer_d3d9;
25 struct d3d_base;
26 
27 //============================================================
28 //  TYPE DEFINITIONS
29 //============================================================
30 
31 class vec2f
32 {
33 public:
vec2f()34 	vec2f()
35 	{
36 		memset(&c, 0, sizeof(float) * 2);
37 	}
vec2f(float x,float y)38 	vec2f(float x, float y)
39 	{
40 		c.x = x;
41 		c.y = y;
42 	}
43 
44 	vec2f operator+(const vec2f& a) const
45 	{
46 		return vec2f(c.x + a.c.x, c.y + a.c.y);
47 	}
48 
49 	vec2f operator-(const vec2f& a) const
50 	{
51 		return vec2f(c.x - a.c.x, c.y - a.c.y);
52 	}
53 
54 	struct
55 	{
56 		float x, y;
57 	} c;
58 };
59 
60 class d3d_texture_manager
61 {
62 public:
d3d_texture_manager()63 	d3d_texture_manager(): m_renderer(nullptr), m_yuv_format(), m_texture_caps(0), m_texture_max_aspect(0), m_texture_max_width(0), m_texture_max_height(0), m_default_texture(nullptr)
64 	{ }
65 
66 	d3d_texture_manager(renderer_d3d9 *d3d);
67 
68 	void                    update_textures();
69 
70 	void                    create_resources();
71 	void                    delete_resources();
72 
73 	texture_info *          find_texinfo(const render_texinfo *texture, uint32_t flags);
74 	uint32_t                texture_compute_hash(const render_texinfo *texture, uint32_t flags);
75 
get_yuv_format()76 	D3DFORMAT               get_yuv_format() const { return m_yuv_format; }
77 
get_texture_caps()78 	DWORD                   get_texture_caps() const { return m_texture_caps; }
get_max_texture_aspect()79 	DWORD                   get_max_texture_aspect() const { return m_texture_max_aspect; }
get_max_texture_width()80 	DWORD                   get_max_texture_width() const { return m_texture_max_width; }
get_max_texture_height()81 	DWORD                   get_max_texture_height() const { return m_texture_max_height; }
82 
get_default_texture()83 	texture_info *          get_default_texture() const { return m_default_texture; }
84 
get_d3d()85 	renderer_d3d9 *         get_d3d() const { return m_renderer; }
86 
87 	std::vector<std::unique_ptr<texture_info>> m_texture_list;  // list of active textures
88 
89 private:
90 	renderer_d3d9 *         m_renderer;
91 	D3DFORMAT               m_yuv_format;               // format to use for YUV textures
92 
93 	DWORD                   m_texture_caps;             // textureCaps field
94 	DWORD                   m_texture_max_aspect;       // texture maximum aspect ratio
95 	DWORD                   m_texture_max_width;        // texture maximum width
96 	DWORD                   m_texture_max_height;       // texture maximum height
97 
98 	bitmap_rgb32            m_default_bitmap;           // experimental: default bitmap
99 	texture_info *          m_default_texture;          // experimental: default texture
100 };
101 
102 
103 /* texture_info holds information about a texture */
104 class texture_info
105 {
106 public:
107 	texture_info(d3d_texture_manager *manager, const render_texinfo *texsource, int prescale, uint32_t flags);
108 	~texture_info();
109 
get_texinfo()110 	render_texinfo &        get_texinfo() { return m_texinfo; }
111 
get_width()112 	int                     get_width() const { return m_rawdims.c.x; }
get_height()113 	int                     get_height() const { return m_rawdims.c.y; }
get_xscale()114 	int                     get_xscale() const { return m_xprescale; }
get_yscale()115 	int                     get_yscale() const { return m_yprescale; }
116 
get_flags()117 	uint32_t                get_flags() const { return m_flags; }
118 
119 	void                    set_data(const render_texinfo *texsource, uint32_t flags);
120 
get_hash()121 	uint32_t                get_hash() const { return m_hash; }
122 
increment_frame_count()123 	void                    increment_frame_count() { m_cur_frame++; }
mask_frame_count(int mask)124 	void                    mask_frame_count(int mask) { m_cur_frame %= mask; }
125 
get_cur_frame()126 	int                     get_cur_frame() const { return m_cur_frame; }
127 
get_tex()128 	IDirect3DTexture9 *     get_tex() const { return m_d3dtex; }
get_surface()129 	IDirect3DSurface9 *     get_surface() const { return m_d3dsurface; }
get_finaltex()130 	IDirect3DTexture9 *     get_finaltex() const { return m_d3dfinaltex; }
131 
get_uvstart()132 	vec2f &                 get_uvstart() { return m_start; }
get_uvstop()133 	vec2f &                 get_uvstop() { return m_stop; }
get_rawdims()134 	vec2f &                 get_rawdims() { return m_rawdims; }
135 
136 private:
137 	void prescale();
138 	void compute_size(int texwidth, int texheight);
139 	void compute_size_subroutine(int texwidth, int texheight, int* p_width, int* p_height);
140 
141 	inline void copyline_palette16(uint32_t *dst, const uint16_t *src, int width, const rgb_t *palette, int xborderpix);
142 	inline void copyline_rgb32(uint32_t *dst, const uint32_t *src, int width, const rgb_t *palette, int xborderpix);
143 	inline void copyline_argb32(uint32_t *dst, const uint32_t *src, int width, const rgb_t *palette, int xborderpix);
144 	inline void copyline_yuy16_to_yuy2(uint16_t *dst, const uint16_t *src, int width, const rgb_t *palette);
145 	inline void copyline_yuy16_to_uyvy(uint16_t *dst, const uint16_t *src, int width, const rgb_t *palette);
146 	inline void copyline_yuy16_to_argb(uint32_t *dst, const uint16_t *src, int width, const rgb_t *palette);
147 
148 	d3d_texture_manager *   m_texture_manager;          // texture manager pointer
149 
150 	renderer_d3d9 *         m_renderer;                 // renderer pointer
151 
152 	uint32_t                m_hash;                     // hash value for the texture
153 	uint32_t                m_flags;                    // rendering flags
154 	render_texinfo          m_texinfo;                  // copy of the texture info
155 	vec2f                   m_start;                    // beggining UV coordinates
156 	vec2f                   m_stop;                     // ending UV coordinates
157 	vec2f                   m_rawdims;                  // raw dims of the texture
158 	int                     m_type;                     // what type of texture are we?
159 	int                     m_xprescale, m_yprescale;   // X/Y prescale factor
160 	int                     m_xborderpix, m_yborderpix; // X/Y border pixels
161 	int                     m_cur_frame;                // what is our current frame?
162 	IDirect3DTexture9 *     m_d3dtex;                   // Direct3D texture pointer
163 	IDirect3DSurface9 *     m_d3dsurface;               // Direct3D offscreen plain surface pointer
164 	IDirect3DTexture9 *     m_d3dfinaltex;              // Direct3D final (post-scaled) texture
165 };
166 
167 /* poly_info holds information about a single polygon/d3d primitive */
168 class poly_info
169 {
170 public:
init(D3DPRIMITIVETYPE type,uint32_t count,uint32_t numverts,uint32_t flags,texture_info * texture,uint32_t modmode,float prim_width,float prim_height)171 	void init(D3DPRIMITIVETYPE type, uint32_t count, uint32_t numverts,
172 				uint32_t flags, texture_info *texture, uint32_t modmode,
173 				float prim_width, float prim_height)
174 	{
175 		m_type = type;
176 		m_count = count;
177 		m_numverts = numverts;
178 		m_flags = flags;
179 		m_texture = texture;
180 		m_modmode = modmode;
181 		m_prim_width = prim_width;
182 		m_prim_height = prim_height;
183 	}
184 
type()185 	D3DPRIMITIVETYPE        type() const { return m_type; }
count()186 	uint32_t                count() const { return m_count; }
numverts()187 	uint32_t                numverts() const { return m_numverts; }
flags()188 	uint32_t                flags() const { return m_flags; }
189 
texture()190 	texture_info *          texture() const { return m_texture; }
modmode()191 	DWORD                   modmode() const { return m_modmode; }
192 
prim_width()193 	float                   prim_width() const { return m_prim_width; }
prim_height()194 	float                   prim_height() const { return m_prim_height; }
195 
196 private:
197 	D3DPRIMITIVETYPE        m_type;         // type of primitive
198 	uint32_t                m_count;        // total number of primitives
199 	uint32_t                m_numverts;     // total number of vertices
200 	uint32_t                m_flags;        // rendering flags
201 
202 	texture_info *          m_texture;      // pointer to texture info
203 	DWORD                   m_modmode;      // texture modulation mode
204 
205 	float                   m_prim_width;   // used by quads
206 	float                   m_prim_height;  // used by quads
207 };
208 
209 /* vertex describes a single vertex */
210 struct vertex
211 {
212 	float       x, y, z;                    // X,Y,Z coordinates
213 	float       rhw;                        // RHW when no HLSL, padding when HLSL
214 	D3DCOLOR    color;                      // diffuse color
215 	float       u0, v0;                     // texture stage 0 coordinates
216 	float       u1, v1;                     // additional info for vector data
217 };
218 
219 
220 /* d3d_render_target is the information about a Direct3D render target chain */
221 class d3d_render_target
222 {
223 public:
224 	// construction/destruction
d3d_render_target()225 	d3d_render_target(): target_width(0), target_height(0), width(0), height(0), screen_index(0), bloom_count(0)
226 	{
227 		for (int index = 0; index < MAX_BLOOM_COUNT; index++)
228 		{
229 			bloom_texture[index] = nullptr;
230 			bloom_surface[index] = nullptr;
231 		}
232 
233 		for (int index = 0; index < 2; index++)
234 		{
235 			source_texture[index] = nullptr;
236 			source_surface[index] = nullptr;
237 			target_texture[index] = nullptr;
238 			target_surface[index] = nullptr;
239 		}
240 
241 		cache_texture = nullptr;
242 		cache_surface = nullptr;
243 	}
244 
245 	~d3d_render_target();
246 
247 	bool init(renderer_d3d9 *d3d, int source_width, int source_height, int target_width, int target_height, int screen_index);
next_index(int index)248 	int next_index(int index) { return ++index > 1 ? 0 : index; }
249 
250 	// real target dimension
251 	int target_width;
252 	int target_height;
253 
254 	// only used to identify/find the render target
255 	int width;
256 	int height;
257 
258 	int screen_index;
259 
260 	IDirect3DSurface9 *target_surface[2];
261 	IDirect3DTexture9 *target_texture[2];
262 	IDirect3DSurface9 *source_surface[2];
263 	IDirect3DTexture9 *source_texture[2];
264 
265 	IDirect3DSurface9 *cache_surface;
266 	IDirect3DTexture9 *cache_texture;
267 
268 	IDirect3DSurface9 *bloom_surface[MAX_BLOOM_COUNT];
269 	IDirect3DTexture9 *bloom_texture[MAX_BLOOM_COUNT];
270 
271 	float bloom_dims[MAX_BLOOM_COUNT][2];
272 
273 	int bloom_count;
274 };
275 
276 #endif
277