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 
29 #include "tgsi/tgsi_transform.h"
30 #include "tgsi/tgsi_info.h"
31 #include "virgl_context.h"
32 #include "virgl_screen.h"
33 
34 struct virgl_transform_context {
35    struct tgsi_transform_context base;
36    bool cull_enabled;
37    bool has_precise;
38    bool fake_fp64;
39 };
40 
41 static void
virgl_tgsi_transform_declaration(struct tgsi_transform_context * ctx,struct tgsi_full_declaration * decl)42 virgl_tgsi_transform_declaration(struct tgsi_transform_context *ctx,
43                                  struct tgsi_full_declaration *decl)
44 {
45    switch (decl->Declaration.File) {
46    case TGSI_FILE_CONSTANT:
47       if (decl->Declaration.Dimension) {
48          if (decl->Dim.Index2D == 0)
49             decl->Declaration.Dimension = 0;
50       }
51       break;
52    default:
53       break;
54    }
55    ctx->emit_declaration(ctx, decl);
56 
57 }
58 
59 /* for now just strip out the new properties the remote doesn't understand
60    yet */
61 static void
virgl_tgsi_transform_property(struct tgsi_transform_context * ctx,struct tgsi_full_property * prop)62 virgl_tgsi_transform_property(struct tgsi_transform_context *ctx,
63                               struct tgsi_full_property *prop)
64 {
65    struct virgl_transform_context *vtctx = (struct virgl_transform_context *)ctx;
66    switch (prop->Property.PropertyName) {
67    case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED:
68    case TGSI_PROPERTY_NUM_CULLDIST_ENABLED:
69       if (vtctx->cull_enabled)
70 	 ctx->emit_property(ctx, prop);
71       break;
72    case TGSI_PROPERTY_NEXT_SHADER:
73       break;
74    default:
75       ctx->emit_property(ctx, prop);
76       break;
77    }
78 }
79 
80 static void
virgl_tgsi_transform_instruction(struct tgsi_transform_context * ctx,struct tgsi_full_instruction * inst)81 virgl_tgsi_transform_instruction(struct tgsi_transform_context *ctx,
82 				 struct tgsi_full_instruction *inst)
83 {
84    struct virgl_transform_context *vtctx = (struct virgl_transform_context *)ctx;
85    if (vtctx->fake_fp64 &&
86        (tgsi_opcode_infer_src_type(inst->Instruction.Opcode, 0) == TGSI_TYPE_DOUBLE ||
87         tgsi_opcode_infer_dst_type(inst->Instruction.Opcode, 0) == TGSI_TYPE_DOUBLE)) {
88       debug_printf("VIRGL: ARB_gpu_shader_fp64 is exposed but not supported.");
89       return;
90    }
91 
92    if (!vtctx->has_precise && inst->Instruction.Precise)
93       inst->Instruction.Precise = 0;
94 
95    for (unsigned i = 0; i < inst->Instruction.NumSrcRegs; i++) {
96       if (inst->Src[i].Register.File == TGSI_FILE_CONSTANT &&
97           inst->Src[i].Register.Dimension &&
98           inst->Src[i].Dimension.Index == 0)
99          inst->Src[i].Register.Dimension = 0;
100    }
101    ctx->emit_instruction(ctx, inst);
102 }
103 
virgl_tgsi_transform(struct virgl_context * vctx,const struct tgsi_token * tokens_in)104 struct tgsi_token *virgl_tgsi_transform(struct virgl_context *vctx, const struct tgsi_token *tokens_in)
105 {
106    struct virgl_screen *vscreen = (struct virgl_screen *)vctx->base.screen;
107    struct virgl_transform_context transform;
108    const uint newLen = tgsi_num_tokens(tokens_in);
109    struct tgsi_token *new_tokens;
110 
111    new_tokens = tgsi_alloc_tokens(newLen);
112    if (!new_tokens)
113       return NULL;
114 
115    memset(&transform, 0, sizeof(transform));
116    transform.base.transform_declaration = virgl_tgsi_transform_declaration;
117    transform.base.transform_property = virgl_tgsi_transform_property;
118    transform.base.transform_instruction = virgl_tgsi_transform_instruction;
119    transform.cull_enabled = vscreen->caps.caps.v1.bset.has_cull;
120    transform.has_precise = vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_TGSI_PRECISE;
121    transform.fake_fp64 =
122       vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_FAKE_FP64;
123    tgsi_transform_shader(tokens_in, new_tokens, newLen, &transform.base);
124 
125    return new_tokens;
126 }
127