1/*
2 * Copyright 2011 Christoph Bumiller
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23#include "nv50_ir_from_common.h"
24
25namespace nv50_ir {
26
27ConverterCommon::ConverterCommon(Program *prog, nv50_ir_prog_info *info,
28                                 nv50_ir_prog_info_out *info_out)
29   :  BuildUtil(prog),
30      info(info),
31      info_out(info_out) {}
32
33ConverterCommon::Subroutine *
34ConverterCommon::getSubroutine(unsigned ip)
35{
36   std::map<unsigned, Subroutine>::iterator it = sub.map.find(ip);
37
38   if (it == sub.map.end())
39      it = sub.map.insert(std::make_pair(
40              ip, Subroutine(new Function(prog, "SUB", ip)))).first;
41
42   return &it->second;
43}
44
45ConverterCommon::Subroutine *
46ConverterCommon::getSubroutine(Function *f)
47{
48   unsigned ip = f->getLabel();
49   std::map<unsigned, Subroutine>::iterator it = sub.map.find(ip);
50
51   if (it == sub.map.end())
52      it = sub.map.insert(std::make_pair(ip, Subroutine(f))).first;
53
54   return &it->second;
55}
56
57uint8_t
58ConverterCommon::translateInterpMode(const struct nv50_ir_varying *var, operation& op)
59{
60   uint8_t mode = NV50_IR_INTERP_PERSPECTIVE;
61
62   if (var->flat)
63      mode = NV50_IR_INTERP_FLAT;
64   else
65   if (var->linear)
66      mode = NV50_IR_INTERP_LINEAR;
67   else
68   if (var->sc)
69      mode = NV50_IR_INTERP_SC;
70
71   op = (mode == NV50_IR_INTERP_PERSPECTIVE || mode == NV50_IR_INTERP_SC)
72      ? OP_PINTERP : OP_LINTERP;
73
74   if (var->centroid)
75      mode |= NV50_IR_INTERP_CENTROID;
76
77   return mode;
78}
79
80void
81ConverterCommon::handleUserClipPlanes()
82{
83   Value *res[8];
84   int n, i, c;
85
86   for (c = 0; c < 4; ++c) {
87      for (i = 0; i < info_out->io.genUserClip; ++i) {
88         Symbol *sym = mkSymbol(FILE_MEMORY_CONST, info->io.auxCBSlot,
89                                TYPE_F32, info->io.ucpBase + i * 16 + c * 4);
90         Value *ucp = mkLoadv(TYPE_F32, sym, NULL);
91         if (c == 0)
92            res[i] = mkOp2v(OP_MUL, TYPE_F32, getScratch(), clipVtx[c], ucp);
93         else
94            mkOp3(OP_MAD, TYPE_F32, res[i], clipVtx[c], ucp, res[i]);
95      }
96   }
97
98   const int first = info_out->numOutputs - (info_out->io.genUserClip + 3) / 4;
99
100   for (i = 0; i < info_out->io.genUserClip; ++i) {
101      n = i / 4 + first;
102      c = i % 4;
103      Symbol *sym =
104         mkSymbol(FILE_SHADER_OUTPUT, 0, TYPE_F32, info_out->out[n].slot[c] * 4);
105      mkStore(OP_EXPORT, TYPE_F32, sym, NULL, res[i]);
106   }
107}
108
109} // namespace nv50_ir
110