1b8e80941Smrg/*
2b8e80941Smrg * Copyright 2014, 2015 Red Hat.
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
8b8e80941Smrg * license, and/or sell copies of the Software, and to permit persons to whom
9b8e80941Smrg * the Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19b8e80941Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20b8e80941Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21b8e80941Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg/* the virgl hw tgsi vs what the current gallium want will diverge over time.
25b8e80941Smrg   so add a transform stage to remove things we don't want to send unless
26b8e80941Smrg   the receiver supports it.
27b8e80941Smrg*/
28b8e80941Smrg
29b8e80941Smrg#include "tgsi/tgsi_transform.h"
30b8e80941Smrg#include "tgsi/tgsi_info.h"
31b8e80941Smrg#include "virgl_context.h"
32b8e80941Smrg#include "virgl_screen.h"
33b8e80941Smrg
34b8e80941Smrgstruct virgl_transform_context {
35b8e80941Smrg   struct tgsi_transform_context base;
36b8e80941Smrg   bool cull_enabled;
37b8e80941Smrg   bool has_precise;
38b8e80941Smrg   bool fake_fp64;
39b8e80941Smrg};
40b8e80941Smrg
41b8e80941Smrgstatic void
42b8e80941Smrgvirgl_tgsi_transform_declaration(struct tgsi_transform_context *ctx,
43b8e80941Smrg                                 struct tgsi_full_declaration *decl)
44b8e80941Smrg{
45b8e80941Smrg   switch (decl->Declaration.File) {
46b8e80941Smrg   case TGSI_FILE_CONSTANT:
47b8e80941Smrg      if (decl->Declaration.Dimension) {
48b8e80941Smrg         if (decl->Dim.Index2D == 0)
49b8e80941Smrg            decl->Declaration.Dimension = 0;
50b8e80941Smrg      }
51b8e80941Smrg      break;
52b8e80941Smrg   default:
53b8e80941Smrg      break;
54b8e80941Smrg   }
55b8e80941Smrg   ctx->emit_declaration(ctx, decl);
56b8e80941Smrg
57b8e80941Smrg}
58b8e80941Smrg
59b8e80941Smrg/* for now just strip out the new properties the remote doesn't understand
60b8e80941Smrg   yet */
61b8e80941Smrgstatic void
62b8e80941Smrgvirgl_tgsi_transform_property(struct tgsi_transform_context *ctx,
63b8e80941Smrg                              struct tgsi_full_property *prop)
64b8e80941Smrg{
65b8e80941Smrg   struct virgl_transform_context *vtctx = (struct virgl_transform_context *)ctx;
66b8e80941Smrg   switch (prop->Property.PropertyName) {
67b8e80941Smrg   case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED:
68b8e80941Smrg   case TGSI_PROPERTY_NUM_CULLDIST_ENABLED:
69b8e80941Smrg      if (vtctx->cull_enabled)
70b8e80941Smrg	 ctx->emit_property(ctx, prop);
71b8e80941Smrg      break;
72b8e80941Smrg   case TGSI_PROPERTY_NEXT_SHADER:
73b8e80941Smrg      break;
74b8e80941Smrg   default:
75b8e80941Smrg      ctx->emit_property(ctx, prop);
76b8e80941Smrg      break;
77b8e80941Smrg   }
78b8e80941Smrg}
79b8e80941Smrg
80b8e80941Smrgstatic void
81b8e80941Smrgvirgl_tgsi_transform_instruction(struct tgsi_transform_context *ctx,
82b8e80941Smrg				 struct tgsi_full_instruction *inst)
83b8e80941Smrg{
84b8e80941Smrg   struct virgl_transform_context *vtctx = (struct virgl_transform_context *)ctx;
85b8e80941Smrg   if (vtctx->fake_fp64 &&
86b8e80941Smrg       (tgsi_opcode_infer_src_type(inst->Instruction.Opcode, 0) == TGSI_TYPE_DOUBLE ||
87b8e80941Smrg        tgsi_opcode_infer_dst_type(inst->Instruction.Opcode, 0) == TGSI_TYPE_DOUBLE)) {
88b8e80941Smrg      debug_printf("VIRGL: ARB_gpu_shader_fp64 is exposed but not supported.");
89b8e80941Smrg      return;
90b8e80941Smrg   }
91b8e80941Smrg
92b8e80941Smrg   if (!vtctx->has_precise && inst->Instruction.Precise)
93b8e80941Smrg      inst->Instruction.Precise = 0;
94b8e80941Smrg
95b8e80941Smrg   for (unsigned i = 0; i < inst->Instruction.NumSrcRegs; i++) {
96b8e80941Smrg      if (inst->Src[i].Register.File == TGSI_FILE_CONSTANT &&
97b8e80941Smrg          inst->Src[i].Register.Dimension &&
98b8e80941Smrg          inst->Src[i].Dimension.Index == 0)
99b8e80941Smrg         inst->Src[i].Register.Dimension = 0;
100b8e80941Smrg   }
101b8e80941Smrg   ctx->emit_instruction(ctx, inst);
102b8e80941Smrg}
103b8e80941Smrg
104b8e80941Smrgstruct tgsi_token *virgl_tgsi_transform(struct virgl_context *vctx, const struct tgsi_token *tokens_in)
105b8e80941Smrg{
106b8e80941Smrg   struct virgl_screen *vscreen = (struct virgl_screen *)vctx->base.screen;
107b8e80941Smrg   struct virgl_transform_context transform;
108b8e80941Smrg   const uint newLen = tgsi_num_tokens(tokens_in);
109b8e80941Smrg   struct tgsi_token *new_tokens;
110b8e80941Smrg
111b8e80941Smrg   new_tokens = tgsi_alloc_tokens(newLen);
112b8e80941Smrg   if (!new_tokens)
113b8e80941Smrg      return NULL;
114b8e80941Smrg
115b8e80941Smrg   memset(&transform, 0, sizeof(transform));
116b8e80941Smrg   transform.base.transform_declaration = virgl_tgsi_transform_declaration;
117b8e80941Smrg   transform.base.transform_property = virgl_tgsi_transform_property;
118b8e80941Smrg   transform.base.transform_instruction = virgl_tgsi_transform_instruction;
119b8e80941Smrg   transform.cull_enabled = vscreen->caps.caps.v1.bset.has_cull;
120b8e80941Smrg   transform.has_precise = vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_TGSI_PRECISE;
121b8e80941Smrg   transform.fake_fp64 =
122b8e80941Smrg      vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_FAKE_FP64;
123b8e80941Smrg   tgsi_transform_shader(tokens_in, new_tokens, newLen, &transform.base);
124b8e80941Smrg
125b8e80941Smrg   return new_tokens;
126b8e80941Smrg}
127