blob: add5cd837e95d2009a470eba7b3d553ca9c3263a [file] [log] [blame]
Jason Sams7c4b8882013-01-04 10:50:05 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#include "rsCpuIntrinsic.h"
19#include "rsCpuIntrinsicInlines.h"
20
21using namespace android;
22using namespace android::renderscript;
23
24namespace android {
25namespace renderscript {
26
27
28class RsdCpuScriptIntrinsic3DLUT : public RsdCpuScriptIntrinsic {
29public:
Stephen Hinesc060f142015-05-13 19:26:09 -070030 void populateScript(Script *) override;
31 void invokeFreeChildren() override;
Jason Sams7c4b8882013-01-04 10:50:05 -080032
Stephen Hinesc060f142015-05-13 19:26:09 -070033 void setGlobalObj(uint32_t slot, ObjectBase *data) override;
Jason Sams7c4b8882013-01-04 10:50:05 -080034
Stephen Hinesc060f142015-05-13 19:26:09 -070035 ~RsdCpuScriptIntrinsic3DLUT() override;
Jason Sams7c4b8882013-01-04 10:50:05 -080036 RsdCpuScriptIntrinsic3DLUT(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e);
37
38protected:
39 ObjectBaseRef<Allocation> mLUT;
40
David Grossb0abb142015-03-12 15:23:03 -070041 static void kernel(const RsExpandKernelDriverInfo *info,
Jason Sams7c4b8882013-01-04 10:50:05 -080042 uint32_t xstart, uint32_t xend,
Chris Wailes9ed79102014-07-25 15:53:28 -070043 uint32_t outstep);
Jason Sams7c4b8882013-01-04 10:50:05 -080044};
45
46}
47}
48
49
50void RsdCpuScriptIntrinsic3DLUT::setGlobalObj(uint32_t slot, ObjectBase *data) {
51 rsAssert(slot == 0);
52 mLUT.set(static_cast<Allocation *>(data));
53}
54
Simon Hosie07e46652014-04-09 17:31:07 -070055extern "C" void rsdIntrinsic3DLUT_K(void *dst, void const *in, size_t count,
Simon Hosie5dcaaa52014-03-15 21:23:51 -070056 void const *lut,
57 int32_t pitchy, int32_t pitchz,
58 int dimx, int dimy, int dimz);
Jason Sams7c4b8882013-01-04 10:50:05 -080059
60
David Grossb0abb142015-03-12 15:23:03 -070061void RsdCpuScriptIntrinsic3DLUT::kernel(const RsExpandKernelDriverInfo *info,
Chris Wailes9ed79102014-07-25 15:53:28 -070062 uint32_t xstart, uint32_t xend,
63 uint32_t outstep) {
David Grossb0abb142015-03-12 15:23:03 -070064 RsdCpuScriptIntrinsic3DLUT *cp = (RsdCpuScriptIntrinsic3DLUT *)info->usr;
Jason Sams7c4b8882013-01-04 10:50:05 -080065
David Grossb0abb142015-03-12 15:23:03 -070066 uchar4 *out = (uchar4 *)info->outPtr[0];
67 uchar4 *in = (uchar4 *)info->inPtr[0];
Jason Sams7c4b8882013-01-04 10:50:05 -080068 uint32_t x1 = xstart;
69 uint32_t x2 = xend;
70
71 const uchar *bp = (const uchar *)cp->mLUT->mHal.drvState.lod[0].mallocPtr;
72
73 int4 dims = {
synergy dev8994abb2013-12-05 00:24:37 -080074 static_cast<int>(cp->mLUT->mHal.drvState.lod[0].dimX - 1),
75 static_cast<int>(cp->mLUT->mHal.drvState.lod[0].dimY - 1),
76 static_cast<int>(cp->mLUT->mHal.drvState.lod[0].dimZ - 1),
Stephen Hinesd533c4c2013-03-06 02:55:32 -080077 -1
Jason Sams7c4b8882013-01-04 10:50:05 -080078 };
Stephen Hinesd533c4c2013-03-06 02:55:32 -080079 const float4 m = (float4)(1.f / 255.f) * convert_float4(dims);
Jason Sams7c4b8882013-01-04 10:50:05 -080080 const int4 coordMul = convert_int4(m * (float4)0x8000);
81 const size_t stride_y = cp->mLUT->mHal.drvState.lod[0].stride;
82 const size_t stride_z = stride_y * cp->mLUT->mHal.drvState.lod[0].dimY;
83
84 //ALOGE("strides %zu %zu", stride_y, stride_z);
85
Jason Sams074424a2014-05-22 13:30:03 -070086#if defined(ARCH_ARM_USE_INTRINSICS)
Simon Hosie07e46652014-04-09 17:31:07 -070087 if (gArchUseSIMD) {
88 int32_t len = x2 - x1;
89 if(len > 0) {
90 rsdIntrinsic3DLUT_K(out, in, len,
91 bp, stride_y, stride_z,
92 dims.x, dims.y, dims.z);
93 x1 += len;
94 out += len;
95 in += len;
Jason Sams7c4b8882013-01-04 10:50:05 -080096 }
Simon Hosie07e46652014-04-09 17:31:07 -070097 }
Jason Sams7c4b8882013-01-04 10:50:05 -080098#endif
99
Simon Hosie07e46652014-04-09 17:31:07 -0700100 while (x1 < x2) {
Jason Sams7c4b8882013-01-04 10:50:05 -0800101 int4 baseCoord = convert_int4(*in) * coordMul;
102 int4 coord1 = baseCoord >> (int4)15;
103 //int4 coord2 = min(coord1 + 1, gDims - 1);
104
105 int4 weight2 = baseCoord & 0x7fff;
106 int4 weight1 = (int4)0x8000 - weight2;
107
108 //ALOGE("coord1 %08x %08x %08x %08x", coord1.x, coord1.y, coord1.z, coord1.w);
109 const uchar *bp2 = bp + (coord1.x * 4) + (coord1.y * stride_y) + (coord1.z * stride_z);
110 const uchar4 *pt_00 = (const uchar4 *)&bp2[0];
111 const uchar4 *pt_10 = (const uchar4 *)&bp2[stride_y];
112 const uchar4 *pt_01 = (const uchar4 *)&bp2[stride_z];
113 const uchar4 *pt_11 = (const uchar4 *)&bp2[stride_y + stride_z];
114
115 uint4 v000 = convert_uint4(pt_00[0]);
116 uint4 v100 = convert_uint4(pt_00[1]);
117 uint4 v010 = convert_uint4(pt_10[0]);
118 uint4 v110 = convert_uint4(pt_10[1]);
119 uint4 v001 = convert_uint4(pt_01[0]);
120 uint4 v101 = convert_uint4(pt_01[1]);
121 uint4 v011 = convert_uint4(pt_11[0]);
122 uint4 v111 = convert_uint4(pt_11[1]);
123
124 uint4 yz00 = ((v000 * weight1.x) + (v100 * weight2.x)) >> (int4)7;
125 uint4 yz10 = ((v010 * weight1.x) + (v110 * weight2.x)) >> (int4)7;
126 uint4 yz01 = ((v001 * weight1.x) + (v101 * weight2.x)) >> (int4)7;
127 uint4 yz11 = ((v011 * weight1.x) + (v111 * weight2.x)) >> (int4)7;
128
Stephen Hines5e3fb0b2013-01-10 01:45:46 -0800129 uint4 z0 = ((yz00 * weight1.y) + (yz10 * weight2.y)) >> (int4)15;
130 uint4 z1 = ((yz01 * weight1.y) + (yz11 * weight2.y)) >> (int4)15;
Jason Sams7c4b8882013-01-04 10:50:05 -0800131
Stephen Hines5e3fb0b2013-01-10 01:45:46 -0800132 uint4 v = ((z0 * weight1.z) + (z1 * weight2.z)) >> (int4)15;
Jason Sams7c4b8882013-01-04 10:50:05 -0800133 uint4 v2 = (v + 0x7f) >> (int4)8;
134
135 uchar4 ret = convert_uchar4(v2);
Tim Murray0b575de2013-03-15 15:56:43 -0700136 ret.w = in->w;
Jason Sams7c4b8882013-01-04 10:50:05 -0800137
138 #if 0
139 if (!x1) {
140 ALOGE("in %08x %08x %08x %08x", in->r, in->g, in->b, in->a);
141 ALOGE("baseCoord %08x %08x %08x %08x", baseCoord.x, baseCoord.y, baseCoord.z, baseCoord.w);
142 ALOGE("coord1 %08x %08x %08x %08x", coord1.x, coord1.y, coord1.z, coord1.w);
143 ALOGE("weight1 %08x %08x %08x %08x", weight1.x, weight1.y, weight1.z, weight1.w);
144 ALOGE("weight2 %08x %08x %08x %08x", weight2.x, weight2.y, weight2.z, weight2.w);
145
146 ALOGE("v000 %08x %08x %08x %08x", v000.x, v000.y, v000.z, v000.w);
147 ALOGE("v100 %08x %08x %08x %08x", v100.x, v100.y, v100.z, v100.w);
148 ALOGE("yz00 %08x %08x %08x %08x", yz00.x, yz00.y, yz00.z, yz00.w);
149 ALOGE("z0 %08x %08x %08x %08x", z0.x, z0.y, z0.z, z0.w);
150
151 ALOGE("v %08x %08x %08x %08x", v.x, v.y, v.z, v.w);
152 ALOGE("v2 %08x %08x %08x %08x", v2.x, v2.y, v2.z, v2.w);
153 }
154 #endif
155 *out = ret;
156
157
158 in++;
159 out++;
160 x1++;
161 }
Jason Sams7c4b8882013-01-04 10:50:05 -0800162}
163
Chris Wailesf3712132014-07-16 15:18:30 -0700164RsdCpuScriptIntrinsic3DLUT::RsdCpuScriptIntrinsic3DLUT(
165 RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) :
166 RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_3DLUT) {
Jason Sams7c4b8882013-01-04 10:50:05 -0800167
168 mRootPtr = &kernel;
169}
170
171RsdCpuScriptIntrinsic3DLUT::~RsdCpuScriptIntrinsic3DLUT() {
172}
173
174void RsdCpuScriptIntrinsic3DLUT::populateScript(Script *s) {
175 s->mHal.info.exportedVariableCount = 1;
176}
177
178void RsdCpuScriptIntrinsic3DLUT::invokeFreeChildren() {
179 mLUT.clear();
180}
181
182
183RsdCpuScriptImpl * rsdIntrinsic_3DLUT(RsdCpuReferenceImpl *ctx,
184 const Script *s, const Element *e) {
185
186 return new RsdCpuScriptIntrinsic3DLUT(ctx, s, e);
187}