mesa.git
8 years agoradeonsi: work around an MSAA fast stencil clear problem
Nicolai Hähnle [Fri, 22 Apr 2016 22:28:46 +0000 (17:28 -0500)]
radeonsi: work around an MSAA fast stencil clear problem

A piglit test (arb_texture_multisample-stencil-clear) has been sent.
This problem was discovered analyzing

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=93767
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
8 years agoradeonsi: expclear must be disabled on first Z/S clear
Nicolai Hähnle [Fri, 22 Apr 2016 21:59:17 +0000 (16:59 -0500)]
radeonsi: expclear must be disabled on first Z/S clear

The documentation and the HW team say so.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
8 years agoradeonsi: move blend choice out of loop in si_blit_decompress_color
Nicolai Hähnle [Fri, 22 Apr 2016 20:28:47 +0000 (15:28 -0500)]
radeonsi: move blend choice out of loop in si_blit_decompress_color

It does not depend on the level or layer.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
8 years agoradeonsi: use level mask for early out in si_blit_decompress_color
Nicolai Hähnle [Fri, 22 Apr 2016 20:27:33 +0000 (15:27 -0500)]
radeonsi: use level mask for early out in si_blit_decompress_color

Mostly for consistency with the other decompress functions, but note that
in the non-DCC decompress case, the function can now early-out in slightly
more (albeit probably rare) cases.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
8 years agoradeonsi: si_blit_decompress_depth is only used for staging
Nicolai Hähnle [Fri, 22 Apr 2016 17:59:47 +0000 (12:59 -0500)]
radeonsi: si_blit_decompress_depth is only used for staging

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
8 years agoradeonsi: only decompress the required ZS planes from si_blit
Nicolai Hähnle [Fri, 22 Apr 2016 18:46:13 +0000 (13:46 -0500)]
radeonsi: only decompress the required ZS planes from si_blit

This happens to "fix" a rendering bug in KotOR2, because it avoids a still
not quite understood bug with MSAA fast stencil clear decompress. For the
stencil clear bug, I have sent a piglit test (arb_texture_multisample-stencil-clear).

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=93767
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
8 years agoradeonsi: decompress Z & S planes in one pass
Nicolai Hähnle [Fri, 22 Apr 2016 18:27:51 +0000 (13:27 -0500)]
radeonsi: decompress Z & S planes in one pass

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
8 years agoradeonsi: early out of si_blit_decompress_depth_in_place based on dirty mask
Nicolai Hähnle [Fri, 22 Apr 2016 17:55:15 +0000 (12:55 -0500)]
radeonsi: early out of si_blit_decompress_depth_in_place based on dirty mask

Avoid dirtying the db_render_state atom when possible.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
8 years agoradeonsi: use MIN2 instead of expanded ?: operator
Nicolai Hähnle [Fri, 22 Apr 2016 17:46:55 +0000 (12:46 -0500)]
radeonsi: use MIN2 instead of expanded ?: operator

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
8 years agoradeonsi: fix brace style
Nicolai Hähnle [Fri, 22 Apr 2016 20:30:37 +0000 (15:30 -0500)]
radeonsi: fix brace style

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
8 years agogallium/util: add u_bit_consecutive for generating a consecutive range of bits
Nicolai Hähnle [Fri, 22 Apr 2016 17:48:19 +0000 (12:48 -0500)]
gallium/util: add u_bit_consecutive for generating a consecutive range of bits

There are some undefined behavior subtleties, so having a function to match
the u_bit_scan_consecutive_range makes sense.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
8 years agoswr: s/Elements/ARRAY_SIZE/
Tim Rowley [Wed, 27 Apr 2016 15:12:44 +0000 (10:12 -0500)]
swr: s/Elements/ARRAY_SIZE/

Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
8 years agoradeonsi: emit s_waitcnt for shader memory barriers and volatile
Nicolai Hähnle [Sun, 17 Apr 2016 22:23:19 +0000 (17:23 -0500)]
radeonsi: emit s_waitcnt for shader memory barriers and volatile

Turns out that this is needed after all to satisfy some strengthened
coherency tests. Depends on support in LLVM, added in r267729.

v2: updated to reflect changes to the LLVM intrinsic

Reviewed-by: Marek Olšák <marek.olsak@amd.com> (v1)
8 years agoswr: [rasterizer] warning cleanup
Tim Rowley [Sat, 23 Apr 2016 01:41:23 +0000 (19:41 -0600)]
swr: [rasterizer] warning cleanup

Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
8 years agoswr: [rasterizer core] implement legacy depth bias enable
Tim Rowley [Mon, 18 Apr 2016 20:35:21 +0000 (14:35 -0600)]
swr: [rasterizer core] implement legacy depth bias enable

Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
8 years agoswr: [rasterizer jitter] support for dumping x86 asm
Tim Rowley [Thu, 21 Apr 2016 20:37:19 +0000 (14:37 -0600)]
swr: [rasterizer jitter] support for dumping x86 asm

Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
8 years agoswr: [rasterizer core] more backend refactoring
Tim Rowley [Thu, 21 Apr 2016 20:24:33 +0000 (14:24 -0600)]
swr: [rasterizer core] more backend refactoring

BackendPixelRate should be easier to read/maintain now hopefully.

Small perf bump by moving some of the pfn's to inline functions
without template params.

Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
8 years agoswr: [rasterizer jitter] add mSimdInt1Ty
Tim Rowley [Wed, 20 Apr 2016 21:57:52 +0000 (15:57 -0600)]
swr: [rasterizer jitter] add mSimdInt1Ty

Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
8 years agoswr: [rasterizer core] backend refactor
Tim Rowley [Thu, 14 Apr 2016 23:03:16 +0000 (17:03 -0600)]
swr: [rasterizer core] backend refactor

Lump all template args into a bundle of traits, and add some
functionality to the MSAA traits.

Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
8 years agosvga: use the SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_INSTRUCTIONS query
Brian Paul [Mon, 25 Apr 2016 23:12:50 +0000 (17:12 -0600)]
svga: use the SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_INSTRUCTIONS query

Instead of a hard-coded 512.  The query typically returns 65536 now.
Fall back to 512 if the query fails as we do for vertex shaders (which
should never happen).

Note that we don't actually enforce this limit in our shaders but it gets
reported via the glGetProgramivARB(GL_MAX_PROGRAM_INSTRUCTIONS_ARB) query.

Reviewed-by: Charmaine Lee <charmainel@vmware.com>
8 years agonouveau: codegen: LOAD: Take src swizzle into account
Hans de Goede [Thu, 31 Mar 2016 06:53:40 +0000 (08:53 +0200)]
nouveau: codegen: LOAD: Take src swizzle into account

The llvm TGSI backend uses pointers in registers and does things
like:

LOAD TEMP[0].y, MEMORY[0], TEMP[0]

Expecting the data at address TEMP[0].x to get loaded to
TEMP[0].y. But this will cause the data at TEMP[0].x + 4 to be
loaded instead.

This commit adds support for a swizzle suffix for the 1st source
operand, which allows using:

LOAD TEMP[0].y, MEMORY[0].xxxx, TEMP[0]

And actually getting the desired behavior

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Reviewed-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonouveau: codegen: LOAD: Do not call fetchSrc(1) if the address is immediate
Hans de Goede [Thu, 21 Apr 2016 11:51:14 +0000 (13:51 +0200)]
nouveau: codegen: LOAD: Do not call fetchSrc(1) if the address is immediate

"off" later gets set to NULL when the address is immediate, so move the
fetchSrc(1) call to the non-immediate branch of the if-else. This brings
handleLOAD's offset handling inline with how it is done in handleSTORE.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Reviewed-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonouveau: codegen: LOAD: Always use component 0 when getting the address
Hans de Goede [Thu, 21 Apr 2016 11:31:01 +0000 (13:31 +0200)]
nouveau: codegen: LOAD: Always use component 0 when getting the address

LOAD loads upto 4 components from the specified resource starting at
the passed in x value of the 2nd source operand, the y, z and w
components of the address should not be used.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Reviewed-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agodri3: Check for dummyContext to see if the glx_context is valid
Stefan Dirsch [Mon, 25 Apr 2016 13:06:25 +0000 (15:06 +0200)]
dri3: Check for dummyContext to see if the glx_context is valid

According to the comments in src/glx/glxcurrent.c __glXGetCurrentContext()
always returns a valid pointer. If no context is made current, it will
contain dummyContext. Thus a test for NULL will always fail.

https://lists.freedesktop.org/archives/mesa-dev/2016-April/113962.html

Signed-off-by: Stefan Dirsch <sndirsch@suse.de>
Reviewed-by: Egbert Eich <eich@freedesktop.org>
Reviewed-by: Emil Velikov <emil.velikov@collabora.com>
8 years agodri2: Check for dummyContext to see if the glx_context is valid
Egbert Eich [Thu, 21 Apr 2016 13:02:53 +0000 (15:02 +0200)]
dri2: Check for dummyContext to see if the glx_context is valid

According to the comments in src/glx/glxcurrent.c __glXGetCurrentContext()
always returns a valid pointer. If no context is made current, it will
contain dummyContext. Thus a test for NULL will always fail.

https://bugzilla.opensuse.org/show_bug.cgi?id=962609

Tested-by: Olaf Hering <ohering@suse.com>
Reviewed-by: Michel Dänzer <michel.daenzer@amd.com>
Reviewed-by: Emil Velikov <emil.velikov@collabora.com>
8 years agoglsl: move uniform block validation to link_uniform_blocks.cpp
Timothy Arceri [Wed, 27 Apr 2016 03:20:45 +0000 (13:20 +1000)]
glsl: move uniform block validation to link_uniform_blocks.cpp

Reviewed-by: Eduardo Lima Mitev <elima@igalia.com>
8 years agodocs: Mention that {ARB,OES}_texture_stencil8 is supported on i965/gen8+
Kenneth Graunke [Tue, 26 Apr 2016 17:41:21 +0000 (10:41 -0700)]
docs: Mention that {ARB,OES}_texture_stencil8 is supported on i965/gen8+

Thanks to Thomas Helland for reminding me to do this.

8 years agoi965: Enable ARB_texture_stencil8 and OES_texture_stencil8 on Gen8+.
Kenneth Graunke [Thu, 3 Mar 2016 09:58:13 +0000 (01:58 -0800)]
i965: Enable ARB_texture_stencil8 and OES_texture_stencil8 on Gen8+.

Stencil texturing is required by ES 3.1.  Apparently we never actually
turned it on.  Do that now.  Also turn on the desktop extension.

Fixes nine dEQP-GLES31.functional tests:

stencil_texturing.format.stencil_index8_2d
texture.border_clamp.formats.stencil_index8.nearest_size_pot
texture.border_clamp.formats.stencil_index8.nearest_size_npot
texture.border_clamp.formats.stencil_index8.gather_size_pot
texture.border_clamp.formats.stencil_index8.gather_size_npot
texture.border_clamp.unused_channels.stencil_index8
state_query.internal_format.renderbuffer.stencil_index8_samples
state_query.internal_format.texture_2d_multisample.stencil_index8_samples
state_query.internal_format.texture_2d_multisample_array.stencil_index8_samples

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Chris Forbes <chrisf@ijw.co.nz>
8 years agomesa: Try to fix CopyTex[Sub]Image of stencil textures.
Kenneth Graunke [Tue, 26 Apr 2016 09:31:20 +0000 (02:31 -0700)]
mesa: Try to fix CopyTex[Sub]Image of stencil textures.

ES prohibits this, but GL appears to allow it.  We at least need this
much, or else we'll crash as there's no source to read from.

This fixed crashes in the ES tests before I realized I needed to
prohibit stencil instead.

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Brian Paul <brianp@vmware.com>
Reviewed-by: Chris Forbes <chrisf@ijw.co.nz>
8 years agomesa: Disallow CopyTexSubImage on stencil formats in ES.
Kenneth Graunke [Tue, 26 Apr 2016 09:29:44 +0000 (02:29 -0700)]
mesa: Disallow CopyTexSubImage on stencil formats in ES.

Fixes
- ES31-CTS.gtf.GL31Tests.texture_stencil8.texture_stencil8
- ES31-CTS.gtf.GL31Tests.texture_stencil8.texture_stencil8_multisample

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Brian Paul <brianp@vmware.com>
Reviewed-by: Chris Forbes <chrisf@ijw.co.nz>
8 years agoi965: Fix MapTextureImage for multi-slice/level stencil buffers.
Kenneth Graunke [Tue, 26 Apr 2016 08:44:51 +0000 (01:44 -0700)]
i965: Fix MapTextureImage for multi-slice/level stencil buffers.

We called intel_miptree_get_image_offset() to get the image offsets
for the current level/slice, but then proceeded to ignore the results
and clobber level/slice 0 every time.

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=94713
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Chris Forbes <chrisf@ijw.co.nz>
8 years agoi965: Move TCS output indirect_offset.file check out a level.
Kenneth Graunke [Sat, 9 Apr 2016 08:27:01 +0000 (01:27 -0700)]
i965: Move TCS output indirect_offset.file check out a level.

I want to add another condition.  Moving the indirect_offset.file
check out a level should make this a little easier.

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
8 years agoi965/fs: Reduce the response length of sampler messages on Skylake.
Kenneth Graunke [Sat, 23 Apr 2016 08:54:33 +0000 (01:54 -0700)]
i965/fs: Reduce the response length of sampler messages on Skylake.

Often, we don't need a full 4 channels worth of data from the sampler.
For example, depth comparisons and red textures only return one value.
To handle this, the sampler message header contains a mask which can
be used to disable channels, and reduce the message length (in SIMD16
mode on all hardware, and SIMD8 mode on Broadwell and later).

We've never used it before, since it required setting up a message
header.  This meant trading a smaller response length for a larger
message length and additional MOVs to set it up.

However, Skylake introduces a terrific new feature: for headerless
messages, you can simply reduce the response length, and it makes
the implicit header contain an appropriate mask.  So to read only
RG, you would simply set the message length to 2 or 4 (SIMD8/16).

This means we can finally take advantage of this at no cost.

total instructions in shared programs: 9091831 -> 9073067 (-0.21%)
instructions in affected programs: 191370 -> 172606 (-9.81%)
helped: 2609
HURT: 0

total cycles in shared programs: 70868114 -> 68454752 (-3.41%)
cycles in affected programs: 35841154 -> 33427792 (-6.73%)
helped: 16357
HURT: 8188

total spills in shared programs: 3492 -> 1707 (-51.12%)
spills in affected programs: 2749 -> 964 (-64.93%)
helped: 74
HURT: 0

total fills in shared programs: 4266 -> 2647 (-37.95%)
fills in affected programs: 3029 -> 1410 (-53.45%)
helped: 74
HURT: 0

LOST:   1
GAINED: 143

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
8 years agonir: Add a helper for figuring out what channels of an SSA def are read
Jason Ekstrand [Fri, 9 Oct 2015 15:13:43 +0000 (08:13 -0700)]
nir: Add a helper for figuring out what channels of an SSA def are read

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
8 years agoi965/fs: Use inst->regs_written for rlen for texture instructions
Jason Ekstrand [Fri, 9 Oct 2015 18:24:35 +0000 (11:24 -0700)]
i965/fs: Use inst->regs_written for rlen for texture instructions

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
8 years agoi965/fs: Properly report regs_written from SAMPLEINFO
Jason Ekstrand [Sat, 10 Oct 2015 01:07:23 +0000 (18:07 -0700)]
i965/fs: Properly report regs_written from SAMPLEINFO

The previous behavior would only allocate one register and then write
four thus potentially stomping three innocent bystanders.

Cc: mesa-stable@lists.freedesktop.org
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
8 years agoi965/blorp: Set regs_written on texturing instructions
Jason Ekstrand [Mon, 12 Oct 2015 21:04:05 +0000 (14:04 -0700)]
i965/blorp: Set regs_written on texturing instructions

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
8 years agoi965: Don't force a header for texture offsets of 0.
Kenneth Graunke [Sat, 23 Apr 2016 22:50:39 +0000 (15:50 -0700)]
i965: Don't force a header for texture offsets of 0.

Calling textureOffset() with an offset of <0, 0, 0> is equivalent to
calliing texture().  We don't actually need to set up an offset,
which causes a message header to be created.

A fairly common pattern is to sample at a point with a bunch of
offsets, and average them.  It's natural to write all the lookups
as textureOffset, but use <0, 0> for the center sample.

shader-db results on Skylake:

total instructions in shared programs: 9092095 -> 9092087 (-0.00%)
instructions in affected programs: 2826 -> 2818 (-0.28%)
helped: 12
HURT: 2

total cycles in shared programs: 70870166 -> 70870144 (-0.00%)
cycles in affected programs: 15924 -> 15902 (-0.14%)
helped: 2
HURT: 0

This also helps prevent code quality regressions in a future patch.

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by Jason Ekstrand <jason@jlekstrand.net>

8 years agor600g: fix and optimize tgsi_cmp when using ABS and NEG modifier
Patrick Rudolph [Mon, 28 Mar 2016 09:52:00 +0000 (11:52 +0200)]
r600g: fix and optimize tgsi_cmp when using ABS and NEG modifier

Some apps set NEG and ABS on the source param to test for zero.
Use ALU_OP3_CNDE insted of ALU_OP3_CNDGE and unset both modifiers.

It also removes the need for a MOV instruction, as ABS isn't
supported on op3.

Tested on AMD CAYMAN and AMD RV770.

Signed-off-by: Patrick Rudolph <siro@das-labor.org>
Cc: mesa-stable@lists.freedesktop.org
Signed-off-by: Dave Airlie <airlied@redhat.com>
8 years agodocs: update softpipe for ARB_compute_shader
Dave Airlie [Tue, 26 Apr 2016 22:52:32 +0000 (08:52 +1000)]
docs: update softpipe for ARB_compute_shader

Signed-off-by: Dave Airlie <airlied@redhat.com>
8 years agosoftpipe: add support for compute shaders. (v2)
Dave Airlie [Tue, 26 Apr 2016 04:32:52 +0000 (14:32 +1000)]
softpipe: add support for compute shaders. (v2)

This enables ARB_compute_shader on softpipe. I've only
tested this with piglit so far, and I hopefully plan
on integrating it with my vulkan work. I'll get to
testing it with deqp more later.

The basic premise is to create up to 1024 restartable
TGSI machines, and execute workgroups of those machines.

v1.1: free machines.
v2: deqp fixes - add samplers support, finish
atomic operations, fix load/store writemasks.

Acked-by: Roland Scheidegger <sroland@vmware.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
8 years agotgsi/exec: initialise SysSemanticToIndex array to -1
Dave Airlie [Tue, 26 Apr 2016 04:31:24 +0000 (14:31 +1000)]
tgsi/exec: initialise SysSemanticToIndex array to -1

We want to use the SysSemanticToIndex to tell if we've seen
the semantics at all.

Acked-by: Roland Scheidegger <sroland@vmware.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
8 years agotgsi/exec: implement restartable machine.
Dave Airlie [Tue, 26 Apr 2016 04:30:31 +0000 (14:30 +1000)]
tgsi/exec: implement restartable machine.

This lets us restart the machine at a PC value, and exits
the machine when we hit a barrier.

Compute shaders will then execute all the threads up to the
barrier, then restart the machines after the barrier once
all are done.

v2: comment the code a bit, change return types.

Acked-by: Roland Scheidegger <sroland@vmware.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
8 years agotgsi/exec: make inputs/outputs optional for compute shaders.
Dave Airlie [Tue, 26 Apr 2016 04:28:56 +0000 (14:28 +1000)]
tgsi/exec: make inputs/outputs optional for compute shaders.

compute shaders don't need input/outputs so don't bother
allocating memory for these.

Acked-by: Roland Scheidegger <sroland@vmware.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
8 years agotgsi/exec: implement load/store/atomic on MEMORY.
Dave Airlie [Tue, 26 Apr 2016 04:26:20 +0000 (14:26 +1000)]
tgsi/exec: implement load/store/atomic on MEMORY.

This implements basic load/store/atomic ops on MEMORY types
for compute shaders.

Acked-by: Roland Scheidegger <sroland@vmware.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
8 years agotgsi/exec: split out setting up masks to separate function
Dave Airlie [Tue, 26 Apr 2016 04:24:53 +0000 (14:24 +1000)]
tgsi/exec: split out setting up masks to separate function

This is just a cleanup that will make later changes easier
to make.

Acked-by: Roland Scheidegger <sroland@vmware.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
8 years agotgsi: accept a starting PC value for exec machine.
Dave Airlie [Tue, 26 Apr 2016 04:19:36 +0000 (14:19 +1000)]
tgsi: accept a starting PC value for exec machine.

This will be used later to restart barriered execution
threads in compute, for now we just want to change the API.

Acked-by: Roland Scheidegger <sroland@vmware.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
8 years agotgsi: move to using vector for system values.
Dave Airlie [Mon, 25 Apr 2016 23:48:46 +0000 (09:48 +1000)]
tgsi: move to using vector for system values.

For compute support some of the system values are .xyz types,
so move to using a vector instead of a single channel.

[airlied: squash swizzle fix from compute series].

Reviewed-by: Brian Paul <brianp@vmware.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
8 years agotgsi/exec: fix system value handling.
Dave Airlie [Tue, 26 Apr 2016 01:14:24 +0000 (11:14 +1000)]
tgsi/exec: fix system value handling.

a) SysSemanticToIndex needs to be indexed with the semantic name
not the decl->Declaration.Semantic.

b) doing this in run is too late, as the mappings are all setup
prior to run in the execs.

Reviewed-by: Brian Paul <brianp@vmware.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
8 years agoi965/blorp: Convert state setup to C
Jason Ekstrand [Fri, 22 Apr 2016 21:48:36 +0000 (14:48 -0700)]
i965/blorp: Convert state setup to C

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Make state setup C-safe
Jason Ekstrand [Fri, 22 Apr 2016 21:51:05 +0000 (14:51 -0700)]
i965/blorp: Make state setup C-safe

Previously they (very rarely) used C++isms that prevented them from being
compiled as C.  As of this commit, they can be compiled as either C or C++.

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Convert brw_blorp.cpp to a C file
Jason Ekstrand [Fri, 22 Apr 2016 23:04:05 +0000 (16:04 -0700)]
i965/blorp: Convert brw_blorp.cpp to a C file

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Make all of brw_blorp.h accessible to C
Jason Ekstrand [Fri, 22 Apr 2016 21:39:50 +0000 (14:39 -0700)]
i965/blorp: Make all of brw_blorp.h accessible to C

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Turn brw_blorp_params into a C-style struct
Jason Ekstrand [Fri, 22 Apr 2016 21:32:48 +0000 (14:32 -0700)]
i965/blorp: Turn brw_blorp_params into a C-style struct

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Turn coord_transform into a C-style struct
Jason Ekstrand [Fri, 22 Apr 2016 01:10:53 +0000 (18:10 -0700)]
i965/blorp: Turn coord_transform into a C-style struct

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Turn blorp_surface_info into a C-style struct
Jason Ekstrand [Thu, 21 Apr 2016 23:39:56 +0000 (16:39 -0700)]
i965/blorp: Turn blorp_surface_info into a C-style struct

This commit is mostly mechanical except that it changes where we set the
swizzle.  Previously, the blorp_surface_info constructor defaulted the
swizzle to SWIZZLE_XYZW.  Now, we memset to zero and fill out the swizzle
when we setup the rest of the struct.

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Roll mip_info into surface_info
Jason Ekstrand [Thu, 21 Apr 2016 23:19:51 +0000 (16:19 -0700)]
i965/blorp: Roll mip_info into surface_info

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Get rid of the blorp_blit_params class
Jason Ekstrand [Fri, 22 Apr 2016 21:06:08 +0000 (14:06 -0700)]
i965/blorp: Get rid of the blorp_blit_params class

It was really just a wrapper around the function that constructed it.

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Remove the hiz params class
Jason Ekstrand [Fri, 22 Apr 2016 20:46:25 +0000 (13:46 -0700)]
i965/blorp: Remove the hiz params class

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Remove the clear params classes
Jason Ekstrand [Fri, 22 Apr 2016 19:55:49 +0000 (12:55 -0700)]
i965/blorp: Remove the clear params classes

They didn't really add anything other than a key and extra layers of
function calls.  This commit just inlines the extra functions and gets rid
of the extra classes.

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Remove the arguments to brw_blorp_params()
Jason Ekstrand [Fri, 22 Apr 2016 20:52:30 +0000 (13:52 -0700)]
i965/blorp: Remove the arguments to brw_blorp_params()

No one was using anything other than the defaults.

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoi965/blorp: Refactor to get rid of the get_wm_prog virtual function
Jason Ekstrand [Fri, 22 Apr 2016 18:38:23 +0000 (11:38 -0700)]
i965/blorp: Refactor to get rid of the get_wm_prog virtual function

Instead of having a virtual member function for getting the WM/PS kernel,
we simply add fields for prog_data and the kernel to brw_blorp_parms and
always make sure those get set as part of the different constructors.

v2: Use use prog_data != NULL to check for a valid program instead of a
    magic kernel offset value

Reviewed-by: Topi Pohjolainen <topi.pohjolainen@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
8 years agoswr: autogenerate swr_context_llvm.h
Tim Rowley [Tue, 26 Apr 2016 16:22:24 +0000 (11:22 -0500)]
swr: autogenerate swr_context_llvm.h

Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
8 years agoanv: honor DESTDIR when installing icd file
Laurent Carlier [Sun, 17 Apr 2016 09:38:53 +0000 (11:38 +0200)]
anv: honor DESTDIR when installing icd file

https://bugs.freedesktop.org/show_bug.cgi?id=94969

Reviewed-by: Chad Versace <chad.versace@intel.com>
8 years agoi965/meta: initialize values to avoid random behaviour on error path
Juha-Pekka Heikkila [Sat, 16 Apr 2016 19:26:23 +0000 (22:26 +0300)]
i965/meta: initialize values to avoid random behaviour on error path

if brw_meta_stencil_blit() errored at wrong place 'target' would
be uninitialized and cause random behaviour on leaving the funtion.

Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com>
Reviewed-by: Eduardo Lima Mitev <elima@igalia.com>
Reviewed-by: Chad Versace <chad.versace@intel.com>
8 years agometa: Avoid random memory access on error
Juha-Pekka Heikkila [Sat, 16 Apr 2016 19:26:22 +0000 (22:26 +0300)]
meta: Avoid random memory access on error

Initialize drawFb to NULL in _mesa_meta_CopyImageSubData_uncompressed()
if getting readFb fails uninitialized drawFb will cause randomness
on cleanup.

Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com>
Reviewed-by: Eduardo Lima Mitev <elima@igalia.com>
Reviewed-by: Chad Versace <chad.versace@intel.com>
8 years agomesa: add tags file to gitignore
Grazvydas Ignotas [Fri, 15 Apr 2016 22:50:12 +0000 (01:50 +0300)]
mesa: add tags file to gitignore

For ctags users like me.

Signed-off-by: Grazvydas Ignotas <notasas@gmail.com>
Reviewed-by: Chad Versace <chad.versace@intel.com>
8 years agomesa: Remove every double semi-colon
Jakob Sinclair [Thu, 14 Apr 2016 16:07:42 +0000 (18:07 +0200)]
mesa: Remove every double semi-colon

Signed-off-by: Jakob Sinclair <sinclair.jakob@openmailbox.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Chad Versace <chad.versace@intel.com>
8 years agoglx: Remove every double semi-colon
Jakob Sinclair [Thu, 14 Apr 2016 16:07:41 +0000 (18:07 +0200)]
glx: Remove every double semi-colon

Signed-off-by: Jakob Sinclair <sinclair.jakob@openmailbox.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Chad Versace <chad.versace@intel.com>
8 years agogallium: Remove every double semi-colon
Jakob Sinclair [Thu, 14 Apr 2016 16:07:40 +0000 (18:07 +0200)]
gallium: Remove every double semi-colon

Signed-off-by: Jakob Sinclair <sinclair.jakob@openmailbox.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Chad Versace <chad.versace@intel.com>
8 years agoegl: Remove every double semi-colon
Jakob Sinclair [Thu, 14 Apr 2016 16:07:39 +0000 (18:07 +0200)]
egl: Remove every double semi-colon

Removes all accidental semi-colons in egl.

Signed-off-by: Jakob Sinclair <sinclair.jakob@openmailbox.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Chad Versace <chad.versace@intel.com>
8 years agogallium/r600: removing double semi-colons
Jakob Sinclair [Wed, 13 Apr 2016 16:43:11 +0000 (18:43 +0200)]
gallium/r600: removing double semi-colons

Trivial change. Removing unnecessary semi-colons from the code.
I don't have push access so someone reviewing this can push it.

Signed-off-by: Jakob Sinclair <sinclair.jakob@openmailbox.org>
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
Reviewed-by: Edward O'Callaghan <eocallaghan@alterapraxis.com>
Reviewed-by: Chad Versace <chad.versace@intel.com>
8 years agomesa/main: removing double semi-colons
Jakob Sinclair [Wed, 13 Apr 2016 16:43:10 +0000 (18:43 +0200)]
mesa/main: removing double semi-colons

Trivial change. Removing unnecessary semi-colons from the code.
I don't have push access so someone reviewing this can push it.

Signed-off-by: Jakob Sinclair <sinclair.jakob@openmailbox.org>
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
Reviewed-by: Edward O'Callaghan <eocallaghan@alterapraxis.com>
Reviewed-by: Chad Versace <chad.versace@intel.com>
8 years agoglsl: removing double semi-colons
Jakob Sinclair [Wed, 13 Apr 2016 16:43:09 +0000 (18:43 +0200)]
glsl: removing double semi-colons

Trivial change. Removing unnecessary semi-colons from the code.
I don't have push access so someone reviewing this can push it.

Signed-off-by: Jakob Sinclair <sinclair.jakob@openmailbox.org>
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
Reviewed-by: Edward O'Callaghan <eocallaghan@alterapraxis.com>
Reviewed-by: Chad Versace <chad.versace@intel.com>
8 years agoglx: Don't enclose includes inside `extern "C" { }`.
Jose Fonseca [Tue, 26 Apr 2016 18:48:12 +0000 (19:48 +0100)]
glx: Don't enclose includes inside `extern "C" { }`.

Ran `make check` inside src/glx to verify everything compiles and links
correctly.

https://bugs.freedesktop.org/show_bug.cgi?id=95158

Reviewed-by: Brian Paul <brianp@vmware.com>
8 years agoradeonsi: add RW_BUFFERS only once in si_ce_needed_cs_space
Marek Olšák [Fri, 22 Apr 2016 07:38:03 +0000 (09:38 +0200)]
radeonsi: add RW_BUFFERS only once in si_ce_needed_cs_space

Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
8 years agoegl: fix make check broken by interop support
Marek Olšák [Fri, 22 Apr 2016 07:00:29 +0000 (09:00 +0200)]
egl: fix make check broken by interop support

8 years agodocs: mark ARB_compute_shader as done for nvc0
Samuel Pitoiset [Tue, 26 Apr 2016 19:02:16 +0000 (21:02 +0200)]
docs: mark ARB_compute_shader as done for nvc0

This has been merged few months ago but this should help
https://mesamatrix.net/ to update its list of supported extensions.

Please note that compute shaders are not really useful without
ARB_image_load_store and only GK104 and GK110 support it for now.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
8 years agonvc0: expose GLSL version 420 on GK110
Samuel Pitoiset [Mon, 18 Apr 2016 16:55:22 +0000 (18:55 +0200)]
nvc0: expose GLSL version 420 on GK110

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonvc0: enable ARB_shader_image_load_store on GK110
Samuel Pitoiset [Mon, 18 Apr 2016 16:54:14 +0000 (18:54 +0200)]
nvc0: enable ARB_shader_image_load_store on GK110

This exposes 8 images for all shader types.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agogk110/ir: add emission for VSHL
Samuel Pitoiset [Tue, 19 Apr 2016 23:04:28 +0000 (01:04 +0200)]
gk110/ir: add emission for VSHL

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agogk110/ir: add emission for OP_SUEAU, OP_SUBFM and OP_SUCLAMP
Samuel Pitoiset [Mon, 18 Apr 2016 23:37:44 +0000 (01:37 +0200)]
gk110/ir: add emission for OP_SUEAU, OP_SUBFM and OP_SUCLAMP

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agogk110/ir: add emission for OP_SULDB and OP_SUSTx
Samuel Pitoiset [Mon, 18 Apr 2016 16:53:37 +0000 (18:53 +0200)]
gk110/ir: add emission for OP_SULDB and OP_SUSTx

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agogk110/ir: add emission for OP_MADSP
Samuel Pitoiset [Mon, 18 Apr 2016 16:50:24 +0000 (18:50 +0200)]
gk110/ir: add emission for OP_MADSP

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agogk110/ir: add emission for OP_PERMT
Samuel Pitoiset [Mon, 18 Apr 2016 16:48:52 +0000 (18:48 +0200)]
gk110/ir: add emission for OP_PERMT

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonvc0: expose GLSL version 420 on GK104
Samuel Pitoiset [Fri, 8 Apr 2016 18:24:58 +0000 (20:24 +0200)]
nvc0: expose GLSL version 420 on GK104

Other chipsets will be added later.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonvc0: enable ARB_shader_image_load_store on GK104
Ilia Mirkin [Sat, 9 Jan 2016 04:06:25 +0000 (23:06 -0500)]
nvc0: enable ARB_shader_image_load_store on GK104

This exposes 8 images for all shader types.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonvc0: inform users that 3D images are not fully supported
Samuel Pitoiset [Sat, 9 Apr 2016 18:32:25 +0000 (20:32 +0200)]
nvc0: inform users that 3D images are not fully supported

3D images are a bit more complicated to implement and will probably
requires a bunch of headaches and we don't care for now because they
do not seem to be really used by apps.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonvc0: reduce GL_MAX_3D_TEXTURE_SIZE to 2048 on Kepler+
Samuel Pitoiset [Mon, 11 Apr 2016 14:58:57 +0000 (16:58 +0200)]
nvc0: reduce GL_MAX_3D_TEXTURE_SIZE to 2048 on Kepler+

The blob sets it to 2048 and using 4096 reports an INVALID_DATA error
with RT_ARRAY_MODE when z is 4096. Suggested by Ilia Mirkin.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
Cc: "11.1 11.2" <mesa-stable@lists.freedesktop.org>
8 years agonvc0/ir: check that the image format doesn't mismatch
Samuel Pitoiset [Mon, 25 Apr 2016 21:13:00 +0000 (23:13 +0200)]
nvc0/ir: check that the image format doesn't mismatch

This re-uses NVE4_SU_INFO_CALL which is not used anymore because we
don't use our lib for format conversions. While we are at it, add a
todo for image buffers because there are some robustness-related
issues to fix.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonvc0/ir: prevent out of bounds when no images are bound
Samuel Pitoiset [Tue, 12 Apr 2016 15:12:00 +0000 (17:12 +0200)]
nvc0/ir: prevent out of bounds when no images are bound

Checking if the image address is not 0 should be enough to prevent
read faults. To improve robustness, make sure that the destination
value of atomic operations is correctly initialized in case the
instruction is not performed.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonvc0/ir: add indirect support for images on Kepler
Samuel Pitoiset [Wed, 6 Apr 2016 15:15:35 +0000 (17:15 +0200)]
nvc0/ir: add indirect support for images on Kepler

This fixes arb_shader_image_load_store-indexing and
arb_shader_image_load_store-max-images.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonvc0/ir: fix 1D arrays images for Kepler
Samuel Pitoiset [Tue, 5 Apr 2016 10:48:32 +0000 (12:48 +0200)]
nvc0/ir: fix 1D arrays images for Kepler

For 1D arrays, the array index is stored in the Z component.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonvc0/ir: fix cube images for Kepler
Samuel Pitoiset [Sat, 2 Apr 2016 10:58:46 +0000 (12:58 +0200)]
nvc0/ir: fix cube images for Kepler

Like 2d array images, the z-dimension needs to be clamped.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonv50/ir: add support for SULDP -> SULDB conversion
Ilia Mirkin [Tue, 5 Apr 2016 14:00:56 +0000 (16:00 +0200)]
nv50/ir: add support for SULDP -> SULDB conversion

This will allow to convert surface formats without adding an extra
call to our lib.

[hakzsam: make use of this for GK104]

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Signed-off-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonv50/ir: make use of OP_SUQ for surfaces query
Samuel Pitoiset [Sat, 9 Apr 2016 15:10:30 +0000 (17:10 +0200)]
nv50/ir: make use of OP_SUQ for surfaces query

This implements RESQ for surfaces which comes from imageSize() GLSL
bultin. As the dimensions are sticked into the driver constant buffer,
this only has to be lowered with loads.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu> (v2)
8 years agonv50/ir: add OP_BUFQ for buffers query
Samuel Pitoiset [Sat, 9 Apr 2016 15:08:56 +0000 (17:08 +0200)]
nv50/ir: add OP_BUFQ for buffers query

TGSI RESQ allows both images and buffers but we have to make a
distinction between these two type of resources in our lowering pass.
Introducing OP_BUFQ which is a fake operand will allow to implement
OP_SUQ for surfaces.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonv50/ir: enable early fragment test with explicit user control
Samuel Pitoiset [Sun, 27 Mar 2016 20:47:37 +0000 (22:47 +0200)]
nv50/ir: enable early fragment test with explicit user control

This feature can be enabled in two ways: as an optimization and by
explicit user control (with OpenGL 4.2 or ARB_shader_image_load_store).

This makes use of the recent TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL to
force early fragment tests when needed.

This fixes a bunch of
dEQP-GLES31.functional.image_load_store.early_fragment_tests.* tests.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonvc0/ir: fix constraints for OP_SUSTx on Kepler
Samuel Pitoiset [Tue, 5 Apr 2016 10:39:12 +0000 (12:39 +0200)]
nvc0/ir: fix constraints for OP_SUSTx on Kepler

Destination type is actually always 32-bits, so typeSizeof() returns 4
and no sources are condensed.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>
8 years agonv50/ir: re-introduce TGSI lowering pass for images
Samuel Pitoiset [Tue, 5 Apr 2016 13:22:00 +0000 (15:22 +0200)]
nv50/ir: re-introduce TGSI lowering pass for images

This is loosely based on the previous lowering pass wrote by calim
four years ago. I did clean the code and fixed some issues.

Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Ilia Mirkin <imirkin@alum.mit.edu>