blob: 99091a911816adb6ccac3dec5910a74cdb202154 [file] [log] [blame]
Jason Samsd19f10d2009-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
Jason Samsd19f10d2009-05-22 14:03:28 -070017#include <GLES/gl.h>
18#include <GLES/glext.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070019
20#include "rsContext.h"
21#include "rsSampler.h"
22
Jason Sams4b962e52009-06-22 17:15:15 -070023
Jason Samsd19f10d2009-05-22 14:03:28 -070024using namespace android;
25using namespace android::renderscript;
26
27
Jason Samsa9e7a052009-09-25 14:51:22 -070028Sampler::Sampler(Context *rsc) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070029{
30 // Should not get called.
31 rsAssert(0);
32}
33
Jason Samsa9e7a052009-09-25 14:51:22 -070034Sampler::Sampler(Context *rsc,
35 RsSamplerValue magFilter,
Jason Samsd19f10d2009-05-22 14:03:28 -070036 RsSamplerValue minFilter,
37 RsSamplerValue wrapS,
38 RsSamplerValue wrapT,
Jason Samsa9e7a052009-09-25 14:51:22 -070039 RsSamplerValue wrapR) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070040{
41 mMagFilter = magFilter;
42 mMinFilter = minFilter;
43 mWrapS = wrapS;
44 mWrapT = wrapT;
45 mWrapR = wrapR;
46}
47
48Sampler::~Sampler()
49{
50}
51
52void Sampler::setupGL()
53{
Jason Sams243e3fb2009-05-28 16:16:24 -070054 GLenum trans[] = {
Jason Sams02fb2cb2009-05-28 15:37:57 -070055 GL_NEAREST, //RS_SAMPLER_NEAREST,
56 GL_LINEAR, //RS_SAMPLER_LINEAR,
Jason Sams243e3fb2009-05-28 16:16:24 -070057 GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
58 GL_REPEAT, //RS_SAMPLER_WRAP,
59 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
Jason Sams02fb2cb2009-05-28 15:37:57 -070060
Jason Sams243e3fb2009-05-28 16:16:24 -070061 };
Jason Sams02fb2cb2009-05-28 15:37:57 -070062
63
Jason Sams243e3fb2009-05-28 16:16:24 -070064 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Samsd19f10d2009-05-22 14:03:28 -070068
69}
70
71void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
72{
73 ss->mSamplers[slot].set(this);
74 mBoundSlot = slot;
75}
76
77void Sampler::unbindFromContext(SamplerState *ss)
78{
79 int32_t slot = mBoundSlot;
80 mBoundSlot = -1;
81 ss->mSamplers[slot].clear();
82}
83
84void SamplerState::setupGL()
85{
Jason Sams02fb2cb2009-05-28 15:37:57 -070086 for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
Jason Samsd19f10d2009-05-22 14:03:28 -070087 Sampler *s = mSamplers[ct].get();
88 if (s) {
89 s->setupGL();
90 } else {
91 glBindTexture(GL_TEXTURE_2D, 0);
92 }
93 }
94}
95
96////////////////////////////////
97
98namespace android {
99namespace renderscript {
100
101
102void rsi_SamplerBegin(Context *rsc)
103{
104 SamplerState * ss = &rsc->mStateSampler;
105
106 ss->mMagFilter = RS_SAMPLER_LINEAR;
107 ss->mMinFilter = RS_SAMPLER_LINEAR;
108 ss->mWrapS = RS_SAMPLER_WRAP;
109 ss->mWrapT = RS_SAMPLER_WRAP;
110 ss->mWrapR = RS_SAMPLER_WRAP;
111}
112
113void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
114{
115 SamplerState * ss = &rsc->mStateSampler;
116
117 switch(param) {
118 case RS_SAMPLER_MAG_FILTER:
119 ss->mMagFilter = value;
120 break;
121 case RS_SAMPLER_MIN_FILTER:
122 ss->mMinFilter = value;
123 break;
124 case RS_SAMPLER_WRAP_S:
125 ss->mWrapS = value;
126 break;
127 case RS_SAMPLER_WRAP_T:
128 ss->mWrapT = value;
129 break;
130 case RS_SAMPLER_WRAP_R:
131 ss->mWrapR = value;
132 break;
133 }
134
135}
136
137RsSampler rsi_SamplerCreate(Context *rsc)
138{
139 SamplerState * ss = &rsc->mStateSampler;
140
141
Jason Samsa9e7a052009-09-25 14:51:22 -0700142 Sampler * s = new Sampler(rsc,
143 ss->mMagFilter,
Jason Sams7ce033d2009-08-18 14:14:24 -0700144 ss->mMinFilter,
145 ss->mWrapS,
Jason Samsd19f10d2009-05-22 14:03:28 -0700146 ss->mWrapT,
147 ss->mWrapR);
Jason Sams07ae4062009-08-27 20:23:34 -0700148 s->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700149 return s;
150}
151
Jason Sams02fb2cb2009-05-28 15:37:57 -0700152
Jason Samsd19f10d2009-05-22 14:03:28 -0700153}}