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