common.h raw

   1  // SPDX-License-Identifier: Unlicense OR MIT
   2  
   3  struct m3x2 {
   4  	vec3 r0;
   5  	vec3 r1;
   6  };
   7  
   8  // fboTextureTransform is the transformation
   9  // that cancels the implied transformation between
  10  // the framebuffer and its texture.
  11  // Only two rows are returned. The last is implied
  12  // to be [0, 0, 1].
  13  const m3x2 fboTextureTransform = m3x2(
  14  #ifdef HLSL
  15  	vec3(1.0, 0.0, 0.0),
  16  	vec3(0.0, -1.0, 1.0)
  17  #else
  18  	vec3(1.0, 0.0, 0.0),
  19  	vec3(0.0, 1.0, 0.0)
  20  #endif
  21  );
  22  
  23  // fboTransform is the transformation
  24  // that cancels the implied transformation between
  25  // the clip space and the framebuffer.
  26  // Only two rows are returned. The last is implied
  27  // to be [0, 0, 1].
  28  const m3x2 fboTransform = m3x2(
  29  #ifdef HLSL
  30  	vec3(1.0, 0.0, 0.0),
  31  	vec3(0.0, 1.0, 0.0)
  32  #else
  33  	vec3(1.0, 0.0, 0.0),
  34  	vec3(0.0, -1.0, 0.0)
  35  #endif
  36  );
  37  
  38  // toClipSpace converts an OpenGL gl_Position value to a
  39  // native GPU position.
  40  vec4 toClipSpace(vec4 pos) {
  41  #ifdef HLSL
  42  	// Map depths to the Direct3D [0; 1] range.
  43  	return vec4(pos.xy, (pos.z + pos.w)*.5, pos.w);
  44  #else
  45  	return pos;
  46  #endif
  47  }
  48  
  49  vec3 transform3x2(m3x2 t, vec3 v) {
  50  	return vec3(dot(t.r0, v), dot(t.r1, v), dot(vec3(0.0, 0.0, 1.0), v));
  51  }
  52