Mesa (master): i915g: Implement srgb textures the easy way.

2012-10-02 Thread Stephane Marchesin
Module: Mesa
Branch: master
Commit: fe3aeb7ea38770fce59b8261d8e2d37d4f68708f
URL:
http://cgit.freedesktop.org/mesa/mesa/commit/?id=fe3aeb7ea38770fce59b8261d8e2d37d4f68708f

Author: Stéphane Marchesin marc...@chromium.org
Date:   Mon Oct  1 01:57:33 2012 -0700

i915g: Implement srgb textures the easy way.

Since the hw can do it, let's use the hw. It's less accurate
but doesn't have the shader instruction count shortcomings.

---

 src/gallium/drivers/i915/i915_context.h   |2 -
 src/gallium/drivers/i915/i915_state_emit.c|  101 +
 src/gallium/drivers/i915/i915_state_sampler.c |8 +-
 3 files changed, 9 insertions(+), 102 deletions(-)

diff --git a/src/gallium/drivers/i915/i915_context.h 
b/src/gallium/drivers/i915/i915_context.h
index c9198e2..4141a3d 100644
--- a/src/gallium/drivers/i915/i915_context.h
+++ b/src/gallium/drivers/i915/i915_context.h
@@ -155,8 +155,6 @@ struct i915_state
unsigned sampler[I915_TEX_UNITS][3];
unsigned sampler_enable_flags;
unsigned sampler_enable_nr;
-   boolean sampler_srgb[I915_TEX_UNITS];
-   int srgb_const_offset;
 
/* texture image buffers */
unsigned texbuffer[I915_TEX_UNITS][2];
diff --git a/src/gallium/drivers/i915/i915_state_emit.c 
b/src/gallium/drivers/i915/i915_state_emit.c
index ac99979..590a1ba 100644
--- a/src/gallium/drivers/i915/i915_state_emit.c
+++ b/src/gallium/drivers/i915/i915_state_emit.c
@@ -314,41 +314,12 @@ emit_sampler(struct i915_context *i915)
}
 }
 
-static boolean is_tex_instruction(uint32_t* instruction)
-{
-   uint32_t op = instruction[0] 0xFF00;
-   return ( (op == T0_TEXLD) ||
-(op == T0_TEXLDP) ||
-(op == T0_TEXLDB));
-}
-
-static uint32_t tex_sampler(uint32_t* instruction)
-{
-   return ( instruction[0]  T0_SAMPLER_NR_MASK);
-}
-
-static uint additional_constants(struct i915_context *i915)
-{
-   int i;
-
-   for (i = 0 ; i  i915-fs-program_len; i+=3) {
-  if ( is_tex_instruction(i915-fs-program + i)) {
-   int sampler = tex_sampler(i915-fs-program + i);
-   assert(sampler  I915_TEX_UNITS);
-   if ( i915-current.sampler_srgb[sampler] )
-  return 1;
-  }
-   }
-   return 0;
-}
-
 static void
 validate_constants(struct i915_context *i915, unsigned *batch_space)
 {
int nr = i915-fs-num_constants ?
   2 + 4*i915-fs-num_constants : 0;
 
-   nr += 4*additional_constants(i915);
*batch_space = nr;
 }
 
@@ -358,11 +329,10 @@ emit_constants(struct i915_context *i915)
/* Collate the user-defined constants with the fragment shader's
 * immediates according to the constant_flags[] array.
 */
-   const uint nr = i915-fs-num_constants + additional_constants(i915);
+   const uint nr = i915-fs-num_constants;
 
assert(nr  I915_MAX_CONSTANT);
if (nr) {
-  const float srgb_constants[4] = {1.0/1.055, 0.055/1.055, 2.4, 0.0822};
   uint i;
 
   OUT_BATCH( _3DSTATE_PIXEL_SHADER_CONSTANTS | (nr * 4) );
@@ -375,16 +345,9 @@ emit_constants(struct i915_context *i915)
 c = (uint *) 
i915_buffer(i915-constants[PIPE_SHADER_FRAGMENT])-data;
 c += 4 * i;
  }
- else if (i  i915-fs-num_constants) {
+ else {
 /* emit program constant */
 c = (uint *) i915-fs-constants[i];
- } else {
-/* emit constants for sRGB */
-
-/* save const position in context for use in shader emit */
-i915-current.srgb_const_offset = i;
-
-c = (uint *) srgb_constants;
  }
 #if 0 /* debug */
  {
@@ -405,64 +368,14 @@ emit_constants(struct i915_context *i915)
 static void
 validate_program(struct i915_context *i915, unsigned *batch_space)
 {
-   uint additional_size = 0, i;
+   uint additional_size = 0;
 
additional_size += i915-current.target_fixup_format ? 3 : 0;
 
-   for (i = 0 ; i  i915-fs-program_len; i+=3)
-  if ( is_tex_instruction(i915-fs-program + i) 
-   i915-current.sampler_srgb[tex_sampler(i915-fs-program+i)] )
- additional_size += 3 * 8 /* 8 instructions for srgb emulation */;
-
-   /* we need more batch space if we want to emulate rgba framebuffers
-* or sRGB textures */
+   /* we need more batch space if we want to emulate rgba framebuffers */
*batch_space = i915-fs-decl_len + i915-fs-program_len + additional_size;
 }
 
-static void emit_instruction(struct i915_context *i915,
- int op,
- int dst_mask,
-int dst_reg,
- int src0_reg,
- int src1_reg,
- int src2_reg)
-{
-   OUT_BATCH(op |
- dst_mask |
- 0 | /* saturate */
- A0_DEST(dst_reg) |
- A0_SRC0(src0_reg)
- );
-   OUT_BATCH(A1_SRC0(src0_reg) | A1_SRC1(src1_reg));
-   OUT_BATCH(A2_SRC1(src1_reg) | A2_SRC2(src2_reg));
-}
-
-static void
-emit_srgb_fixup(struct 

Mesa (master): i915g: Implement sRGB textures

2012-06-27 Thread Stephane Marchesin
Module: Mesa
Branch: master
Commit: 45fc069600ddbfe07a0a0cd5280161a8c7c55dd0
URL:
http://cgit.freedesktop.org/mesa/mesa/commit/?id=45fc069600ddbfe07a0a0cd5280161a8c7c55dd0

Author: Stéphane Marchesin marc...@chromium.org
Date:   Mon Jun 25 19:45:56 2012 -0700

i915g: Implement sRGB textures

Since we don't have them in hw we emulate them in the shader. Although not
recommended by the spec it is legit.

As a side effect we also get GL 2.1. I think this is as far as we can take
the i915.

---

 src/gallium/drivers/i915/i915_context.h   |2 +
 src/gallium/drivers/i915/i915_screen.c|5 +-
 src/gallium/drivers/i915/i915_state_emit.c|  128 +++--
 src/gallium/drivers/i915/i915_state_sampler.c |3 +
 src/gallium/drivers/i915/i915_state_static.c  |2 +-
 5 files changed, 128 insertions(+), 12 deletions(-)

diff --git a/src/gallium/drivers/i915/i915_context.h 
b/src/gallium/drivers/i915/i915_context.h
index b019c9f..16b0c57 100644
--- a/src/gallium/drivers/i915/i915_context.h
+++ b/src/gallium/drivers/i915/i915_context.h
@@ -155,6 +155,8 @@ struct i915_state
unsigned sampler[I915_TEX_UNITS][3];
unsigned sampler_enable_flags;
unsigned sampler_enable_nr;
+   boolean sampler_srgb[I915_TEX_UNITS];
+   int srgb_const_offset;
 
/* texture image buffers */
unsigned texbuffer[I915_TEX_UNITS][2];
diff --git a/src/gallium/drivers/i915/i915_screen.c 
b/src/gallium/drivers/i915/i915_screen.c
index 9f29339..ff35203 100644
--- a/src/gallium/drivers/i915/i915_screen.c
+++ b/src/gallium/drivers/i915/i915_screen.c
@@ -110,7 +110,9 @@ i915_get_shader_param(struct pipe_screen *screen, unsigned 
shader, enum pipe_sha
 return PIPE_MAX_VERTEX_SAMPLERS;
  else
 return 0;
-  default:
+  case PIPE_SHADER_CAP_INTEGERS:
+ return 1;
+   default:
  return draw_get_shader_param(shader, cap);
   }
case PIPE_SHADER_FRAGMENT:
@@ -290,6 +292,7 @@ i915_is_format_supported(struct pipe_screen *screen,
 {
static const enum pipe_format tex_supported[] = {
   PIPE_FORMAT_B8G8R8A8_UNORM,
+  PIPE_FORMAT_B8G8R8A8_SRGB,
   PIPE_FORMAT_B8G8R8X8_UNORM,
   PIPE_FORMAT_R8G8B8A8_UNORM,
   PIPE_FORMAT_R8G8B8X8_UNORM,
diff --git a/src/gallium/drivers/i915/i915_state_emit.c 
b/src/gallium/drivers/i915/i915_state_emit.c
index 8ab8fb8..ac99979 100644
--- a/src/gallium/drivers/i915/i915_state_emit.c
+++ b/src/gallium/drivers/i915/i915_state_emit.c
@@ -30,6 +30,7 @@
 #include i915_context.h
 #include i915_batch.h
 #include i915_debug.h
+#include i915_fpc.h
 #include i915_resource.h
 
 #include pipe/p_context.h
@@ -313,11 +314,42 @@ emit_sampler(struct i915_context *i915)
}
 }
 
+static boolean is_tex_instruction(uint32_t* instruction)
+{
+   uint32_t op = instruction[0] 0xFF00;
+   return ( (op == T0_TEXLD) ||
+(op == T0_TEXLDP) ||
+(op == T0_TEXLDB));
+}
+
+static uint32_t tex_sampler(uint32_t* instruction)
+{
+   return ( instruction[0]  T0_SAMPLER_NR_MASK);
+}
+
+static uint additional_constants(struct i915_context *i915)
+{
+   int i;
+
+   for (i = 0 ; i  i915-fs-program_len; i+=3) {
+  if ( is_tex_instruction(i915-fs-program + i)) {
+   int sampler = tex_sampler(i915-fs-program + i);
+   assert(sampler  I915_TEX_UNITS);
+   if ( i915-current.sampler_srgb[sampler] )
+  return 1;
+  }
+   }
+   return 0;
+}
+
 static void
 validate_constants(struct i915_context *i915, unsigned *batch_space)
 {
-   *batch_space = i915-fs-num_constants ?
+   int nr = i915-fs-num_constants ?
   2 + 4*i915-fs-num_constants : 0;
+
+   nr += 4*additional_constants(i915);
+   *batch_space = nr;
 }
 
 static void
@@ -326,8 +358,11 @@ emit_constants(struct i915_context *i915)
/* Collate the user-defined constants with the fragment shader's
 * immediates according to the constant_flags[] array.
 */
-   const uint nr = i915-fs-num_constants;
+   const uint nr = i915-fs-num_constants + additional_constants(i915);
+
+   assert(nr  I915_MAX_CONSTANT);
if (nr) {
+  const float srgb_constants[4] = {1.0/1.055, 0.055/1.055, 2.4, 0.0822};
   uint i;
 
   OUT_BATCH( _3DSTATE_PIXEL_SHADER_CONSTANTS | (nr * 4) );
@@ -340,9 +375,16 @@ emit_constants(struct i915_context *i915)
 c = (uint *) 
i915_buffer(i915-constants[PIPE_SHADER_FRAGMENT])-data;
 c += 4 * i;
  }
- else {
+ else if (i  i915-fs-num_constants) {
 /* emit program constant */
 c = (uint *) i915-fs-constants[i];
+ } else {
+/* emit constants for sRGB */
+
+/* save const position in context for use in shader emit */
+i915-current.srgb_const_offset = i;
+
+c = (uint *) srgb_constants;
  }
 #if 0 /* debug */
  {
@@ -363,18 +405,74 @@ emit_constants(struct i915_context *i915)
 static void
 validate_program(struct i915_context *i915,