1/*
2    SDL - Simple DirectMedia Layer
3    Copyright (C) 1997-2006 Sam Lantinga
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19    Sam Lantinga
20    slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24#include "SDL_video.h"
25#include "../SDL_blit.h"
26#include "SDL_fb3dfx.h"
27#include "3dfx_mmio.h"
28
29
30/* Wait for vertical retrace */
31static void WaitVBL(_THIS)
32{
33	/* find start of retrace */
34	tdfx_waitidle();
35	while( (tdfx_in32(TDFX_STATUS) & STATUS_RETRACE) == STATUS_RETRACE )
36		;
37	/* wait until we're past the start */
38	while( (tdfx_in32(TDFX_STATUS) & STATUS_RETRACE) == 0 )
39		;
40}
41static void WaitIdle(_THIS)
42{
43	tdfx_waitidle();
44}
45
46/* Sets video mem colorkey and accelerated blit function */
47static int SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key)
48{
49	return(0);
50}
51
52static int FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color)
53{
54	int bpp;
55	Uint32 dst_base;
56	Uint32 format;
57	int dstX, dstY;
58
59	/* Don't blit to the display surface when switched away */
60	if ( switched_away ) {
61		return -2; /* no hardware access */
62	}
63	if ( dst == this->screen ) {
64		SDL_mutexP(hw_lock);
65	}
66
67	/* Set the destination pixel format */
68	dst_base = ((char *)dst->pixels - mapped_mem);
69	bpp = dst->format->BitsPerPixel;
70	format = dst->pitch | ((bpp+((bpp==8) ? 0 : 8)) << 13);
71
72	/* Calculate source and destination base coordinates */
73	dstX = rect->x;
74	dstY = rect->y;
75
76	/* Execute the fill command */
77	tdfx_wait(6);
78	tdfx_out32(DSTBASE, dst_base);
79	tdfx_out32(DSTFORMAT, format);
80	tdfx_out32(COLORFORE, color);
81	tdfx_out32(COMMAND_2D, COMMAND_2D_FILLRECT);
82	tdfx_out32(DSTSIZE, rect->w | (rect->h << 16));
83	tdfx_out32(LAUNCH_2D, dstX | (dstY << 16));
84
85	FB_AddBusySurface(dst);
86
87	if ( dst == this->screen ) {
88		SDL_mutexV(hw_lock);
89	}
90	return(0);
91}
92
93static int HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
94                       SDL_Surface *dst, SDL_Rect *dstrect)
95{
96	SDL_VideoDevice *this = current_video;
97	int bpp;
98	Uint32 src_format;
99	Uint32 dst_format;
100	Uint32 src_base;
101	Uint32 dst_base;
102	int srcX, srcY;
103	int dstX, dstY;
104	Uint32 blitop;
105	Uint32 use_colorkey;
106
107	/* Don't blit to the display surface when switched away */
108	if ( switched_away ) {
109		return -2; /* no hardware access */
110	}
111	if ( dst == this->screen ) {
112		SDL_mutexP(hw_lock);
113	}
114
115	/* Set the source and destination pixel format */
116	src_base = ((char *)src->pixels - mapped_mem);
117	bpp = src->format->BitsPerPixel;
118	src_format = src->pitch | ((bpp+((bpp==8) ? 0 : 8)) << 13);
119	dst_base = ((char *)dst->pixels - mapped_mem);
120	bpp = dst->format->BitsPerPixel;
121	dst_format = dst->pitch | ((bpp+((bpp==8) ? 0 : 8)) << 13);
122
123	srcX = srcrect->x;
124	srcY = srcrect->y;
125	dstX = dstrect->x;
126	dstY = dstrect->y;
127
128	/* Assemble the blit operation */
129	blitop = COMMAND_2D_BITBLT | (0xCC << 24);
130	if ( srcX <= dstX ) {
131		blitop |= BIT(14);
132		srcX += (dstrect->w - 1);
133		dstX += (dstrect->w - 1);
134	}
135	if ( srcY <= dstY ) {
136		blitop |= BIT(15);
137		srcY += (dstrect->h - 1);
138		dstY += (dstrect->h - 1);
139	}
140
141	/* Perform the blit! */
142	if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
143    		tdfx_wait(3);
144    		tdfx_out32(SRCCOLORKEYMIN, src->format->colorkey);
145    		tdfx_out32(SRCCOLORKEYMAX, src->format->colorkey);
146    		tdfx_out32(ROP_2D, 0xAA00);
147		use_colorkey = 1;
148	} else {
149		use_colorkey = 0;
150	}
151	tdfx_wait(9);
152	tdfx_out32(SRCBASE, (Uint32)src_base);
153	tdfx_out32(SRCFORMAT, src_format);
154	tdfx_out32(DSTBASE, (Uint32)dst_base);
155	tdfx_out32(DSTFORMAT, src_format);
156	tdfx_out32(COMMAND_2D, blitop);
157	tdfx_out32(COMMANDEXTRA_2D, use_colorkey);
158	tdfx_out32(DSTSIZE, dstrect->w | (dstrect->h << 16));
159	tdfx_out32(DSTXY, dstX | (dstY << 16));
160	tdfx_out32(LAUNCH_2D, srcX | (srcY << 16));
161
162	FB_AddBusySurface(src);
163	FB_AddBusySurface(dst);
164
165	if ( dst == this->screen ) {
166		SDL_mutexV(hw_lock);
167	}
168	return(0);
169}
170
171static int CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
172{
173	int accelerated;
174
175	/* Set initial acceleration on */
176	src->flags |= SDL_HWACCEL;
177
178	/* Set the surface attributes */
179	if ( (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
180		if ( ! this->info.blit_hw_A ) {
181			src->flags &= ~SDL_HWACCEL;
182		}
183	}
184	if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
185		if ( ! this->info.blit_hw_CC ) {
186			src->flags &= ~SDL_HWACCEL;
187		}
188	}
189
190	/* Check to see if final surface blit is accelerated */
191	accelerated = !!(src->flags & SDL_HWACCEL);
192	if ( accelerated ) {
193		src->map->hw_blit = HWAccelBlit;
194	}
195	return(accelerated);
196}
197
198void FB_3DfxAccel(_THIS, __u32 card)
199{
200	/* We have hardware accelerated surface functions */
201	this->CheckHWBlit = CheckHWBlit;
202	wait_vbl = WaitVBL;
203	wait_idle = WaitIdle;
204
205	/* Reset the 3Dfx controller */
206	tdfx_out32(BRESERROR0, 0);
207	tdfx_out32(BRESERROR1, 0);
208
209	/* The 3Dfx has an accelerated color fill */
210	this->info.blit_fill = 1;
211	this->FillHWRect = FillHWRect;
212
213	/* The 3Dfx has accelerated normal and colorkey blits */
214	this->info.blit_hw = 1;
215	this->info.blit_hw_CC = 1;
216	this->SetHWColorKey = SetHWColorKey;
217}
218