Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:53:36+00:00 2026-06-15T05:53:36+00:00

(How) can a scalar be cast to another scalar without conversion in GLSL? The

  • 0

(How) can a scalar be cast to another scalar without conversion in GLSL?

The definition of “cast” used in this question is changing the interpretation of the exact same data without changing the data itself, assuming the two types are of the same number of bits. i.e. (float)B00101111 == B00101111

The definition of “conversion” used in this question is changing both the interpretation of the data as well as reformatting said data to some mathematical approximation of the original value, i.e. (float)1 == 1.0

5.4.3 of GLSL 4.30.6 spec implies that scalar float-to-int and int-to-float constructors perform only conversion:
“When constructors are used to convert any floating-point type to an integer type, the fractional part of the
floating-point value is dropped.”
– pg. 87

Another nail in the coffin is with this line on section 5.1, pg. 85:
“There is no typecast operator; constructors are used instead.”

The goal is to use a Buffer-Texture/Texture-Buffer and texelFetch(…) to obtain float vec4s (128bit) and fill a struct such as:

struct MyStruct
{
int foo; //32bit
float bar; //32bit
int baz; //32bit
short blah; //16bit
short hmm; //16bit
} // 128bit total

…but there is trouble pinning down how to break apart that vec4 into the binary sub-chunks and re-cast them to set said struct values.

Would the following, in theory work?

MyStruct myUuberChunk = MyStruct(myVec4);

The rationale behind using float vec4s (or int vec4s for that matter), pending it is correct, is to get all values in one 128bit fetch for bandwidth performance purposes (most cards have four 32-bit memory buses).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:53:37+00:00Added an answer on June 15, 2026 at 5:53 am

    Would the following, in theory work?

    No.

    GLSL does not allow you to directly reinterpret structures. While GLSL (3.30+) does guarantee that integers and floats are 32-bits, you can’t just take a vec4 and pretend that it’s something else.

    However, OpenGL 3.3+ does allow you to reinterpret individual scalar values. First, your code should use an integer buffer texture, not a floating-point one. That’s just to prevent the texel fetch logic from doing something unpleasant in the case of denomralized values, NaN, or other floating-point oddities.

    Thus, you should be using a usamplerBuffer and getting back a uvec4.

    To get the actual values, you have to do bit reinterpretation, provided by appropriate library functions:

    struct Data
    {
      int first;
      float value;
      int second;
      int half1;
      int half2;
    };
    
    Data UnpackStruct(uvec4 packed)
    {
      Data ret;
      ret.first = int(packed[0]); //Sign bit is preserved, per GLSL standard.
      ret.value = uintBitsToFloat(packed[1]);
      ret.second = int(packed[2]);
      ret.half1 = (packed[3] >> 16) & 0x0000FFFF;
      ret.half2 = packed[3] & 0x0000FFFF;
      return ret;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Scala, how can I do something like this: def cast [Type] (x: _
Can you explain why this won't compile: (this is the error: ../Man.cpp:33:9: error: conversion
can anybody tell me the reason why I m getting this error: conversion to
In the QUERY select clause it states that you can use a scalar function
Can anyone help me in converting scalar type of openCV to basic types like
How can I pass the result from a scalar [single row, single value] query
How can I create a node in Scala without using literals? What I need
@uncheckedVariance can be used to bridge the gap between Scala's declaration site variance annotations
This question could easily take multiple paths, so I will hit the more specific
I have a recursive scalar function that needs to update a record in another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.