blob: c6a848c658084ff539271cb70203d768a0425a71 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 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
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -070018#include <GLES/gl.h>
19#include <GLES/glext.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070020#include "rsContext.h"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#include <OpenGL/glext.h>
25#endif //ANDROID_RS_BUILD_FOR_HOST
26
Jason Sams326e0dd2009-05-22 14:03:28 -070027#include "rsSampler.h"
28
Jason Sams1aa5a4e2009-06-22 17:15:15 -070029
Jason Sams326e0dd2009-05-22 14:03:28 -070030using namespace android;
31using namespace android::renderscript;
32
33
Jason Samse514b452009-09-25 14:51:22 -070034Sampler::Sampler(Context *rsc) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070035{
Jason Samsf2649a92009-09-25 16:37:33 -070036 mAllocFile = __FILE__;
37 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070038 // Should not get called.
39 rsAssert(0);
40}
41
Jason Samse514b452009-09-25 14:51:22 -070042Sampler::Sampler(Context *rsc,
43 RsSamplerValue magFilter,
Jason Sams326e0dd2009-05-22 14:03:28 -070044 RsSamplerValue minFilter,
45 RsSamplerValue wrapS,
46 RsSamplerValue wrapT,
Jason Samse514b452009-09-25 14:51:22 -070047 RsSamplerValue wrapR) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070048{
Jason Samsf2649a92009-09-25 16:37:33 -070049 mAllocFile = __FILE__;
50 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070051 mMagFilter = magFilter;
52 mMinFilter = minFilter;
53 mWrapS = wrapS;
54 mWrapT = wrapT;
55 mWrapR = wrapR;
56}
57
58Sampler::~Sampler()
59{
60}
61
Jason Sams900f1612010-09-16 18:18:29 -070062void Sampler::setupGL(const Context *rsc, const Allocation *tex)
Jason Sams326e0dd2009-05-22 14:03:28 -070063{
Jason Sams2f2898c2009-05-28 16:16:24 -070064 GLenum trans[] = {
Jason Sams39c8bc72009-05-28 15:37:57 -070065 GL_NEAREST, //RS_SAMPLER_NEAREST,
66 GL_LINEAR, //RS_SAMPLER_LINEAR,
Jason Sams2f2898c2009-05-28 16:16:24 -070067 GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
68 GL_REPEAT, //RS_SAMPLER_WRAP,
69 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
Jason Sams2f2898c2009-05-28 16:16:24 -070070 };
Jason Sams39c8bc72009-05-28 15:37:57 -070071
Jason Sams4c5f99e2010-09-14 14:59:03 -070072 GLenum transNP[] = {
73 GL_NEAREST, //RS_SAMPLER_NEAREST,
74 GL_LINEAR, //RS_SAMPLER_LINEAR,
75 GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
76 GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP,
77 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
78 };
Jason Samsef21edc2010-02-22 15:37:51 -080079
Jason Sams900f1612010-09-16 18:18:29 -070080 if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) {
Jason Sams4c5f99e2010-09-14 14:59:03 -070081 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080085 } else {
Jason Sams900f1612010-09-16 18:18:29 -070086 if (tex->getHasGraphicsMipmaps()) {
87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
88 } else {
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
90 }
Jason Sams4c5f99e2010-09-14 14:59:03 -070091 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080094 }
Jason Sams326e0dd2009-05-22 14:03:28 -070095
Jason Sams4c5f99e2010-09-14 14:59:03 -070096 rsc->checkError("Sampler::setupGL2 tex env");
Jason Sams326e0dd2009-05-22 14:03:28 -070097}
98
99void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
100{
101 ss->mSamplers[slot].set(this);
102 mBoundSlot = slot;
103}
104
105void Sampler::unbindFromContext(SamplerState *ss)
106{
107 int32_t slot = mBoundSlot;
108 mBoundSlot = -1;
109 ss->mSamplers[slot].clear();
110}
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700111
112void Sampler::serialize(OStream *stream) const
113{
Jason Sams4c5f99e2010-09-14 14:59:03 -0700114
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700115}
116
117Sampler *Sampler::createFromStream(Context *rsc, IStream *stream)
118{
119 return NULL;
120}
121
Jason Sams3eb28f02010-01-27 14:41:43 -0800122/*
Jason Sams326e0dd2009-05-22 14:03:28 -0700123void SamplerState::setupGL()
124{
Jason Sams39c8bc72009-05-28 15:37:57 -0700125 for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700126 Sampler *s = mSamplers[ct].get();
127 if (s) {
Jason Sams3eb28f02010-01-27 14:41:43 -0800128 s->setupGL(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700129 } else {
130 glBindTexture(GL_TEXTURE_2D, 0);
131 }
132 }
Jason Sams3eb28f02010-01-27 14:41:43 -0800133}*/
Jason Sams326e0dd2009-05-22 14:03:28 -0700134
135////////////////////////////////
136
137namespace android {
138namespace renderscript {
139
140
141void rsi_SamplerBegin(Context *rsc)
142{
143 SamplerState * ss = &rsc->mStateSampler;
144
145 ss->mMagFilter = RS_SAMPLER_LINEAR;
146 ss->mMinFilter = RS_SAMPLER_LINEAR;
147 ss->mWrapS = RS_SAMPLER_WRAP;
148 ss->mWrapT = RS_SAMPLER_WRAP;
149 ss->mWrapR = RS_SAMPLER_WRAP;
150}
151
152void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
153{
154 SamplerState * ss = &rsc->mStateSampler;
155
156 switch(param) {
157 case RS_SAMPLER_MAG_FILTER:
158 ss->mMagFilter = value;
159 break;
160 case RS_SAMPLER_MIN_FILTER:
161 ss->mMinFilter = value;
162 break;
163 case RS_SAMPLER_WRAP_S:
164 ss->mWrapS = value;
165 break;
166 case RS_SAMPLER_WRAP_T:
167 ss->mWrapT = value;
168 break;
169 case RS_SAMPLER_WRAP_R:
170 ss->mWrapR = value;
171 break;
172 }
173
174}
175
176RsSampler rsi_SamplerCreate(Context *rsc)
177{
178 SamplerState * ss = &rsc->mStateSampler;
179
180
Jason Samse514b452009-09-25 14:51:22 -0700181 Sampler * s = new Sampler(rsc,
182 ss->mMagFilter,
Jason Sams707aaf32009-08-18 14:14:24 -0700183 ss->mMinFilter,
184 ss->mWrapS,
Jason Sams326e0dd2009-05-22 14:03:28 -0700185 ss->mWrapT,
186 ss->mWrapR);
Jason Sams9397e302009-08-27 20:23:34 -0700187 s->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700188 return s;
189}
190
Jason Sams39c8bc72009-05-28 15:37:57 -0700191
Jason Sams326e0dd2009-05-22 14:03:28 -0700192}}