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