]> git.tdb.fi Git - libs/gl.git/commitdiff
Implement textureSize as a visitor function in the SPIR-V generator
authorMikko Rasa <tdb@tdb.fi>
Wed, 28 Apr 2021 11:38:55 +0000 (14:38 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 28 Apr 2021 11:38:55 +0000 (14:38 +0300)
It requires an image as an operand, not a sampled image.

source/glsl/spirv.cpp
source/glsl/spirv.h
source/glsl/spirvconstants.h

index 33da0d5ca86c830bbb8f2248f4ee774b8ecc5e0d..186521c5efe109b1bfcb1f6d331cfb666f2617fa 100644 (file)
@@ -107,7 +107,7 @@ const SpirVGenerator::BuiltinFunctionInfo SpirVGenerator::builtin_functions[] =
        { "findLSB", "u", "GLSL.std.450", GLSL450_FIND_I_LSB, { 1 }, 0, 0 },
        { "findMSB", "i", "GLSL.std.450", GLSL450_FIND_S_MSB, { 1 }, 0, 0 },
        { "findMSB", "u", "GLSL.std.450", GLSL450_FIND_U_MSB, { 1 }, 0, 0 },
-       { "textureSize", "", "", OP_IMAGE_QUERY_SIZE_LOD, { 1, 2 }, CAP_IMAGE_QUERY, 0 },
+       { "textureSize", "", "", 0, { }, CAP_IMAGE_QUERY, &SpirVGenerator::visit_builtin_texture_query },
        { "texture", "", "", 0, { }, 0, &SpirVGenerator::visit_builtin_texture },
        { "textureLod", "", "", 0, { }, 0, &SpirVGenerator::visit_builtin_texture },
        { "texelFetch", "", "", 0, { }, 0, &SpirVGenerator::visit_builtin_texel_fetch },
@@ -1284,6 +1284,36 @@ void SpirVGenerator::visit_builtin_matrix_comp_mult(FunctionCall &call, const ve
        r_expression_result_id = write_construct(get_id(*call.type), column_ids, n_columns);
 }
 
+void SpirVGenerator::visit_builtin_texture_query(FunctionCall &call, const vector<Id> &argument_ids)
+{
+       if(argument_ids.size()<1)
+               throw internal_error("invalid texture query call");
+
+       Opcode opcode;
+       if(call.name=="textureSize")
+               opcode = OP_IMAGE_QUERY_SIZE_LOD;
+       else
+               throw internal_error("invalid texture query call");
+
+       ImageTypeDeclaration &image_arg0 = dynamic_cast<ImageTypeDeclaration &>(*call.arguments[0]->type);
+
+       Id image_id;
+       if(image_arg0.sampled)
+       {
+               Id image_type_id = get_item(image_type_ids, get_id(image_arg0));
+               image_id = write_expression(OP_IMAGE, image_type_id, argument_ids[0]);
+       }
+       else
+               image_id = argument_ids[0];
+
+       Id result_type_id = get_id(*call.type);
+       r_expression_result_id = begin_expression(opcode, result_type_id, argument_ids.size());
+       writer.write(image_id);
+       for(unsigned i=1; i<argument_ids.size(); ++i)
+               writer.write(argument_ids[i]);
+       end_expression(opcode);
+}
+
 void SpirVGenerator::visit_builtin_texture(FunctionCall &call, const vector<Id> &argument_ids)
 {
        if(argument_ids.size()<2)
@@ -1468,6 +1498,7 @@ void SpirVGenerator::visit(ImageTypeDeclaration &image)
        {
                writer.write_op_name(type_id, image.name);
                writer.write_op(content.globals, OP_TYPE_SAMPLED_IMAGE, type_id, image_id);
+               insert_unique(image_type_ids, type_id, image_id);
        }
 
        if(image.dimensions==ImageTypeDeclaration::ONE)
index c023adae9af3cc5e75a637298afc8f98fd244fc7..f18bf61c39443196795c2fdb9d80145e2e8d888c 100644 (file)
@@ -80,6 +80,7 @@ private:
        std::map<Node *, Declaration> declared_ids;
        std::map<std::string, Id> declared_uniform_ids;
        std::map<TypeKey, Id> standard_type_ids;
+       std::map<Id, Id> image_type_ids;
        std::map<TypeKey, Id> array_type_ids;
        std::map<TypeKey, Id> pointer_type_ids;
        std::map<std::string, Id> function_type_ids;
@@ -149,6 +150,7 @@ private:
        virtual void visit(FunctionCall &);
        void visit_constructor(FunctionCall &, const std::vector<Id> &, bool);
        void visit_builtin_matrix_comp_mult(FunctionCall &, const std::vector<Id> &);
+       void visit_builtin_texture_query(FunctionCall &, const std::vector<Id> &);
        void visit_builtin_texture(FunctionCall &, const std::vector<Id> &);
        void visit_builtin_texel_fetch(FunctionCall &, const std::vector<Id> &);
        void visit_builtin_interpolate(FunctionCall &, const std::vector<Id> &);
index 04d283490e790dd21421d4ef7ee2dda228ea16a5..3d424844e6d6975bf59e9013e08dc536a4a00a44 100644 (file)
@@ -64,6 +64,7 @@ enum SpirVOpcode
        OP_IMAGE_SAMPLE_DREF_IMPLICIT_LOD = 89,
        OP_IMAGE_SAMPLE_DREF_EXPLICIT_LOD = 89,
        OP_IMAGE_FETCH = 95,
+       OP_IMAGE = 100,
        OP_IMAGE_QUERY_SIZE_LOD = 103,
        OP_CONVERT_F_TO_U = 109,
        OP_CONVERT_F_TO_S = 110,