uc_hwmap.c 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
   Copyright (c) 2003 Andreas Robinson, All rights reserved.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
*/

// Hardware mapping functions ------------------------------------------------

#include "uc_hw.h"
#include <gfx/convert.h>

/// Map DirectFB blending functions to hardware
16 17 18 19 20
inline void
uc_map_blending_fn( struct uc_hw_alpha      *hwalpha,
                    DFBSurfaceBlendFunction  sblend,
                    DFBSurfaceBlendFunction  dblend,
                    DFBSurfacePixelFormat    dst_format )
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
     bool dst_alpha = DFB_PIXELFORMAT_HAS_ALPHA(dst_format);

     // The HW's blending equation is:
     // (Ca * FCa + Cbias + Cb * FCb) << Cshift

     // Set source blending function

     // Ca  -- always from source color.
     hwalpha->regHABLCsat = HC_HABLCsat_MASK | HC_HABLCa_OPC | HC_HABLCa_Csrc;
     // Aa  -- always from source alpha.
     hwalpha->regHABLAsat = HC_HABLAsat_MASK | HC_HABLAa_OPA | HC_HABLAa_Asrc;

     // FCa and FAa depend on the following condition.
     switch (sblend) {
          case DSBF_ZERO:
               // GL_ZERO -- (0, 0, 0, 0)
               hwalpha->regHABLCsat |= HC_HABLFCa_OPC | HC_HABLFCa_HABLRCa;
               hwalpha->regHABLAsat |= HC_HABLFAa_OPA | HC_HABLFAa_HABLFRA;
               hwalpha->regHABLRFCa = 0x0;
               hwalpha->regHABLRAa = 0x0;
               break;

          case DSBF_ONE:
               // GL_ONE -- (1, 1, 1, 1)
               hwalpha->regHABLCsat |= HC_HABLFCa_InvOPC | HC_HABLFCa_HABLRCa;
               hwalpha->regHABLAsat |= HC_HABLFAa_InvOPA | HC_HABLFAa_HABLFRA;
               hwalpha->regHABLRFCa = 0x0;
               hwalpha->regHABLRAa = 0x0;
               break;

          case DSBF_SRCCOLOR:
               // GL_SRC_COLOR -- (Rs, Gs, Bs, As)
               hwalpha->regHABLCsat |= HC_HABLFCa_OPC | HC_HABLFCa_Csrc;
               hwalpha->regHABLAsat |= HC_HABLFAa_OPA | HC_HABLFAa_Asrc;
               break;

          case DSBF_INVSRCCOLOR:
               // GL_ONE_MINUS_SRC_COLOR -- (1, 1, 1, 1) - (Rs, Gs, Bs, As)
               hwalpha->regHABLCsat |= HC_HABLFCa_InvOPC | HC_HABLFCa_Csrc;
               hwalpha->regHABLAsat |= HC_HABLFAa_InvOPA | HC_HABLFAa_Asrc;
               break;

          case DSBF_SRCALPHA:
               // GL_SRC_ALPHA -- (As, As, As, As)
               hwalpha->regHABLCsat |= HC_HABLFCa_OPC | HC_HABLFCa_Asrc;
               hwalpha->regHABLAsat |= HC_HABLFAa_OPA | HC_HABLFAa_Asrc;
               break;

          case DSBF_INVSRCALPHA:
               // GL_ONE_MINUS_SRC_ALPHA -- (1, 1, 1, 1) - (As, As, As, As)
               hwalpha->regHABLCsat |= HC_HABLFCa_InvOPC | HC_HABLFCa_Asrc;
               hwalpha->regHABLAsat |= HC_HABLFAa_InvOPA | HC_HABLFAa_Asrc;
               break;

          case DSBF_DESTALPHA:
               // GL_DST_ALPHA
               if (!dst_alpha) { // (1, 1, 1, 1)
                    hwalpha->regHABLCsat |= HC_HABLFCa_InvOPC | HC_HABLFCa_HABLRCa;
                    hwalpha->regHABLAsat |= HC_HABLFAa_InvOPA | HC_HABLFAa_HABLFRA;
                    hwalpha->regHABLRFCa = 0x0;
                    hwalpha->regHABLRAa = 0x0;
               }
               else { // (Ad, Ad, Ad, Ad)
                    hwalpha->regHABLCsat |= HC_HABLFCa_OPC | HC_HABLFCa_Adst;
                    hwalpha->regHABLAsat |= HC_HABLFAa_OPA | HC_HABLFAa_Adst;
               }
               break;

          case DSBF_INVDESTALPHA:
               // GL_ONE_MINUS_DST_ALPHA
               if (!dst_alpha) { // (1, 1, 1, 1) - (1, 1, 1, 1) = (0, 0, 0, 0)
                    hwalpha->regHABLCsat |= HC_HABLFCa_OPC | HC_HABLFCa_HABLRCa;
                    hwalpha->regHABLAsat |= HC_HABLFAa_OPA | HC_HABLFAa_HABLFRA;
                    hwalpha->regHABLRFCa = 0x0;
                    hwalpha->regHABLRAa = 0x0;
               }
               else { // (1, 1, 1, 1) - (Ad, Ad, Ad, Ad)
                    hwalpha->regHABLCsat |= HC_HABLFCa_InvOPC | HC_HABLFCa_Adst;
                    hwalpha->regHABLAsat |= HC_HABLFAa_InvOPA | HC_HABLFAa_Adst;
               }
               break;

          case DSBF_DESTCOLOR:
               // GL_DST_COLOR -- (Rd, Gd, Bd, Ad)
               hwalpha->regHABLCsat |= HC_HABLFCa_OPC | HC_HABLFCa_Cdst;
               hwalpha->regHABLAsat |= HC_HABLFAa_OPA | HC_HABLFAa_Adst;
               break;

          case DSBF_INVDESTCOLOR:
               // GL_ONE_MINUS_DST_COLOR -- (1, 1, 1, 1) - (Rd, Gd, Bd, Ad)
               hwalpha->regHABLCsat |= HC_HABLFCa_InvOPC | HC_HABLFCa_Cdst;
               hwalpha->regHABLAsat |= HC_HABLFAa_InvOPA | HC_HABLFAa_Adst;
               break;

          case DSBF_SRCALPHASAT:
               // GL_SRC_ALPHA_SATURATE
               if (!dst_alpha) {
                    // (f, f, f, 1), f = min(As, 1 - Ad) = min(As, 1 - 1) = 0
                    // So (f, f, f, 1) = (0, 0, 0, 1)
                    hwalpha->regHABLCsat |= HC_HABLFCa_OPC | HC_HABLFCa_HABLRCa;
                    hwalpha->regHABLAsat |= HC_HABLFAa_InvOPA | HC_HABLFAa_HABLFRA;
                    hwalpha->regHABLRFCa = 0x0;
                    hwalpha->regHABLRAa = 0x0;
               }
               else {
                    // (f, f, f, 1), f = min(As, 1 - Ad)
                    hwalpha->regHABLCsat |= HC_HABLFCa_OPC | HC_HABLFCa_mimAsrcInvAdst;
                    hwalpha->regHABLAsat |= HC_HABLFAa_InvOPA | HC_HABLFAa_HABLFRA;
                    hwalpha->regHABLRFCa = 0x0;
                    hwalpha->regHABLRAa = 0x0;
               }
               break;
     }

     // Set destination blending function

     // Op is add.
     // bias is 0.

     hwalpha->regHABLCsat |= HC_HABLCbias_HABLRCbias;
     hwalpha->regHABLAsat |= HC_HABLAbias_HABLRAbias;

     // Cb  -- always from destination color.
     hwalpha->regHABLCop = HC_HABLCb_OPC | HC_HABLCb_Cdst;
     // Ab  -- always from destination alpha.
     hwalpha->regHABLAop = HC_HABLAb_OPA | HC_HABLAb_Adst;

     // FCb -- depends on the following condition.
     switch (dblend) {
          case DSBF_ZERO:
               // GL_ZERO -- (0, 0, 0, 0)
               hwalpha->regHABLCop |= HC_HABLFCb_OPC | HC_HABLFCb_HABLRCb;
               hwalpha->regHABLAop |= HC_HABLFAb_OPA | HC_HABLFAb_HABLFRA;
               hwalpha->regHABLRFCb = 0x0;
               hwalpha->regHABLRAb = 0x0;
               break;

          case DSBF_ONE:
               // GL_ONE -- (1, 1, 1, 1)
               hwalpha->regHABLCop |= HC_HABLFCb_InvOPC | HC_HABLFCb_HABLRCb;
               hwalpha->regHABLAop |= HC_HABLFAb_InvOPA | HC_HABLFAb_HABLFRA;
               hwalpha->regHABLRFCb = 0x0;
               hwalpha->regHABLRAb = 0x0;
               break;

          case DSBF_SRCCOLOR:
               // GL_SRC_COLOR -- (Rs, Gs, Bs, As)
               hwalpha->regHABLCop |= HC_HABLFCb_OPC | HC_HABLFCb_Csrc;
               hwalpha->regHABLAop |= HC_HABLFAb_OPA | HC_HABLFAb_Asrc;
               break;

          case DSBF_INVSRCCOLOR:
               // GL_ONE_MINUS_SRC_COLOR -- (1, 1, 1, 1) - (Rs, Gs, Bs, As)
               hwalpha->regHABLCop |= HC_HABLFCb_InvOPC | HC_HABLFCb_Csrc;
               hwalpha->regHABLAop |= HC_HABLFAb_InvOPA | HC_HABLFAb_Asrc;
               break;

          case DSBF_SRCALPHA:
               // GL_SRC_ALPHA -- (As, As, As, As)
               hwalpha->regHABLCop |= HC_HABLFCb_OPC | HC_HABLFCb_Asrc;
               hwalpha->regHABLAop |= HC_HABLFAb_OPA | HC_HABLFAb_Asrc;
               break;

          case DSBF_INVSRCALPHA:
               // GL_ONE_MINUS_SRC_ALPHA -- (1, 1, 1, 1) - (As, As, As, As)
               hwalpha->regHABLCop |= HC_HABLFCb_InvOPC | HC_HABLFCb_Asrc;
               hwalpha->regHABLAop |= HC_HABLFAb_InvOPA | HC_HABLFAb_0;
               break;

          case DSBF_DESTALPHA:
               // GL_DST_ALPHA
               if (!dst_alpha) { // (1, 1, 1, 1)
                    hwalpha->regHABLCop |= HC_HABLFCb_InvOPC | HC_HABLFCb_HABLRCb;
                    hwalpha->regHABLAop |= HC_HABLFAb_InvOPA | HC_HABLFAb_HABLFRA;
                    hwalpha->regHABLRFCb = 0x0;
                    hwalpha->regHABLRAb = 0x0;
               }
               else { // (Ad, Ad, Ad, Ad)
                    hwalpha->regHABLCop |= HC_HABLFCb_OPC | HC_HABLFCb_Adst;
                    hwalpha->regHABLAop |= HC_HABLFAb_OPA | HC_HABLFAb_Adst;
               }
               break;

          case DSBF_INVDESTALPHA:
               // GL_ONE_MINUS_DST_ALPHA
               if (!dst_alpha) { // (1, 1, 1, 1) - (1, 1, 1, 1) = (0, 0, 0, 0)
                    hwalpha->regHABLCop |= HC_HABLFCb_OPC | HC_HABLFCb_HABLRCb;
                    hwalpha->regHABLAop |= HC_HABLFAb_OPA | HC_HABLFAb_HABLFRA;
                    hwalpha->regHABLRFCb = 0x0;
                    hwalpha->regHABLRAb = 0x0;
               }
               else { // (1, 1, 1, 1) - (Ad, Ad, Ad, Ad)
                    hwalpha->regHABLCop |= HC_HABLFCb_InvOPC | HC_HABLFCb_Adst;
                    hwalpha->regHABLAop |= HC_HABLFAb_InvOPA | HC_HABLFAb_Adst;
               }
               break;

          case DSBF_DESTCOLOR:
               // GL_DST_COLOR -- (Rd, Gd, Bd, Ad)
               hwalpha->regHABLCop |= HC_HABLFCb_OPC | HC_HABLFCb_Cdst;
               hwalpha->regHABLAop |= HC_HABLFAb_OPA | HC_HABLFAb_Adst;
               break;

          case DSBF_INVDESTCOLOR:
               // GL_ONE_MINUS_DST_COLOR -- (1, 1, 1, 1) - (Rd, Gd, Bd, Ad)
               hwalpha->regHABLCop |= HC_HABLFCb_InvOPC | HC_HABLFCb_Cdst;
               hwalpha->regHABLAop |= HC_HABLFAb_InvOPA | HC_HABLFAb_Adst;
               break;

          case DSBF_SRCALPHASAT:
               // Unsupported?

          default:
               hwalpha->regHABLCop |= HC_HABLFCb_OPC | HC_HABLFCb_HABLRCb;
               hwalpha->regHABLAop |= HC_HABLFAb_OPA | HC_HABLFAb_HABLFRA;
               hwalpha->regHABLRFCb = 0x0;
               hwalpha->regHABLRAb = 0x0;
               break;
     }
241 242 243
}

/// Map DFBSurfaceBlittingFlags to the hardware
244 245 246 247
inline void
uc_map_blitflags( struct uc_hw_texture    *tex,
                  DFBSurfaceBlittingFlags  bflags,
                  DFBSurfacePixelFormat    sformat )
248
{
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
     bool gotalpha = DFB_PIXELFORMAT_HAS_ALPHA(sformat);

     if (bflags & DSBLIT_COLORIZE) {
          // Cv0 = Ct*Cf

          // Hw setting:
          // Ca = Ct, Cb = Cf, Cop = +, Cc = 0, Cbias = 0, Cshift = No.

          tex->regHTXnTBLCsat_0 = HC_HTXnTBLCsat_MASK |
                                  HC_HTXnTBLCa_TOPC | HC_HTXnTBLCa_Tex |
                                  HC_HTXnTBLCb_TOPC | HC_HTXnTBLCb_Dif |
                                  HC_HTXnTBLCc_TOPC | HC_HTXnTBLCc_0;
          tex->regHTXnTBLCop_0 = HC_HTXnTBLCop_Add |
                                 HC_HTXnTBLCbias_Cbias | HC_HTXnTBLCbias_0 |
                                 HC_HTXnTBLCshift_No;
          tex->regHTXnTBLMPfog_0 = HC_HTXnTBLMPfog_0;
     }
     else {
          // Cv0 = Ct

          // Hw setting:
          // Ca = 0, Cb = 0, Cop = +, Cc = 0, Cbias = Ct, Cshift = No.

          tex->regHTXnTBLCsat_0 = HC_HTXnTBLCsat_MASK |
                                  HC_HTXnTBLCa_TOPC | HC_HTXnTBLCa_0 |
                                  HC_HTXnTBLCb_TOPC | HC_HTXnTBLCb_0 |
                                  HC_HTXnTBLCc_TOPC | HC_HTXnTBLCc_0;
          tex->regHTXnTBLCop_0 = HC_HTXnTBLCop_Add |
                                 HC_HTXnTBLCbias_Cbias | HC_HTXnTBLCbias_Tex |
                                 HC_HTXnTBLCshift_No;
          tex->regHTXnTBLMPfog_0 = HC_HTXnTBLMPfog_0;
     }

     if (bflags & DSBLIT_BLEND_COLORALPHA) {
          if ((bflags & DSBLIT_BLEND_ALPHACHANNEL) && gotalpha) {
               // Av0 = At*Af

               // Hw setting:
               // Aa = At, Ab = Af, Cop = +, Ac = 0, Abias = 0, Ashift = No.

               tex->regHTXnTBLAsat_0 = HC_HTXnTBLAsat_MASK |
                                       HC_HTXnTBLAa_TOPA | HC_HTXnTBLAa_Atex |
                                       HC_HTXnTBLAb_TOPA | HC_HTXnTBLAb_Adif |
                                       HC_HTXnTBLAc_TOPA | HC_HTXnTBLAc_HTXnTBLRA;
               tex->regHTXnTBLCop_0 |= HC_HTXnTBLAop_Add |
                                       HC_HTXnTBLAbias_HTXnTBLRAbias | HC_HTXnTBLAshift_No;
               tex->regHTXnTBLRAa_0 = 0x0;
               tex->regHTXnTBLRFog_0 = 0x0;
          }
          else {
               // (!(bflags & DSBLIT_BLEND_ALPHACHANNEL) && gotalpha) || !gotalpha
               // Av0 = Af

               // Hw setting:
               // Aa = 0, Ab = 0, Cop = +, Ac = 0, Abias = Af, Ashift = No.

               tex->regHTXnTBLAsat_0 = HC_HTXnTBLAsat_MASK |
                                       HC_HTXnTBLAa_TOPA | HC_HTXnTBLAa_HTXnTBLRA |
                                       HC_HTXnTBLAb_TOPA | HC_HTXnTBLAb_HTXnTBLRA |
                                       HC_HTXnTBLAc_TOPA | HC_HTXnTBLAc_HTXnTBLRA;
               tex->regHTXnTBLCop_0 |= HC_HTXnTBLAop_Add |
                                       HC_HTXnTBLAbias_Adif | HC_HTXnTBLAshift_No;
               tex->regHTXnTBLRAa_0 = 0x0;
               tex->regHTXnTBLRFog_0 = 0x0;
          }
     }
     else {  // !(bflags & DSBLIT_BLEND_COLORALPHA)
          if ((bflags & DSBLIT_BLEND_ALPHACHANNEL) && gotalpha) {
               // Av0 = At

               // Hw setting:
               // Aa = 0, Ab = 0, Cop = +, Ac = 0, Abias = At, Ashift = No.

               tex->regHTXnTBLAsat_0 = HC_HTXnTBLAsat_MASK |
                                       HC_HTXnTBLAa_TOPA | HC_HTXnTBLAa_HTXnTBLRA |
                                       HC_HTXnTBLAb_TOPA | HC_HTXnTBLAb_HTXnTBLRA |
                                       HC_HTXnTBLAc_TOPA | HC_HTXnTBLAc_HTXnTBLRA;
               tex->regHTXnTBLCop_0 |= HC_HTXnTBLAop_Add |
                                       HC_HTXnTBLAbias_Atex | HC_HTXnTBLAshift_No;
               tex->regHTXnTBLRAa_0 = 0x0;
               tex->regHTXnTBLRFog_0 = 0x0;
          }
          else { // !gotalpha
               // Av0 = 1.0

               // D_BUG warning: I'm guessing where values should go,
               // and how big (0xff = 1.0 ?) it should be.

               // Hw setting:
               // Aa = 1.0, Ab = 1.0, Cop = -, Ac = 1.0, Abias = 1.0, Ashift = No.
               // => Av = Aa*(Ab-Ac) + Abias = 1*(1-1)+1 = 1

               tex->regHTXnTBLAsat_0 = HC_HTXnTBLAsat_MASK |
                                       HC_HTXnTBLAa_TOPA | HC_HTXnTBLAa_HTXnTBLRA |
                                       HC_HTXnTBLAb_TOPA | HC_HTXnTBLAb_HTXnTBLRA |
                                       HC_HTXnTBLAc_TOPA | HC_HTXnTBLAc_HTXnTBLRA;
               tex->regHTXnTBLCop_0 |= HC_HTXnTBLAop_Add |
                                       HC_HTXnTBLAbias_Inv | HC_HTXnTBLAbias_HTXnTBLRAbias | HC_HTXnTBLAshift_No;
               tex->regHTXnTBLRAa_0 = 0x0;
               tex->regHTXnTBLRFog_0 = 0x0;
          }
     }
351
}
352