source: nanovis/branches/1.2/shaders/mergesort.cg @ 5046

Last change on this file since 5046 was 2852, checked in by ldelgass, 12 years ago

Fix up line endings and formatting in shader code, also add emacs magic line
to get C++ mode highlighting

File size: 3.7 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2// Copyright (c) 2004-2005 Lutz Latta
3//
4// Permission is hereby granted, free of charge, to any person obtaining
5// a copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// Odd-even merge sort, implemented via parallel sorting network.
23#define USE_RGB_FORMAT
24
25#ifndef USE_RGB_FORMAT
26float2
27#else
28float3
29#endif
30mergeSortEnd(float2 current : TEXCOORD0,
31             uniform samplerRECT sortData : register(s0),
32             uniform int2 size,
33             uniform int step) : COLOR
34{
35#ifndef USE_RGB_FORMAT
36    float2 currentSample = texRECT(sortData, (float2)current).xy;
37#else
38    float3 currentSample = texRECT(sortData, (float2)current).xyz;
39#endif
40
41    float i = (current.y - 0.5) * size.x + current.x;
42    //float i = current.y * size.x + current.x;
43
44    // swap direction
45    float b = (fmod(i / step, 2.0) < 1.0 ? 1.0 : -1.0);
46
47       
48    float otherI = i + (b * step);
49    float2 otherPos = float2(fmod(otherI, size.x), floor(otherI / size.x) + 0.5);
50    //float2 otherPos = float2(fmod(otherI, size.x), floor(otherI / size.x));
51#ifndef USE_RGB_FORMAT
52    float2 otherSample = texRECT(sortData, otherPos).xy;
53#else
54    float3 otherSample = texRECT(sortData, otherPos).xyz;
55#endif
56    if (b >= 0) {
57        return currentSample.x > otherSample.x ? currentSample : otherSample;
58    } else {
59        return currentSample.x < otherSample.x ? currentSample : otherSample;
60    }
61}
62#ifndef USE_RGB_FORMAT
63float2
64#else
65float3
66#endif
67mergeSortRecursion(float2 _Current : TEXCOORD0,
68                   uniform samplerRECT _SortData : register(s0),
69                   uniform int2 _Size,
70                   uniform int _Step,
71                   uniform int _Count) : COLOR
72{
73#ifndef USE_RGB_FORMAT
74    float2 currentSample = texRECT(_SortData, (float2)_Current).xy;
75#else
76    float3 currentSample = texRECT(_SortData, (float2)_Current).xyz;
77#endif
78    float i = (_Current.y - 0.5) * _Size.x + _Current.x;
79    //float i = _Current.y * _Size.x + _Current.x;
80
81    float iMod = fmod((float)(i / _Step), (float)_Count);
82
83    if (iMod >= 1.0 && iMod < _Count - 1.0) {
84        float b = (fmod((float)iMod, 2.0) > 1.0 ? 1.0 : -1.0);
85
86        float otherI = i + (b * _Step);
87        float2 otherPos = float2(fmod(otherI, _Size.x), floor(otherI / _Size.x) + 0.5);
88        //float2 otherPos = float2(fmod(otherI, _Size.x), floor(otherI / _Size.x));
89
90#ifndef USE_RGB_FORMAT 
91        float2 otherSample = texRECT(_SortData, otherPos).xy;
92#else
93        float3 otherSample = texRECT(_SortData, otherPos).xyz;
94#endif
95        if (b >= 0) {
96            return currentSample.x > otherSample.x ? currentSample : otherSample;
97        } else {
98            return currentSample.x < otherSample.x ? currentSample : otherSample;
99        }
100    } else
101        return currentSample;
102}
Note: See TracBrowser for help on using the repository browser.